Skip to content

Commit

Permalink
@NonNullByDefault does not work for type arguments of a local type (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-herrmann authored and rgrunber committed Jan 9, 2024
1 parent 67fce40 commit 6d61b03
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
package org.eclipse.jdt.internal.compiler.lookup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -373,6 +374,7 @@ void buildLocalTypeBinding(SourceTypeBinding enclosingType) {
checkParameterizedTypeBounds();
checkParameterizedSuperTypeCollisions();
}
this.referenceContext.updateSupertypesWithAnnotations(Collections.emptyMap());
buildFieldsAndMethods();
localType.faultInTypesForFieldsAndMethods();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19132,4 +19132,114 @@ void foo(String... strings) {
runner.classLibraries = this.LIBS;
runner.runConformTest();
}
public void testGH1693_a() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"X.java",
"""
import java.util.Iterator;
import org.eclipse.jdt.annotation.NonNullByDefault;

@NonNullByDefault
public class X {
Iterator<String> getProcessIterator() {
abstract class StringIterator implements Iterator<String> { }
return new StringIterator() {
@Override
public boolean hasNext() {
return false;
}
@Override
public String next() {
return null;
}
};
}
}
"""
};
runner.customOptions = getCompilerOptions();
runner.classLibraries = this.LIBS;
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 15)\n" +
" return null;\n" +
" ^^^^\n" +
"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" +
"----------\n";
runner.runNegativeTest();
}
public void testGH1693_b() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"X.java",
"""
import java.util.Iterator;
import org.eclipse.jdt.annotation.NonNullByDefault;

@NonNullByDefault
public class X {
Iterator<String> getProcessIterator() {
class StringIterator implements Iterator<String> {
@Override
public boolean hasNext() {
return false;
}
@Override
public String next() {
return null;
}
}
return new StringIterator();
}
}
"""
};
runner.customOptions = getCompilerOptions();
runner.classLibraries = this.LIBS;
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 14)\n" +
" return null;\n" +
" ^^^^\n" +
"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" +
"----------\n";
runner.runNegativeTest();
}
public void testGH1693_c() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"X.java",
"""
import java.util.Iterator;
import org.eclipse.jdt.annotation.NonNull;

public class X {
Iterator<@NonNull String> getProcessIterator() {
class StringIterator implements Iterator<@NonNull String> {
@Override
public boolean hasNext() {
return false;
}
@Override
public @NonNull String next() {
return null;
}
}
return new StringIterator();
}
}
"""
};
runner.customOptions = getCompilerOptions();
runner.classLibraries = this.LIBS;
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 13)\n" +
" return null;\n" +
" ^^^^\n" +
"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" +
"----------\n";
runner.runNegativeTest();
}
}

0 comments on commit 6d61b03

Please sign in to comment.