synic
synic

Reputation: 26668

Getting rid of annoying warnings in Eclipse

The following code, where Config.PREFERENCES_ENABLE is a static final boolean, produces a compiler warning (on all the code after the if block and the warning is "Dead Code"), and trying to use @SuppressWarnings("all") on it produces a syntax error. I know I'm being a bit OCD here, but you know how it is.

void displayPreferences() {

    if(!Config.PREFERENCES_ENABLED) return;

    startActivityForResult(
            new Intent(this, PrefsActivity.class),
            PREFERENCE_ACTIVITY);
}

Upvotes: 3

Views: 476

Answers (3)

Stephen C
Stephen C

Reputation: 718718

Why don't you just write it like this?

void displayPreferences() {
    if (Config.PREFERENCES_ENABLED) {
        startActivityForResult(
            new Intent(this, PrefsActivity.class),
            PREFERENCE_ACTIVITY);
    }
}

Upvotes: 0

WhiteFang34
WhiteFang34

Reputation: 72039

Aside from changing the Eclipse compiler preferences, there are a few other options you can choose:

  1. Specify @SuppressWarnings("unused") for the method to suppress the specific warning.
  2. Remove the final from Config.PREFERENCES_ENABLED to satisfy the compiler.
  3. Store the preference in some external file and read that at runtime.

The first option at least allows you to keep the unused compiler warning enabled, which might be useful in other parts of code later. The second option might not be desirable if you're really concerned that something might change it during runtime (or perhaps that's even desirable). The third option has the benefit of not hard coding the preference and allowing it be configured in an external file that's easier to change without recompiling code.

Upvotes: 3

Isaac Truett
Isaac Truett

Reputation: 8874

Window > Preferences > Java > Compiler > Errors/Warnings > Unused Code

I think. I don't have Eclipse open in front of me. You can use the filter box in the preferences dialog to find it, too.

Upvotes: 1

Related Questions