Community
Participate
Working Groups
JDT's null inference fails in the simple case of an intermediate variable between the null check of the object and the actual object usage. This is a problem expecially when using Xtend together with @NonNull/@Nullable annotations and strict checks, as Xtend generally creates Java code like: Object input = InputSource.getInput(); // return value annotated as @Nullable boolean _tripleNotEquals = (input != null); if (_tripleNotEquals) { Object convert = Converter.convert(input); // parameter annotated with @NonNull } In this example, it is clearly apparent that input can never be null in line 4. However, the JDT infers the input as potentially null / @Nullable and creates an error if the "Conflict between null annotations and null inference" preference is set to "Error". Steps to Reproduce: 1) Preparation: - go to Preferences | Java | Compiler | Errors/Warnings | Null Analysis - check "Enable annotation-based null analysis" - set "Conflict between null annotations and null inference" to "Error" 2) create Java class: package test; import java.util.Random; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; public class Example { @Nullable public Object getInput() { return new Random().nextBoolean() ? new Object() : null; } @NonNull public Object convert(@NonNull final Object input) { return input; } public void perform() { final Object input = getInput(); final boolean notNull = (input != null); if (notNull) { // leads to error "Null type mismatch: required '@NonNull Object' but the provided value is inferred as @Nullable" final Object converted = convert(input); } } }
This RFE is a member of a group of existing requests marked as [correlation]: https://bugs.eclipse.org/bugs/buglist.cgi?short_desc=[correlation]&classification=Eclipse&query_format=advanced&short_desc_type=allwordssubstr&component=Core&product=JDT&list_id=7605356 It is a known limitation of the existing analysis that it cannot track the correlation between local variables, like: "I know that when (notNull==true) then also (input!=null)". Adding correlation analysis would be a complex task, with significant impact on compiler performance. I don't have a strategy how to make both ends meet. Maybe you should ask the Xtend guys to stop generating that intermediate boolean variable. Alternatively: could you configure the source folder holding the generated Java files to ignore optional errors/warnings, or would that break warnings in Xtend, too?
Bulk closing all compiler bugs tagged [null][correlation], because we have no plans to add such a feature: it would be a tremendous implementation effort, beyond our current man power, and may be impossible to achieve within the desired performance bounds. If s.o. has a viable strategy or even implementation for such a feature, I'm all ears.
Verified for 4.7 M1.
I created a new umbrella RFE outlining what would be needed to address this issue. *** This bug has been marked as a duplicate of bug 538421 ***