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 (+117 lines)
Lines 410-415 Link Here
410
		"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" +
410
		"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" +
411
		"----------\n"  /* compiler output */);
411
		"----------\n"  /* compiler output */);
412
}
412
}
413
// null is passed to a non-null parameter in a qualified allocation expression, across CUs
414
public void test_nonnull_parameter_010() {
415
	Map customOptions = getCompilerOptions();
416
	customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR);
417
	runNegativeTestWithLibs(
418
		new String[] {
419
			"ContainingInner2.java",
420
			"public class ContainingInner2 {\n" + 
421
			"    public ContainingInner2 (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + 
422
			"    }\n" + 
423
			"    public class Inner {\n" + 
424
			"        public Inner (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + 
425
			"        }\n" + 
426
			"    }\n" + 
427
			"}\n",
428
			"X.java",
429
			"public class X {\n" +
430
			"	 void create() {\n" +
431
			"          ContainingInner2 container = new ContainingInner2(null);\n" +
432
			"	       ContainingInner2.Inner inner = container.new Inner(null);\n" +
433
			"    }\n" +
434
		  	"}\n"},
435
		customOptions,
436
		"----------\n" + 
437
		"1. ERROR in X.java (at line 3)\n" + 
438
		"	ContainingInner2 container = new ContainingInner2(null);\n" + 
439
		"	                                                  ^^^^\n" + 
440
		"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
441
		"----------\n" + 
442
		"2. ERROR in X.java (at line 4)\n" + 
443
		"	ContainingInner2.Inner inner = container.new Inner(null);\n" + 
444
		"	                                                   ^^^^\n" + 
445
		"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
446
		"----------\n"  /* compiler output */);
447
}
448
// null is passed to a non-null parameter in a qualified allocation expression, target class read from .class
449
public void test_nonnull_parameter_011() {
450
	Map customOptions = getCompilerOptions();
451
	customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR);
452
	runConformTestWithLibs(
453
			new String[] {
454
				"ContainingInner2.java",
455
				"public class ContainingInner2 {\n" + 
456
				"    public ContainingInner2 (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + 
457
				"    }\n" + 
458
				"    public class Inner {\n" + 
459
				"        public Inner (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + 
460
				"        }\n" + 
461
				"    }\n" + 
462
				"}\n",
463
			},
464
			null /*customOptions*/,
465
			"");
466
	runNegativeTestWithLibs(
467
		false, // flush directory
468
		new String[] {
469
			"X.java",
470
			"public class X {\n" +
471
			"	 void create() {\n" +
472
			"          ContainingInner2 container = new ContainingInner2(null);\n" +
473
			"	       ContainingInner2.Inner inner = container.new Inner(null);\n" +
474
			"    }\n" +
475
		  	"}\n"},
476
		customOptions,
477
		"----------\n" + 
478
		"1. ERROR in X.java (at line 3)\n" + 
479
		"	ContainingInner2 container = new ContainingInner2(null);\n" + 
480
		"	                                                  ^^^^\n" + 
481
		"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
482
		"----------\n" + 
483
		"2. ERROR in X.java (at line 4)\n" + 
484
		"	ContainingInner2.Inner inner = container.new Inner(null);\n" + 
485
		"	                                                   ^^^^\n" + 
486
		"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
487
		"----------\n"  /* compiler output */);
488
}
489
//null is passed to a non-null parameter in a qualified allocation expression, generic constructor, target class read from .class
490
public void test_nonnull_parameter_012() {
491
	Map customOptions = getCompilerOptions();
492
	customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR);
493
	runConformTestWithLibs(
494
			new String[] {
495
				"ContainingInner2.java",
496
				"public class ContainingInner2 {\n" + 
497
				"    public ContainingInner2 (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + 
498
				"    }\n" + 
499
				"    public class Inner {\n" + 
500
				"        public <T> Inner (@org.eclipse.jdt.annotation.NonNull T o) {\n" + 
501
				"        }\n" + 
502
				"    }\n" + 
503
				"}\n",
504
			},
505
			null /*customOptions*/,
506
			"");
507
	runNegativeTestWithLibs(
508
		false, // flush directory
509
		new String[] {
510
			"X.java",
511
			"public class X {\n" +
512
			"	 void create() {\n" +
513
			"          ContainingInner2 container = new ContainingInner2(null);\n" +
514
			"	       ContainingInner2.Inner inner = container.new Inner(null);\n" +
515
			"    }\n" +
516
		  	"}\n"},
517
		customOptions,
518
		"----------\n" + 
519
		"1. ERROR in X.java (at line 3)\n" + 
520
		"	ContainingInner2 container = new ContainingInner2(null);\n" + 
521
		"	                                                  ^^^^\n" + 
522
		"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
523
		"----------\n" + 
524
		"2. ERROR in X.java (at line 4)\n" + 
525
		"	ContainingInner2.Inner inner = container.new Inner(null);\n" + 
526
		"	                                                   ^^^^\n" + 
527
		"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + 
528
		"----------\n"  /* compiler output */);
529
}
413
// assigning potential null to a nonnull local variable
530
// assigning potential null to a nonnull local variable
414
public void test_nonnull_local_001() {
531
public void test_nonnull_local_001() {
415
	runNegativeTest(
532
	runNegativeTest(
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/classfmt/MethodInfo.java (-1 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 186342 - [compiler][null] Using annotations for null checking
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.classfmt;
12
package org.eclipse.jdt.internal.compiler.classfmt;
12
13
Lines 270-275 Link Here
270
public IBinaryAnnotation[] getParameterAnnotations(int index) {
271
public IBinaryAnnotation[] getParameterAnnotations(int index) {
271
	return null;
272
	return null;
272
}
273
}
274
public int getNumParameterAnnotations() {
275
	return 0;
276
}
273
/**
277
/**
274
 * Answer the name of the method.
278
 * Answer the name of the method.
275
 *
279
 *
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/classfmt/MethodInfoWithParameterAnnotations.java (+4 lines)
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *    tyeung@bea.com  - initial API and implementation
9
 *    tyeung@bea.com  - initial API and implementation
10
 *    IBM Corporation - fix for bug 342757
10
 *    IBM Corporation - fix for bug 342757
11
 *    Stephan Herrmann - Contribution for bug 186342 - [compiler][null] Using annotations for null checking
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.classfmt;
13
package org.eclipse.jdt.internal.compiler.classfmt;
13
14
Lines 36-41 Link Here
36
public IBinaryAnnotation[] getParameterAnnotations(int index) {
37
public IBinaryAnnotation[] getParameterAnnotations(int index) {
37
	return this.parameterAnnotations[index];
38
	return this.parameterAnnotations[index];
38
}
39
}
40
public int getNumParameterAnnotations() {
41
	return this.parameterAnnotations == null ? 0 : this.parameterAnnotations.length;
42
}
39
protected void initialize() {
43
protected void initialize() {
40
	for (int i = 0, l = this.parameterAnnotations == null ? 0 : this.parameterAnnotations.length; i < l; i++) {
44
	for (int i = 0, l = this.parameterAnnotations == null ? 0 : this.parameterAnnotations.length; i < l; i++) {
41
		AnnotationInfo[] infos = this.parameterAnnotations[i];
45
		AnnotationInfo[] infos = this.parameterAnnotations[i];
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/env/IBinaryMethod.java (-1 / +9 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 186342 - [compiler][null] Using annotations for null checking
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.env;
12
package org.eclipse.jdt.internal.compiler.env;
12
13
Lines 66-71 Link Here
66
IBinaryAnnotation[] getParameterAnnotations(int index);
67
IBinaryAnnotation[] getParameterAnnotations(int index);
67
68
68
/**
69
/**
70
 * Answer the number of parameter annotations that can be retrieved
71
 * using {@link #getParameterAnnotations(int)}.
72
 * @return one beyond the highest legal argument to {@link #getParameterAnnotations(int)}.
73
 */
74
int getNumParameterAnnotations();
75
76
/**
69
 * Answer the name of the method.
77
 * Answer the name of the method.
70
 *
78
 *
71
 * For a constructor, answer <init> & <clinit> for a clinit method.
79
 * For a constructor, answer <init> & <clinit> for a clinit method.
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java (-4 / +6 lines)
Lines 1178-1185 Link Here
1178
1178
1179
	// parameters:
1179
	// parameters:
1180
	TypeBinding[] parameters = methodBinding.parameters;
1180
	TypeBinding[] parameters = methodBinding.parameters;
1181
	for (int j = 0; j < parameters.length; j++) {
1181
	int numVisibleParams = parameters.length;
1182
		IBinaryAnnotation[] paramAnnotations = method.getParameterAnnotations(j);
1182
	int startIndex = method.getNumParameterAnnotations() - numVisibleParams;
1183
	for (int j = 0; j < numVisibleParams; j++) {
1184
		IBinaryAnnotation[] paramAnnotations = method.getParameterAnnotations(j+startIndex);
1183
		if (paramAnnotations != null) {
1185
		if (paramAnnotations != null) {
1184
			for (int i = 0; i < paramAnnotations.length; i++) {
1186
			for (int i = 0; i < paramAnnotations.length; i++) {
1185
				char[] annotationTypeName = paramAnnotations[i].getTypeName();
1187
				char[] annotationTypeName = paramAnnotations[i].getTypeName();
Lines 1188-1199 Link Here
1188
				char[][] typeName = CharOperation.splitOn('/', annotationTypeName, 1, annotationTypeName.length-1); // cut of leading 'L' and trailing ';'
1190
				char[][] typeName = CharOperation.splitOn('/', annotationTypeName, 1, annotationTypeName.length-1); // cut of leading 'L' and trailing ';'
1189
				if (CharOperation.equals(typeName, nonNullAnnotationName)) {
1191
				if (CharOperation.equals(typeName, nonNullAnnotationName)) {
1190
					if (methodBinding.parameterNonNullness == null)
1192
					if (methodBinding.parameterNonNullness == null)
1191
						methodBinding.parameterNonNullness = new Boolean[parameters.length];
1193
						methodBinding.parameterNonNullness = new Boolean[numVisibleParams];
1192
					methodBinding.parameterNonNullness[j] = Boolean.TRUE;
1194
					methodBinding.parameterNonNullness[j] = Boolean.TRUE;
1193
					break;
1195
					break;
1194
				} else if (CharOperation.equals(typeName, nullableAnnotationName)) {
1196
				} else if (CharOperation.equals(typeName, nullableAnnotationName)) {
1195
					if (methodBinding.parameterNonNullness == null)
1197
					if (methodBinding.parameterNonNullness == null)
1196
						methodBinding.parameterNonNullness = new Boolean[parameters.length];
1198
						methodBinding.parameterNonNullness = new Boolean[numVisibleParams];
1197
					methodBinding.parameterNonNullness[j] = Boolean.FALSE;
1199
					methodBinding.parameterNonNullness[j] = Boolean.FALSE;
1198
					break;
1200
					break;
1199
				}
1201
				}
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MostSpecificExceptionMethodBinding.java (-1 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 186342 - [compiler][null] Using annotations for null checking
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 27-32 Link Here
27
				mostSpecificExceptions, 
28
				mostSpecificExceptions, 
28
				originalMethod.declaringClass);
29
				originalMethod.declaringClass);
29
		this.originalMethod = originalMethod;
30
		this.originalMethod = originalMethod;
31
		this.parameterNonNullness = originalMethod.parameterNonNullness;
30
	}
32
	}
31
	
33
	
32
	public MethodBinding original() {
34
	public MethodBinding original() {
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedGenericMethodBinding.java (+3 lines)
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 186342 - [compiler][null] Using annotations for null checking
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 301-306 Link Here
301
	    									? originalMethod.returnType // no substitution if original was static
302
	    									? originalMethod.returnType // no substitution if original was static
302
	    									: Scope.substitute(rawType, originalMethod.returnType));
303
	    									: Scope.substitute(rawType, originalMethod.returnType));
303
	    this.wasInferred = false; // not resulting from method invocation inferrence
304
	    this.wasInferred = false; // not resulting from method invocation inferrence
305
	    this.parameterNonNullness = originalMethod.parameterNonNullness;
304
	}
306
	}
305
307
306
    /**
308
    /**
Lines 342-347 Link Here
342
			}
344
			}
343
		}
345
		}
344
	    this.wasInferred = true;// resulting from method invocation inferrence
346
	    this.wasInferred = true;// resulting from method invocation inferrence
347
	    this.parameterNonNullness = originalMethod.parameterNonNullness;
345
	}
348
	}
346
349
347
	/*
350
	/*
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedMethodBinding.java (-1 / +4 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 186342 - [compiler][null] Using annotations for null checking
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 38-43 Link Here
38
		 * is substituted by a raw type.
39
		 * is substituted by a raw type.
39
		 */
40
		 */
40
		this.tagBits = originalMethod.tagBits & ~TagBits.HasMissingType;
41
		this.tagBits = originalMethod.tagBits & ~TagBits.HasMissingType;
42
		this.parameterNonNullness = originalMethod.parameterNonNullness;
41
43
42
		final TypeVariableBinding[] originalVariables = originalMethod.typeVariables;
44
		final TypeVariableBinding[] originalVariables = originalMethod.typeVariables;
43
		Substitution substitution = null;
45
		Substitution substitution = null;
Lines 150-155 Link Here
150
		 * is substituted by a raw type.
152
		 * is substituted by a raw type.
151
		 */
153
		 */
152
		this.tagBits = originalMethod.tagBits & ~TagBits.HasMissingType;
154
		this.tagBits = originalMethod.tagBits & ~TagBits.HasMissingType;
155
		this.parameterNonNullness = originalMethod.parameterNonNullness;
153
156
154
		final TypeVariableBinding[] originalVariables = originalMethod.typeVariables;
157
		final TypeVariableBinding[] originalVariables = originalMethod.typeVariables;
155
		Substitution substitution = null;
158
		Substitution substitution = null;
(-)a/org.eclipse.jdt.core/eval/org/eclipse/jdt/internal/eval/CodeSnippetSkeleton.java (-1 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 186342 - [compiler][null] Using annotations for null checking
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.eval;
12
package org.eclipse.jdt.internal.eval;
12
13
Lines 64-69 Link Here
64
		public IBinaryAnnotation[] getParameterAnnotations(int index) {
65
		public IBinaryAnnotation[] getParameterAnnotations(int index) {
65
			return null;
66
			return null;
66
		}
67
		}
68
		public int getNumParameterAnnotations() {
69
			return 0;
70
		}
67
		public char[] getSelector() {
71
		public char[] getSelector() {
68
			return this.selector;
72
			return this.selector;
69
		}
73
		}

Return to bug 186342