3

I'm using the Eclipse JDT Null annotation processor and I'm getting some weird behaviour when using java.lang.Class.

package test; import org.eclipse.jdt.annotation.Nullable; public class AnnotationSpul { @Nullable public <V> V get1(Class<V> type) { return get2(type); //This line has a warning } @Nullable public <V> V get2(Class<V> type) { return null; } } 

This is my package info:

@NonNullByDefault({ PARAMETER, RETURN_TYPE, FIELD }) package test; import static org.eclipse.jdt.annotation.DefaultLocation.FIELD; import static org.eclipse.jdt.annotation.DefaultLocation.PARAMETER; import static org.eclipse.jdt.annotation.DefaultLocation.RETURN_TYPE; import org.eclipse.jdt.annotation.NonNullByDefault; 

The warning I get is: "The expression of type '@NonNull Class' needs unchecked conversion to conform to '@NonNull Class<@Nullable V>'"

I don't understand why I get the warning. The method signatures are exactly the same, so why does the value passed need conversion? Why is type inferred as @NonNull Class<V> in one method and @NonNull Class<@Nullable V> in the other?

9
  • I am not very familiar with those annotations, but for me it looks like you declared to have parameters and return values non null by default (in the package info). Afterwards you declare a method (and thus the return type) as nullable. This is obviously contradictory, isn't it? [I know, this doesn't deal with the difference between those two methods.] Commented Apr 23, 2015 at 11:23
  • 1
    I tried this using the Checker Framework's Nullness Checker and it worked as you desire. The Nullness Checker even recognizes Eclipse's annotations. Commented Apr 23, 2015 at 23:08
  • As a matter of style, so long as you are usng a Java 8 compiler it is best to write a type annotation next to the type it modifies. For example, use public <V> @Nullable V get1(...) rather than @Nullable public <V> V get1(...). Commented Apr 23, 2015 at 23:11
  • 1
    @DavidtenHove Yes, the Nullness Checker of the Checker Framework does not give the warning. It is more precise than the Eclipse JDT Null Analysis, for your example. Commented Apr 24, 2015 at 12:56
  • 1
    For posterity: this was a bug in ecj, that got fixed as of 4.5M5. Commented Jun 1, 2015 at 22:08

1 Answer 1

2

This was a bug in ecj 4.4. In a combination of type inference and null inference ecj over-eagerly inferred the type parameter to @Nullable V.

The bug was fixed in 4.5M5, so the upcoming release 4.5 (Mars) will accept the program as expected.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.