Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Herrmann2012-06-27 22:27:08 +0000
committerStephan Herrmann2012-09-01 12:37:41 +0000
commit375c3eabfe0fc59171b5af39f76294fd2cd37adf (patch)
tree871247c71935af22e26548e4763674154a968a9e
parent61a474732255f2151fd5c245d12df573f050207b (diff)
downloadeclipse.jdt.core-375c3eabfe0fc59171b5af39f76294fd2cd37adf.tar.gz
eclipse.jdt.core-375c3eabfe0fc59171b5af39f76294fd2cd37adf.tar.xz
eclipse.jdt.core-375c3eabfe0fc59171b5af39f76294fd2cd37adf.zip
Bug 331649 - [compiler][null] consider null annotations for fields
- apply nullness default to binary fields (test & fix)
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java67
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java6
2 files changed, 73 insertions, 0 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java
index cf54fe54dd..dc28313652 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java
@@ -4092,6 +4092,73 @@ public void test_nonnull_field_13() {
"");
}
+// A field in a different CU is implicitly @NonNull (by type default) - that class is read from binary
+// Assignment to other @NonNull field should not raise a warning
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=331649
+public void test_nonnull_field_14() {
+ runConformTestWithLibs(
+ new String[] {
+ "p1/X.java",
+ "package p1;\n" +
+ "import org.eclipse.jdt.annotation.*;\n" +
+ "@NonNullByDefault\n" +
+ "public class X {\n" +
+ " public String s1 = \"\";\n" +
+ "}\n",
+ },
+ null /*customOptions*/,
+ "");
+ runConformTestWithLibs(
+ new String[] {
+ "p2/Y.java",
+ "package p2;\n" +
+ "import org.eclipse.jdt.annotation.*;\n" +
+ "import p1.X;\n" +
+ "public class Y {\n" +
+ " @NonNull String s2 = \"\";\n" +
+ " void foo(X other) {\n" +
+ " s2 = other.s1;\n" +
+ " }\n" +
+ "}\n"
+ },
+ null /*customOptions*/,
+ "");
+}
+
+// A field in a different CU is implicitly @NonNull (by package default) - that class is read from binary
+// Assignment to other @NonNull field should not raise a warning
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=331649
+public void test_nonnull_field_14b() {
+ runConformTestWithLibs(
+ new String[] {
+ "p1/package-info.java",
+ "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
+ "package p1;\n",
+ "p1/X.java",
+ "package p1;\n" +
+ "public class X {\n" +
+ " public String s1 = \"\";\n" +
+ "}\n",
+ },
+ null /*customOptions*/,
+ "");
+ runConformTestWithLibs(
+ new String[] {
+ "p2/Y.java",
+ "package p2;\n" +
+ "import org.eclipse.jdt.annotation.*;\n" +
+ "import p1.X;\n" +
+ "public class Y {\n" +
+ " @NonNull String s2 = \"\";\n" +
+ " void foo(X other) {\n" +
+ " s2 = other.s1;\n" +
+ " }\n" +
+ "}\n"
+ },
+ null /*customOptions*/,
+ "");
+}
+
// access to a nullable field - field reference
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=331649
public void test_nullable_field_1() {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java
index fdd9258a8f..b8d4e2664b 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java
@@ -1171,6 +1171,7 @@ void scanFieldForNullAnnotation(IBinaryField field, FieldBinding fieldBinding) {
if (fieldBinding.type == null || fieldBinding.type.isBaseType())
return; // null annotations are only applied to reference types
+ boolean explicitNullness = false;
IBinaryAnnotation[] annotations = field.getAnnotations();
if (annotations != null) {
for (int i = 0; i < annotations.length; i++) {
@@ -1180,14 +1181,19 @@ void scanFieldForNullAnnotation(IBinaryField field, FieldBinding fieldBinding) {
char[][] typeName = CharOperation.splitOn('/', annotationTypeName, 1, annotationTypeName.length-1); // cut of leading 'L' and trailing ';'
if (CharOperation.equals(typeName, nonNullAnnotationName)) {
fieldBinding.tagBits |= TagBits.AnnotationNonNull;
+ explicitNullness = true;
break;
}
if (CharOperation.equals(typeName, nullableAnnotationName)) {
fieldBinding.tagBits |= TagBits.AnnotationNullable;
+ explicitNullness = true;
break;
}
}
}
+ if (!explicitNullness && (this.tagBits & TagBits.AnnotationNonNullByDefault) != 0) {
+ fieldBinding.tagBits |= TagBits.AnnotationNonNull;
+ }
}
void scanMethodForNullAnnotation(IBinaryMethod method, MethodBinding methodBinding) {

Back to the top