Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 441338
Collapse All | Expand All

(-)a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java (+127 lines)
Lines 5425-5430 Link Here
5425
		"Null type mismatch (type annotations): required \'@NonNull Collection\' but this expression has type \'@Nullable Collection\'\n" + 
5425
		"Null type mismatch (type annotations): required \'@NonNull Collection\' but this expression has type \'@Nullable Collection\'\n" + 
5426
		"----------\n");
5426
		"----------\n");
5427
}
5427
}
5428
// top-level annotation is overridden at use-site, details remain - parameterized type
5429
public void testTypeVariable19() {
5430
	runNegativeTestWithLibs(
5431
		new String[] {
5432
			"X.java",
5433
			"import java.util.ArrayList;\n" + 
5434
			"import java.util.List;\n" + 
5435
			"\n" + 
5436
			"import org.eclipse.jdt.annotation.NonNull;\n" + 
5437
			"import org.eclipse.jdt.annotation.Nullable;\n" +
5438
			"interface I<T,U extends List<T>> {\n" +
5439
			"	U get0();\n" +
5440
			"	@Nullable U get1();\n" + 
5441
			"	@NonNull U get2();\n" + 
5442
			"}\n" + 
5443
			"class X {\n" + 
5444
			"	static String test (I<@Nullable String, @NonNull ArrayList<@Nullable String>> i1,\n" +
5445
			"						I<@NonNull String, @Nullable ArrayList<@NonNull String>> i2, int s) {\n" +
5446
			"		switch(s) {\n" +
5447
			"			case 0 : return i1.get0().get(0).toUpperCase(); // problem at detail\n" + 
5448
			"			case 1 : return i1.get1().get(0).toUpperCase(); // 2 problems\n" + 
5449
			"			case 2 : return i1.get2().get(0).toUpperCase(); // problem at detail\n" + 
5450
			"			case 3 : return i2.get0().get(0).toUpperCase(); // problem at top\n" + 
5451
			"			case 4 : return i2.get1().get(0).toUpperCase(); // problem at top\n" + 
5452
			"			case 5 : return i2.get2().get(0).toUpperCase(); // OK\n" +
5453
			"			default : return \"\";" +
5454
			"		}\n" + 
5455
			"	}\n" + 
5456
			"}\n"
5457
		},
5458
		getCompilerOptions(),
5459
		"----------\n" + 
5460
		"1. ERROR in X.java (at line 15)\n" + 
5461
		"	case 0 : return i1.get0().get(0).toUpperCase(); // problem at detail\n" + 
5462
		"	                ^^^^^^^^^^^^^^^^\n" + 
5463
		"Potential null pointer access: The method get(int) may return null\n" + 
5464
		"----------\n" + 
5465
		"2. ERROR in X.java (at line 16)\n" + 
5466
		"	case 1 : return i1.get1().get(0).toUpperCase(); // 2 problems\n" + 
5467
		"	                ^^^^^^^^^\n" + 
5468
		"Potential null pointer access: The method get1() may return null\n" + 
5469
		"----------\n" + 
5470
		"3. ERROR in X.java (at line 16)\n" + 
5471
		"	case 1 : return i1.get1().get(0).toUpperCase(); // 2 problems\n" + 
5472
		"	                ^^^^^^^^^^^^^^^^\n" + 
5473
		"Potential null pointer access: The method get(int) may return null\n" + 
5474
		"----------\n" + 
5475
		"4. ERROR in X.java (at line 17)\n" + 
5476
		"	case 2 : return i1.get2().get(0).toUpperCase(); // problem at detail\n" + 
5477
		"	                ^^^^^^^^^^^^^^^^\n" + 
5478
		"Potential null pointer access: The method get(int) may return null\n" + 
5479
		"----------\n" + 
5480
		"5. ERROR in X.java (at line 18)\n" + 
5481
		"	case 3 : return i2.get0().get(0).toUpperCase(); // problem at top\n" + 
5482
		"	                ^^^^^^^^^\n" + 
5483
		"Potential null pointer access: The method get0() may return null\n" + 
5484
		"----------\n" + 
5485
		"6. ERROR in X.java (at line 19)\n" + 
5486
		"	case 4 : return i2.get1().get(0).toUpperCase(); // problem at top\n" + 
5487
		"	                ^^^^^^^^^\n" + 
5488
		"Potential null pointer access: The method get1() may return null\n" + 
5489
		"----------\n");
5490
}
5491
// top-level annotation is overridden at use-site, array with anotations on dimensions
5492
public void testTypeVariable19a() {
5493
	runNegativeTestWithLibs(
5494
		new String[] {
5495
			"X.java",
5496
			"import org.eclipse.jdt.annotation.NonNull;\n" + 
5497
			"import org.eclipse.jdt.annotation.Nullable;\n" +
5498
			"interface I1<T> {\n" + 
5499
			"	T @Nullable[] get0();\n" + 
5500
			"	@Nullable T @NonNull[] get1();\n" + 
5501
			"	@Nullable T @Nullable[] get2();\n" + 
5502
			"}\n" + 
5503
			"interface I2<T> {\n" + 
5504
			"	T @NonNull[] get0();\n" + 
5505
			"	@NonNull T @NonNull[] get1();\n" + 
5506
			"	@NonNull T @Nullable[] get2();\n" + 
5507
			"}\n" + 
5508
			"class X {\n" + 
5509
			"	static String test (I1<@NonNull String> i1, I2<@Nullable String> i2, int s) {\n" +
5510
			"		switch (s) {\n" + 
5511
			"			case 0: return i1.get0()[0].toUpperCase(); // problem on array\n" + 
5512
			"			case 1: return i1.get1()[0].toUpperCase(); // problem on element\n" + 
5513
			"			case 2: return i1.get2()[0].toUpperCase(); // 2 problems\n" +
5514
			"			case 3: return i2.get0()[0].toUpperCase(); // problem on element\n" + 
5515
			"			case 4: return i2.get1()[0].toUpperCase(); // OK\n" + 
5516
			"			case 5: return i2.get2()[0].toUpperCase(); // problem on array\n" +
5517
			"			default: return \"\";\n" +
5518
			"		}\n" + 
5519
			"	}\n" + 
5520
			"}\n"
5521
		},
5522
		getCompilerOptions(),
5523
		"----------\n" + 
5524
		"1. ERROR in X.java (at line 16)\n" + 
5525
		"	case 0: return i1.get0()[0].toUpperCase(); // problem on array\n" + 
5526
		"	               ^^^^^^^^^\n" + 
5527
		"Potential null pointer access: The method get0() may return null\n" + 
5528
		"----------\n" + 
5529
		"2. ERROR in X.java (at line 17)\n" + 
5530
		"	case 1: return i1.get1()[0].toUpperCase(); // problem on element\n" + 
5531
		"	               ^^^^^^^^^^^^\n" + 
5532
		"Potential null pointer access: array element may be null\n" + 
5533
		"----------\n" + 
5534
		"3. ERROR in X.java (at line 18)\n" + 
5535
		"	case 2: return i1.get2()[0].toUpperCase(); // 2 problems\n" + 
5536
		"	               ^^^^^^^^^\n" + 
5537
		"Potential null pointer access: The method get2() may return null\n" + 
5538
		"----------\n" + 
5539
		"4. ERROR in X.java (at line 18)\n" + 
5540
		"	case 2: return i1.get2()[0].toUpperCase(); // 2 problems\n" + 
5541
		"	               ^^^^^^^^^^^^\n" + 
5542
		"Potential null pointer access: array element may be null\n" + 
5543
		"----------\n" + 
5544
		"5. ERROR in X.java (at line 19)\n" + 
5545
		"	case 3: return i2.get0()[0].toUpperCase(); // problem on element\n" + 
5546
		"	               ^^^^^^^^^^^^\n" + 
5547
		"Potential null pointer access: array element may be null\n" + 
5548
		"----------\n" + 
5549
		"6. ERROR in X.java (at line 21)\n" + 
5550
		"	case 5: return i2.get2()[0].toUpperCase(); // problem on array\n" + 
5551
		"	               ^^^^^^^^^\n" + 
5552
		"Potential null pointer access: The method get2() may return null\n" + 
5553
		"----------\n");
5554
}
5428
public void testBug434600() {
5555
public void testBug434600() {
5429
	runConformTestWithLibs(
5556
	runConformTestWithLibs(
5430
		new String[] {
5557
		new String[] {
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BoundSet.java (-2 / +2 lines)
Lines 242-248 Link Here
242
				if (annot != null) {
242
				if (annot != null) {
243
					// only get here if exactly one of @NonNull or @Nullable was hinted; now apply this hint:
243
					// only get here if exactly one of @NonNull or @Nullable was hinted; now apply this hint:
244
					for (int i = 0; i < boundTypes.length; i++)
244
					for (int i = 0; i < boundTypes.length; i++)
245
						boundTypes[i] = environment.createAnnotatedType(boundTypes[i], annot);
245
						boundTypes[i] = environment.createAnnotatedType(boundTypes[i].unannotated(true), annot);
246
				}
246
				}
247
			}
247
			}
248
		}
248
		}
Lines 270-276 Link Here
270
			AnnotationBinding[] annot = environment.nullAnnotationsFromTagBits(nullHints);
270
			AnnotationBinding[] annot = environment.nullAnnotationsFromTagBits(nullHints);
271
			if (annot != null)
271
			if (annot != null)
272
				// only get here if exactly one of @NonNull or @Nullable was hinted; now apply this hint:
272
				// only get here if exactly one of @NonNull or @Nullable was hinted; now apply this hint:
273
				return environment.createAnnotatedType(type, annot);
273
				return environment.createAnnotatedType(type.unannotated(true), annot);
274
			return type;
274
			return type;
275
		}
275
		}
276
		public void setInstantiation(TypeBinding type, InferenceVariable variable, LookupEnvironment environment) {
276
		public void setInstantiation(TypeBinding type, InferenceVariable variable, LookupEnvironment environment) {
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java (-11 / +1 lines)
Lines 883-899 Link Here
883
		if (removeOnlyNullAnnotations) {
883
		if (removeOnlyNullAnnotations) {
884
			ReferenceBinding unannotatedGenericType = (ReferenceBinding) this.environment.getUnannotatedType(this.type);
884
			ReferenceBinding unannotatedGenericType = (ReferenceBinding) this.environment.getUnannotatedType(this.type);
885
			AnnotationBinding[] newAnnotations = this.environment.filterNullTypeAnnotations(this.typeAnnotations);
885
			AnnotationBinding[] newAnnotations = this.environment.filterNullTypeAnnotations(this.typeAnnotations);
886
			TypeBinding[] newArguments = null;
886
			return this.environment.createParameterizedType(unannotatedGenericType, this.arguments	, this.enclosingType, newAnnotations);
887
			if (this.arguments != null) {
888
				newArguments = new TypeBinding[this.arguments.length];
889
				for (int i = 0; i < this.arguments.length; i++) {
890
					newArguments[i] = this.arguments[i].unannotated(removeOnlyNullAnnotations);
891
				}
892
			}
893
			ReferenceBinding newEnclosing = null;
894
			if (this.enclosingType != null)
895
				newEnclosing = (ReferenceBinding)this.enclosingType.unannotated(removeOnlyNullAnnotations);
896
			return this.environment.createParameterizedType(unannotatedGenericType, newArguments, newEnclosing, newAnnotations);
897
		}
887
		}
898
		return this.environment.getUnannotatedType(this);
888
		return this.environment.getUnannotatedType(this);
899
	}
889
	}
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/RawTypeBinding.java (-4 / +1 lines)
Lines 88-97 Link Here
88
		if (removeOnlyNullAnnotations) {
88
		if (removeOnlyNullAnnotations) {
89
			ReferenceBinding unannotatedGenericType = (ReferenceBinding) this.environment.getUnannotatedType(this.genericType());
89
			ReferenceBinding unannotatedGenericType = (ReferenceBinding) this.environment.getUnannotatedType(this.genericType());
90
			AnnotationBinding[] newAnnotations = this.environment.filterNullTypeAnnotations(this.typeAnnotations);
90
			AnnotationBinding[] newAnnotations = this.environment.filterNullTypeAnnotations(this.typeAnnotations);
91
			ReferenceBinding newEnclosing = null;
91
			return this.environment.createRawType(unannotatedGenericType, this.enclosingType(), newAnnotations);
92
			if (this.enclosingType() != null)
93
				newEnclosing = (ReferenceBinding)this.enclosingType().unannotated(removeOnlyNullAnnotations);
94
			return this.environment.createRawType(unannotatedGenericType, newEnclosing, newAnnotations);
95
		}
92
		}
96
		return this.environment.getUnannotatedType(this);
93
		return this.environment.getUnannotatedType(this);
97
	}
94
	}

Return to bug 441338