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 385b9c2..0a8af6b 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 @@ -655,6 +655,55 @@ this.LIBS, true /* shouldFlush*/); } +// BugĀ 367203 - [compiler][null] detect assigning null to nonnull argument +public void test_nonnull_argument_001() { + runNegativeTestWithLibs( + new String[] { + "ShowNPE2.java", + "import org.eclipse.jdt.annotation.NonNullByDefault;\n" + + "@NonNullByDefault\n" + + "public class ShowNPE2 {\n" + + " public Object foo(Object o1, final boolean b) {\n" + + " o1 = null; // expect NPE error\n" + + " System.out.println(o1.toString()); \n" + + " return null; // expect NPE error\n" + + " }\n" + + "}" + }, + "----------\n" + + "1. ERROR in ShowNPE2.java (at line 5)\n" + + " o1 = null; // expect NPE error\n" + + " ^^^^\n" + + "Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + + "----------\n" + + "2. ERROR in ShowNPE2.java (at line 7)\n" + + " return null; // expect NPE error\n" + + " ^^^^\n" + + "Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + + "----------\n"); +} +// BugĀ 367203 - [compiler][null] detect assigning null to nonnull argument +public void test_nonnull_argument_002() { + runNegativeTestWithLibs( + new String[] { + "ShowNPE2.java", + "import org.eclipse.jdt.annotation.NonNullByDefault;\n" + + "@NonNullByDefault\n" + + "public class ShowNPE2 {\n" + + " public Object foo(Object o1, final boolean b) {\n" + + " bar(o1); // expecting no problem\n" + + " return null; // expect NPE error\n" + + " }\n" + + " void bar(Object o2) {}\n" + + "}" + }, + "----------\n" + + "1. ERROR in ShowNPE2.java (at line 6)\n" + + " return null; // expect NPE error\n" + + " ^^^^\n" + + "Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + + "----------\n"); +} // assigning potential null to a nonnull local variable public void test_nonnull_local_001() { runNegativeTest( diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java index f6d98fd..e00530b 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java @@ -83,8 +83,7 @@ * Materialize a null parameter annotation that has been added from the current default, * in order to ensure that this annotation will be generated into the .class file, too. */ - public void addParameterNonNullAnnotation(int i, ReferenceBinding annotationBinding) { - Argument argument = this.arguments[i]; + public void addParameterNonNullAnnotation(Argument argument, ReferenceBinding annotationBinding) { if (argument.type != null) // null happens for constructors of anonymous classes argument.annotations = addAnnotation(argument.type, argument.annotations, annotationBinding); } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java index 0e1140d..34d41d5 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java @@ -458,8 +458,11 @@ if (this.parameterNonNullness[i] == null) { added = true; this.parameterNonNullness[i] = Boolean.TRUE; - if (sourceMethod != null) - sourceMethod.addParameterNonNullAnnotation(i, (ReferenceBinding)annotationBinding); + if (sourceMethod != null) { + Argument argument = sourceMethod.arguments[i]; + sourceMethod.addParameterNonNullAnnotation(argument, (ReferenceBinding)annotationBinding); + argument.binding.tagBits |= TagBits.AnnotationNonNull; + } } else if (this.parameterNonNullness[i].booleanValue()) { sourceMethod.scope.problemReporter().nullAnnotationIsRedundant(sourceMethod, i); }