views:

2079

answers:

2

I use a class for detecting email addresses which uses static final booleans to configure the matching behavior. Since I upgraded to Eclipse 3.5 I get warnings about dead code, since Eclipse notices that one branch in this can not be reached:

private static final boolean ALLOW_DOMAIN_LITERALS = false;
private static final String domain = ALLOW_DOMAIN_LITERALS ? rfc2822Domain : rfc1035DomainName;

Oddly enough it is happy with this:

private static final String domain;
static {
    if(ALLOW_DOMAIN_LITERALS) {
        domain = rfc2822Domain;
    } else {
        domain= rfc1035DomainName;
    }
}

since it seems to recognize the common if(DEBUG) pattern, but the ternary operator doesn't seem to count.

Since I'd rather not fork the class too much just to keep Eclipse happy, I'd prefer putting an @SuppressWarnings at the top instead of changing the code. Unfortunately I can't find a matching one apart from the brute-force "all". Is there a value just for the dead code detection?

+3  A: 

Select Ignore in Windows -> Preferences > Java > Compiler > Errors/Warnings under Potential programming problems section

J-16 SDiZ
But I generally like the feature, it just fails here. If it would be my own code I'd just change it. You could also disable it on the project level (i.e. enable project-specific compiler settings in the project properties), but I don't even want to do that.
Peter Becker
+6  A: 

All SuppressWarnings values Eclipse 3.5 "knows" are listed at the end of this page. It seems that there is no value for suppressing only the new dead-code detection. But you can use the @SuppressWarnings("all") just before the domain declaration so it will suppress warnings for only that line not for the whole class:

private static final boolean ALLOW_DOMAIN_LITERALS = false;
@SuppressWarnings("all") 
private static final String domain = ALLOW_DOMAIN_LITERALS ? rfc2822Domain : rfc1035DomainName;

Because dead code check is a new one you can also suggest an enchancement in the Eclipse bug database for supporting the ternary operation as well.

Csaba_H
Here is the bugzilla entry: https://bugs.eclipse.org/bugs/show_bug.cgi?id=282768Bugzilla certainly lacks both the duplicate search and Wiki syntax StackOverflow offers :-)
Peter Becker
I added one for the missing @SuppressWarnings, too: https://bugs.eclipse.org/bugs/show_bug.cgi?id=282770
Peter Becker
The status of the both requests is now verified fixed
Casebash
It appears that you can not do this for a particular line inside a method. It must be done for a whole method, otherwise a "insert EnumBody" error is shown.
Adam