View | Details | Raw Unified | Return to bug 367203
Collapse All | Expand All

(-)a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java (+49 lines)
Lines 655-660 Link Here
655
		this.LIBS,
655
		this.LIBS,
656
		true /* shouldFlush*/);
656
		true /* shouldFlush*/);
657
}
657
}
658
// BugĀ 367203 - [compiler][null] detect assigning null to nonnull argument
659
public void test_nonnull_argument_001() {
660
	runNegativeTestWithLibs(
661
			new String[] {
662
				"ShowNPE2.java",
663
				"import org.eclipse.jdt.annotation.NonNullByDefault;\n" + 
664
				"@NonNullByDefault\n" + 
665
				"public class ShowNPE2 {\n" + 
666
				"     public Object foo(Object o1, final boolean b) {\n" + 
667
				"         o1 = null;   // expect NPE error\n" + 
668
				"         System.out.println(o1.toString());   \n" + 
669
				"         return null;  // expect NPE error\n" + 
670
				"    }\n" + 
671
				"}"
672
			},
673
			"----------\n" + 
674
			"1. ERROR in ShowNPE2.java (at line 5)\n" + 
675
			"	o1 = null;   // expect NPE error\n" + 
676
			"	     ^^^^\n" + 
677
			"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
678
			"----------\n" + 
679
			"2. ERROR in ShowNPE2.java (at line 7)\n" + 
680
			"	return null;  // expect NPE error\n" + 
681
			"	       ^^^^\n" + 
682
			"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
683
			"----------\n");
684
}
685
// BugĀ 367203 - [compiler][null] detect assigning null to nonnull argument
686
public void test_nonnull_argument_002() {
687
	runNegativeTestWithLibs(
688
			new String[] {
689
				"ShowNPE2.java",
690
				"import org.eclipse.jdt.annotation.NonNullByDefault;\n" + 
691
				"@NonNullByDefault\n" + 
692
				"public class ShowNPE2 {\n" + 
693
				"    public Object foo(Object o1, final boolean b) {\n" + 
694
				"        bar(o1); // expecting no problem\n" + 
695
				"        return null;  // expect NPE error\n" + 
696
				"    }\n" +
697
				"    void bar(Object o2) {}\n" + 
698
				"}"
699
			},
700
			"----------\n" + 
701
			"1. ERROR in ShowNPE2.java (at line 6)\n" + 
702
			"	return null;  // expect NPE error\n" + 
703
			"	       ^^^^\n" + 
704
			"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
705
			"----------\n");
706
}
658
// assigning potential null to a nonnull local variable
707
// assigning potential null to a nonnull local variable
659
public void test_nonnull_local_001() {
708
public void test_nonnull_local_001() {
660
	runNegativeTest(
709
	runNegativeTest(
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java (-2 / +1 lines)
Lines 83-90 Link Here
83
	 * Materialize a null parameter annotation that has been added from the current default,
83
	 * Materialize a null parameter annotation that has been added from the current default,
84
	 * in order to ensure that this annotation will be generated into the .class file, too.
84
	 * in order to ensure that this annotation will be generated into the .class file, too.
85
	 */
85
	 */
86
	public void addParameterNonNullAnnotation(int i, ReferenceBinding annotationBinding) {
86
	public void addParameterNonNullAnnotation(Argument argument, ReferenceBinding annotationBinding) {
87
		Argument argument = this.arguments[i];
88
		if (argument.type != null) // null happens for constructors of anonymous classes
87
		if (argument.type != null) // null happens for constructors of anonymous classes
89
			argument.annotations = addAnnotation(argument.type, argument.annotations, annotationBinding);
88
			argument.annotations = addAnnotation(argument.type, argument.annotations, annotationBinding);
90
	}
89
	}
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java (-2 / +5 lines)
Lines 458-465 Link Here
458
		if (this.parameterNonNullness[i] == null) {
458
		if (this.parameterNonNullness[i] == null) {
459
			added = true;
459
			added = true;
460
			this.parameterNonNullness[i] = Boolean.TRUE;
460
			this.parameterNonNullness[i] = Boolean.TRUE;
461
			if (sourceMethod != null)
461
			if (sourceMethod != null) {
462
				sourceMethod.addParameterNonNullAnnotation(i, (ReferenceBinding)annotationBinding);
462
				Argument argument = sourceMethod.arguments[i];
463
				sourceMethod.addParameterNonNullAnnotation(argument, (ReferenceBinding)annotationBinding);
464
				argument.binding.tagBits |= TagBits.AnnotationNonNull;
465
			}
463
		} else if (this.parameterNonNullness[i].booleanValue()) {
466
		} else if (this.parameterNonNullness[i].booleanValue()) {
464
			sourceMethod.scope.problemReporter().nullAnnotationIsRedundant(sourceMethod, i);
467
			sourceMethod.scope.problemReporter().nullAnnotationIsRedundant(sourceMethod, i);
465
		}
468
		}

Return to bug 367203