| author | Stephan Herrmann | 2012-12-02 14:06:50 (EST) |
|---|---|---|
| committer | Jayaprakash Arthanareeswaran | 2012-12-04 08:23:27 (EST) |
| commit | 0001b1dcd0c4e435d76a7a88b7548ff6832acf06 (patch) (side-by-side diff) | |
| tree | 7cfa19f6629cca9f3a6efbafaa6627b64ab445fd | |
| parent | 16efd827ef17fd438d7ee95c275c7d08039d0249 (diff) | |
| download | eclipse.jdt.core-0001b1dcd0c4e435d76a7a88b7548ff6832acf06.zip eclipse.jdt.core-0001b1dcd0c4e435d76a7a88b7548ff6832acf06.tar.gz eclipse.jdt.core-0001b1dcd0c4e435d76a7a88b7548ff6832acf06.tar.bz2 | |
Bug 381443 - [compiler][null] Allow parameter widening from @NonNull to
unannotated
3 files changed, 42 insertions, 39 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java index 0912731..e2fd4e6 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java @@ -25,6 +25,7 @@ * bug 374605 - Unreasonable warning for enum-based switch statements * bug 375366 - ECJ ignores unusedParameterIncludeDocCommentReference unless enableJavadoc option is set * bug 388281 - [compiler][null] inheritance of null annotations as an option + * bug 381443 - [compiler][null] Allow parameter widening from @NonNull to unannotated *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -12633,12 +12634,7 @@ public void test313_warn_options() { " ^^^^^^\n" + "Missing nullable annotation: inherited method from X declares this parameter as @Nullable\n" + "----------\n" + - "3. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/p/X.java (at line 9)\n" + - " @Nullable Object foo(Object o, Object o2) { return null; }\n" + - " ^^^^^^\n" + - "Missing non-null annotation: inherited method from X declares this parameter as @NonNull\n" + - "----------\n" + - "3 problems (3 warnings)", + "2 problems (2 warnings)", true); } @@ -12682,12 +12678,7 @@ public void test314_warn_options() { " ^^^^^^\n" + "Missing nullable annotation: inherited method from X declares this parameter as @Nullable\n" + "----------\n" + - "3. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/p/X.java (at line 9)\n" + - " @Nullable Object foo(Object o, Object o2) { return null; }\n" + - " ^^^^^^\n" + - "Missing non-null annotation: inherited method from X declares this parameter as @NonNull\n" + - "----------\n" + - "3 problems (3 errors)", + "2 problems (2 errors)", true); } @@ -13673,12 +13664,7 @@ public void testBug375366c() throws IOException { " ^^^^^^\n" + "Missing nullable annotation: inherited method from X declares this parameter as @Nullable\n" + "----------\n" + - "3. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/p/X.java (at line 9)\n" + - " @Nullable Object foo(Object o, Object o2) { return null; }\n" + - " ^^^^^^\n" + - "Missing non-null annotation: inherited method from X declares this parameter as @NonNull\n" + - "----------\n" + - "3 problems (3 errors)", + "2 problems (2 errors)", false/*don't flush*/); } 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 4997d50..fc2e484 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 @@ -920,10 +920,10 @@ public void test_parameter_specification_inheritance_008() { options.put(JavaCore.COMPILER_PB_NULL_UNCHECKED_CONVERSION, JavaCore.ERROR); runConformTestWithLibs( new String[] { - "IX.java", + "X.java", "import org.eclipse.jdt.annotation.*;\n" + - "public interface IX {\n" + - " void printObject(@NonNull Object o);\n" + + "public class X {\n" + + " void printObject(@NonNull Object o) { System.out.print(o.toString()); }\n" + "}\n" }, null /*customOptions*/, @@ -931,26 +931,25 @@ public void test_parameter_specification_inheritance_008() { runNegativeTestWithLibs( false, // don't flush new String[] { - "X.java", - "public class X implements IX {\n" + - " public void printObject(Object o) { System.out.print(o.toString()); }\n" + + "XSub.java", + "public class XSub extends X {\n" + + " @Override\n" + + " public void printObject(Object o) { super.printObject(o); }\n" + "}\n", "M.java", "public class M{\n" + - " void foo(IX x, Object o) {\n" + + " void foo(X x, Object o) {\n" + " x.printObject(o);\n" + " }\n" + "}\n" }, options, "----------\n" + - // additional error: - "1. ERROR in X.java (at line 2)\n" + - " public void printObject(Object o) { System.out.print(o.toString()); }\n" + - " ^^^^^^\n" + - "Missing non-null annotation: inherited method from IX declares this parameter as @NonNull\n" + + "1. ERROR in XSub.java (at line 3)\n" + + " public void printObject(Object o) { super.printObject(o); }\n" + + " ^\n" + + "Null type safety: The expression of type Object needs unchecked conversion to conform to \'@NonNull Object\'\n" + "----------\n" + - // main error: "----------\n" + "1. ERROR in M.java (at line 3)\n" + " x.printObject(o);\n" + @@ -1114,18 +1113,14 @@ public void test_parameter_specification_inheritance_013() { " public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + " ^^^^^^^^^^^^^^^^\n" + "The return type is incompatible with the @NonNull return from IY.getString(String, String, String)\n" + + // no problem regarding s1: widening @NonNull to unannotated "----------\n" + "2. ERROR in p1\\Y.java (at line 5)\n" + " public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + - " ^^^^^^\n" + - "Missing non-null annotation: inherited method from IY declares this parameter as @NonNull\n" + - "----------\n" + - "3. ERROR in p1\\Y.java (at line 5)\n" + - " public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + " ^^^^^^^^^^^^^^^\n" + "Illegal redefinition of parameter s2, inherited method from X declares this parameter as @Nullable\n" + "----------\n" + - "4. ERROR in p1\\Y.java (at line 5)\n" + + "3. ERROR in p1\\Y.java (at line 5)\n" + " public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + " ^^^^^^^^^^^^^^^\n" + "Illegal redefinition of parameter s3, inherited method from IY declares this parameter as @Nullable\n" + @@ -1200,6 +1195,29 @@ public void test_parameter_specification_inheritance_014() { "The method getString3(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + "----------\n"); } +// a method relaxes the parameter null specification from @NonNull to un-annotated +// see https://bugs.eclipse.org/381443 +public void test_parameter_specification_inheritance_015() { + runConformTest( + new String[] { + "X.java", + "import org.eclipse.jdt.annotation.*;\n" + + "public class X {\n" + + " void foo(@NonNull String s) { System.out.println(s); }\n" + + "}\n", + "XSub.java", + "import org.eclipse.jdt.annotation.*;\n" + + "public class XSub extends X {\n" + + " public void foo(String s) { if (s != null) super.foo(s); }\n" + + " void bar() { foo(null); }\n" + + "}\n" + }, + "", + this.LIBS, + false/*shouldFlush*/, + null/*vmArgs*/); +} + // a nullable return value is dereferenced without a check public void test_nullable_return_001() { runNegativeTestWithLibs( diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ImplicitNullAnnotationVerifier.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ImplicitNullAnnotationVerifier.java index 908ad32..664455c 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ImplicitNullAnnotationVerifier.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ImplicitNullAnnotationVerifier.java @@ -319,10 +319,9 @@ public class ImplicitNullAnnotationVerifier { } else { scope.problemReporter().cannotImplementIncompatibleNullness(currentMethod, inheritedMethod); } - } else if (inheritedNonNullNess != null + } else if (inheritedNonNullNess == Boolean.FALSE // unannotated conflics only with inherited @Nullable && currentNonNullNess == null) { - // weak conflict (TODO reconsider this case) if (currentArgument != null) { scope.problemReporter().parameterLackingNullAnnotation( currentArgument, |

