View | Details | Raw Unified | Return to bug 186342 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java (+99 lines)
Lines 1972-1977 Link Here
1972
		"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" +
1972
		"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" +
1973
		"----------\n");
1973
		"----------\n");
1974
}
1974
}
1975
//same as test_default_nullness_003b, but default-induced annotations are combined with explicit ones (not null related)
1976
public void test_default_nullness_003b() {
1977
	Map customOptions = getCompilerOptions();
1978
	runConformTestWithLibs(
1979
		new String[] {
1980
	"p1/Annot.java",
1981
			"package p1;\n" +
1982
			"import static java.lang.annotation.ElementType.*;\n" +
1983
			"import java.lang.annotation.*;\n" +
1984
			"@Retention(RetentionPolicy.CLASS)\n" +
1985
			"@Target({METHOD,PARAMETER})\n" +
1986
			"public @interface Annot {}\n",
1987
	"p1/X.java",
1988
			"package p1;\n" +
1989
			"import org.eclipse.jdt.annotation.*;\n" +
1990
			"@NonNullByDefault\n" +
1991
			"public class X {\n" +
1992
			"    protected @Annot Object getObject(@Annot @Nullable Object o) {\n" +
1993
			"        return new Object();\n" +
1994
			"    }\n" +
1995
			"	 protected @Annot void bar(@Annot Object o2) { }\n" + // parameter is nonnull per type default
1996
			"}\n",
1997
	"p2/package-info.java",
1998
			"@org.eclipse.jdt.annotation.NonNullByDefault\n" +
1999
			"package p2;\n",
2000
			},
2001
			customOptions,
2002
			"");
2003
	// check if default is visible from package-info.class.
2004
	runNegativeTestWithLibs(
2005
		false, // don't flush
2006
		new String[] {
2007
	"p2/Y.java",
2008
			"package p2;\n" +
2009
			"import org.eclipse.jdt.annotation.*;\n" +
2010
			"public class Y extends p1.X {\n" +
2011
			"    @Override\n" +
2012
			"    protected @Nullable Object getObject(@Nullable Object o) {\n" + // can't override inherited default nonnull
2013
			"        bar(o);\n" + // parameter is nonnull in super class's .class file
2014
			"        accept(o);\n" +
2015
			"        return o;\n" +
2016
			"    }\n" +
2017
			"    void accept(@p1.Annot Object a) {}\n" + // governed by package level default
2018
			"}\n"
2019
		},
2020
		customOptions,
2021
		"----------\n" +
2022
		"1. ERROR in p2\\Y.java (at line 5)\n" +
2023
		"	protected @Nullable Object getObject(@Nullable Object o) {\n" +
2024
		"	          ^^^^^^^^^^^^^^^^\n" +
2025
		"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" +
2026
		"----------\n" +
2027
		"2. ERROR in p2\\Y.java (at line 6)\n" +
2028
		"	bar(o);\n" +
2029
		"	    ^\n" +
2030
		"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" +
2031
		"----------\n" +
2032
		"3. ERROR in p2\\Y.java (at line 7)\n" +
2033
		"	accept(o);\n" +
2034
		"	       ^\n" +
2035
		"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" +
2036
		"----------\n");
2037
}
1975
// don't apply type-level default to non-reference type
2038
// don't apply type-level default to non-reference type
1976
public void test_default_nullness_004() {
2039
public void test_default_nullness_004() {
1977
	Map customOptions = getCompilerOptions();
2040
	Map customOptions = getCompilerOptions();
Lines 2193-2198 Link Here
2193
		"The nullness annotation is redundant with a default that applies to this location\n" +
2256
		"The nullness annotation is redundant with a default that applies to this location\n" +
2194
		"----------\n");
2257
		"----------\n");
2195
}
2258
}
2259
// package-info declares nonnull-by-default
2260
// special compile order due to import of type from that package
2261
// cf. https://bugs.eclipse.org/bugs/show_bug.cgi?id=186342#add_comment
2262
public void test_default_nullness_011() {
2263
	runNegativeTestWithLibs(
2264
		new String[] {
2265
			"Main.java",
2266
			"import p1.C;\n" +
2267
			"public class Main {\n" +
2268
			"    void test(@org.eclipse.jdt.annotation.NonNull Object o) {\n" +
2269
			"        o = null;\n" +
2270
			"        new C(null);\n" +
2271
			"    }\n" +
2272
			"}\n",
2273
			"p1/C.java",
2274
			"package p1;\n" +
2275
			"@org.eclipse.jdt.annotation.NonNullByDefault\n" +
2276
			"public class C {\n" +
2277
			"    public C (Object o) {}\n" +
2278
			"}\n",
2279
			"p1/package-info.java",
2280
			"@org.eclipse.jdt.annotation.NonNullByDefault\n" +
2281
			"package p1;\n"
2282
		},
2283
		"----------\n" + 
2284
		"1. ERROR in Main.java (at line 4)\n" + 
2285
		"	o = null;\n" + 
2286
		"	    ^^^^\n" + 
2287
		"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
2288
		"----------\n" + 
2289
		"2. ERROR in Main.java (at line 5)\n" + 
2290
		"	new C(null);\n" + 
2291
		"	      ^^^^\n" + 
2292
		"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
2293
		"----------\n");
2294
}
2196
// a nonnull variable is dereferenced in a loop
2295
// a nonnull variable is dereferenced in a loop
2197
public void test_nonnull_var_in_constrol_structure_1() {
2296
public void test_nonnull_var_in_constrol_structure_1() {
2198
	Map customOptions = getCompilerOptions();
2297
	Map customOptions = getCompilerOptions();
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java (-2 lines)
Lines 1133-1141 Link Here
1133
	boolean tolerateMissing = this.mayTolerateMissingType;
1133
	boolean tolerateMissing = this.mayTolerateMissingType;
1134
	this.mayTolerateMissingType = true;
1134
	this.mayTolerateMissingType = true;
1135
	try {
1135
	try {
1136
		int id = nullAnnotation.id;
1137
		nullAnnotation = BinaryTypeBinding.resolveType(nullAnnotation, this, false);
1136
		nullAnnotation = BinaryTypeBinding.resolveType(nullAnnotation, this, false);
1138
		nullAnnotation.id = id;
1139
	} finally {
1137
	} finally {
1140
		this.mayTolerateMissingType = tolerateMissing;
1138
		this.mayTolerateMissingType = tolerateMissing;
1141
	}
1139
	}
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/PackageBinding.java (-3 / +6 lines)
Lines 267-281 Link Here
267
	if (this.environment.nullableAnnotationPackage == this
267
	if (this.environment.nullableAnnotationPackage == this
268
			&& CharOperation.equals(type.compoundName, this.environment.getNullableAnnotationName())) {
268
			&& CharOperation.equals(type.compoundName, this.environment.getNullableAnnotationName())) {
269
		type.id = TypeIds.T_ConfiguredAnnotationNullable;
269
		type.id = TypeIds.T_ConfiguredAnnotationNullable;
270
		this.environment.nullableAnnotationPackage = null; // don't check again
270
		if (!(type instanceof UnresolvedReferenceBinding)) // unresolved will need to check back for the resolved type
271
			this.environment.nullableAnnotationPackage = null; // don't check again
271
	} else if (this.environment.nonnullAnnotationPackage == this
272
	} else if (this.environment.nonnullAnnotationPackage == this
272
			&& CharOperation.equals(type.compoundName, this.environment.getNonNullAnnotationName())) {
273
			&& CharOperation.equals(type.compoundName, this.environment.getNonNullAnnotationName())) {
273
		type.id = TypeIds.T_ConfiguredAnnotationNonNull;
274
		type.id = TypeIds.T_ConfiguredAnnotationNonNull;
274
		this.environment.nonnullAnnotationPackage = null; // don't check again
275
		if (!(type instanceof UnresolvedReferenceBinding)) // unresolved will need to check back for the resolved type
276
			this.environment.nonnullAnnotationPackage = null; // don't check again
275
	} else if (this.environment.nonnullByDefaultAnnotationPackage == this
277
	} else if (this.environment.nonnullByDefaultAnnotationPackage == this
276
			&& CharOperation.equals(type.compoundName, this.environment.getNonNullByDefaultAnnotationName())) {
278
			&& CharOperation.equals(type.compoundName, this.environment.getNonNullByDefaultAnnotationName())) {
277
		type.id = TypeIds.T_ConfiguredAnnotationNonNullByDefault;
279
		type.id = TypeIds.T_ConfiguredAnnotationNonNullByDefault;
278
		this.environment.nonnullByDefaultAnnotationPackage = null; // don't check again
280
		if (!(type instanceof UnresolvedReferenceBinding)) // unresolved will need to check back for the resolved type
281
			this.environment.nonnullByDefaultAnnotationPackage = null; // don't check again
279
	}
282
	}
280
}
283
}
281
284

Return to bug 186342