Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShankha Banerjee2014-02-20 22:45:24 +0000
committerStephan Herrmann2014-02-20 22:46:30 +0000
commitf1a111b3a23818f90206cf827aa78528b873971f (patch)
treebab6dc6e4b22d352b16aae2c95c4d1fb0c6af212
parent42bd9b01a3f5cafa3c20186dd8b7608f467f1611 (diff)
downloadeclipse.jdt.core-I20140225-0800.tar.gz
eclipse.jdt.core-I20140225-0800.tar.xz
eclipse.jdt.core-I20140225-0800.zip
Bug 411771 - [compiler][null] Enum constants not recognised as beingI20140225-1430I20140225-0800
NonNull. Signed-off-by: Shankha Banerjee <shankhba@in.ibm.com>
-rw-r--r--org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/AnnotationDependencyTests.java108
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java11
2 files changed, 115 insertions, 4 deletions
diff --git a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/AnnotationDependencyTests.java b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/AnnotationDependencyTests.java
index dc0cbaddd3..03769b8f7b 100644
--- a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/AnnotationDependencyTests.java
+++ b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/AnnotationDependencyTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2012 Walter Harley and others.
+ * Copyright (c) 2009, 2014 Walter Harley and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -1557,4 +1557,110 @@ public class AnnotationDependencyTests extends BuilderTests {
// verify that Test2 only was recompiled
expectingUniqueCompiledClasses(new String[] { "p1.Test2" });
}
+
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=411771
+ //[compiler][null] Enum constants not recognized as being NonNull.
+ //This test case exposes the bug mentioned in the defect. The enum
+ //definition comes from a file different from where it is accessed.
+ public void test411771a() throws IOException, JavaModelException {
+ setupProjectForNullAnnotations();
+ String testEnumCode = "package p1;\n" +
+ "enum TestEnum {FOO };\n";
+ env.addClass( this.srcRoot, "p1", "TestEnum", testEnumCode );
+ fullBuild( this.projectPath );
+ expectingNoProblems();
+
+ String nullTestCode = "package p1;\n" +
+ "import org.eclipse.jdt.annotation.NonNull;\n" +
+ "public class NullTest {\n" +
+ " public static TestEnum bla() {\n" +
+ " @NonNull final TestEnum t = TestEnum.FOO;\n" +
+ " return t;\n" +
+ " }\n" +
+ "}";
+ env.addClass( this.srcRoot, "p1", "NullTest", nullTestCode );
+ incrementalBuild( this.projectPath );
+ expectingNoProblems();
+
+ expectingUniqueCompiledClasses(new String[] { "p1.NullTest" });
+ }
+
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=411771
+ //[compiler][null] Enum constants not recognized as being NonNull.
+ //Distinguish between enum constant and enum type. The enum type should not
+ //be marked as NonNull.
+ public void test411771b() throws IOException, JavaModelException {
+ setupProjectForNullAnnotations();
+ String testEnumCode = "package p1;\n" +
+ "enum TestEnum { FOO };\n";
+ env.addClass( this.srcRoot, "p1", "TestEnum", testEnumCode );
+ fullBuild( this.projectPath );
+ expectingNoProblems();
+
+ String testClass = "package p1;\n" +
+ "public class X { TestEnum f; };\n";
+ env.addClass( this.srcRoot, "p1", "X", testClass );
+ incrementalBuild( this.projectPath );
+ expectingNoProblems();
+
+ String nullTestCode = "package p1;\n" +
+ "import org.eclipse.jdt.annotation.NonNull;\n" +
+ "public class NullTest {\n" +
+ " public static TestEnum bla(X x) {\n" +
+ " @NonNull final TestEnum t = x.f;\n" +
+ " return t;\n" +
+ " }\n" +
+ "}\n";
+ IPath test1Path = env.addClass( this.srcRoot, "p1", "NullTest", nullTestCode );
+ incrementalBuild( this.projectPath );
+
+ expectingProblemsFor(test1Path,
+ "Problem : Null type safety: The expression of type TestEnum needs unchecked conversion to conform to " +
+ "'@NonNull TestEnum' [ resource : </Project/src/p1/NullTest.java> range : <144,147> category : <90> severity : <1>]");
+
+ expectingUniqueCompiledClasses(new String[] { "p1.NullTest" });
+ }
+
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=411771
+ //[compiler][null] Enum constants not recognized as being NonNull.
+ //A enum may contain fields other than predefined constants. We
+ //should not tag them as NonNull.
+ public void test411771c() throws IOException, JavaModelException {
+ setupProjectForNullAnnotations();
+ String testClass = "package p1;\n" +
+ "public class A {}";
+ env.addClass( this.srcRoot, "p1", "A", testClass );
+ fullBuild( this.projectPath );
+ expectingNoProblems();
+
+ String testEnumCode = "package p1;\n" +
+ "enum TestEnum {\n" +
+ " FOO;\n" +
+ " public static A a;" +
+ "};\n";
+ env.addClass( this.srcRoot, "p1", "TestEnum", testEnumCode );
+ incrementalBuild( this.projectPath );
+ expectingNoProblems();
+
+ String nullTestCode = "package p1;\n" +
+ "import org.eclipse.jdt.annotation.NonNull;\n" +
+ "public class NullTest {\n" +
+ " public static TestEnum bla() {\n" +
+ " @NonNull final TestEnum t = TestEnum.FOO;\n" +
+ " return t;\n" +
+ " }\n" +
+ " public A testint() {\n" +
+ " @NonNull A a = TestEnum.a;\n" +
+ " return a;\n" +
+ " }\n" +
+ "}";
+ IPath test1Path = env.addClass( this.srcRoot, "p1", "NullTest", nullTestCode );
+ incrementalBuild( this.projectPath );
+ expectingProblemsFor(test1Path,
+ "Problem : Null type safety: The expression of type A needs unchecked conversion to conform to " +
+ "'@NonNull A' [ resource : </Project/src/p1/NullTest.java> range : <209,219> category : <90> severity : <1>]");
+
+ expectingUniqueCompiledClasses(new String[] { "p1.NullTest" });
+ }
+
}
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 9f7abc71fe..06461424d9 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -449,7 +449,7 @@ private void createFields(IBinaryField[] iFields, long sourceLevel, char[][][] m
if (this.environment.globalOptions.isAnnotationBasedNullAnalysisEnabled) {
for (int i = 0; i <size; i++) {
IBinaryField binaryField = iFields[i];
- scanFieldForNullAnnotation(binaryField, this.fields[i]);
+ scanFieldForNullAnnotation(binaryField, this.fields[i], this.isEnum());
}
}
}
@@ -1163,7 +1163,7 @@ SimpleLookupTable storedAnnotations(boolean forceInitialize) {
return this.storedAnnotations;
}
-void scanFieldForNullAnnotation(IBinaryField field, FieldBinding fieldBinding) {
+void scanFieldForNullAnnotation(IBinaryField field, FieldBinding fieldBinding, boolean isEnum) {
// global option is checked by caller
char[][] nullableAnnotationName = this.environment.getNullableAnnotationName();
char[][] nonNullAnnotationName = this.environment.getNonNullAnnotationName();
@@ -1196,6 +1196,11 @@ void scanFieldForNullAnnotation(IBinaryField field, FieldBinding fieldBinding) {
if (!explicitNullness && (this.tagBits & TagBits.AnnotationNonNullByDefault) != 0) {
fieldBinding.tagBits |= TagBits.AnnotationNonNull;
}
+ if (isEnum) {
+ if ((field.getModifiers() & ClassFileConstants.AccEnum) != 0) {
+ fieldBinding.tagBits |= TagBits.AnnotationNonNull;
+ }
+ }
}
void scanMethodForNullAnnotation(IBinaryMethod method, MethodBinding methodBinding) {

Back to the top