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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/flow/FlowContext.java (-1 / +37 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 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 556-567 Link Here
556
		case CAN_ONLY_NULL | IN_COMPARISON_NON_NULL:
556
		case CAN_ONLY_NULL | IN_COMPARISON_NON_NULL:
557
		case CAN_ONLY_NULL | IN_ASSIGNMENT:
557
		case CAN_ONLY_NULL | IN_ASSIGNMENT:
558
		case CAN_ONLY_NULL | IN_INSTANCEOF:
558
		case CAN_ONLY_NULL | IN_INSTANCEOF:
559
			TypeBinding compileTimeType = reference.resolvedType;
559
			if (flowInfo.isDefinitelyNull(local)) {
560
			if (flowInfo.isDefinitelyNull(local)) {
560
				switch(checkType & CONTEXT_MASK) {
561
				switch(checkType & CONTEXT_MASK) {
561
					case FlowContext.IN_COMPARISON_NULL:
562
					case FlowContext.IN_COMPARISON_NULL:
563
						if (!compileTimeType.isBaseType() && ((checkType & CHECK_MASK) == CAN_ONLY_NULL)) { // check for auto-unboxing first and report appropriate warning
564
							TypeBinding unboxedType = scope.environment().computeBoxingType(compileTimeType);
565
							if (unboxedType != null && unboxedType != compileTimeType) {
566
								scope.problemReporter().localVariableNullReference(local, reference);
567
								return;
568
							}
569
						}
562
						scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
570
						scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
563
						return;
571
						return;
564
					case FlowContext.IN_COMPARISON_NON_NULL:
572
					case FlowContext.IN_COMPARISON_NON_NULL:
573
						if (!compileTimeType.isBaseType()  && ((checkType & CHECK_MASK) == CAN_ONLY_NULL)) { // check for auto-unboxing first and report appropriate warning
574
							TypeBinding unboxedType = scope.environment().computeBoxingType(compileTimeType);
575
							if (unboxedType != null && unboxedType != compileTimeType) {
576
								scope.problemReporter().localVariableNullReference(local, reference);
577
								return;
578
							}
579
						}
565
						scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
580
						scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
566
						return;
581
						return;
567
					case FlowContext.IN_ASSIGNMENT:
582
					case FlowContext.IN_ASSIGNMENT:
Lines 571-576 Link Here
571
						scope.problemReporter().localVariableNullInstanceof(local, reference);
586
						scope.problemReporter().localVariableNullInstanceof(local, reference);
572
						return;
587
						return;
573
				}
588
				}
589
			} else if (flowInfo.isPotentiallyNull(local)) {
590
				switch(checkType & CONTEXT_MASK) {
591
					case FlowContext.IN_COMPARISON_NULL:
592
						if (!compileTimeType.isBaseType()  && ((checkType & CHECK_MASK) == CAN_ONLY_NULL)) { // check for auto-unboxing first and report appropriate warning
593
							TypeBinding unboxedType = scope.environment().computeBoxingType(compileTimeType);
594
							if (unboxedType != null && unboxedType != compileTimeType) {
595
								scope.problemReporter().localVariablePotentialNullReference(local, reference);
596
								return;
597
							}
598
						}
599
						break;
600
					case FlowContext.IN_COMPARISON_NON_NULL:
601
						if (!compileTimeType.isBaseType()  && ((checkType & CHECK_MASK) == CAN_ONLY_NULL)) { // check for auto-unboxing first and report appropriate warning
602
							TypeBinding unboxedType = scope.environment().computeBoxingType(compileTimeType);
603
							if (unboxedType != null && unboxedType != compileTimeType) {
604
								scope.problemReporter().localVariablePotentialNullReference(local, reference);
605
								return;
606
							}
607
						}
608
						break;
609
					}
574
			} else if (flowInfo.cannotBeDefinitelyNullOrNonNull(local)) {
610
			} else if (flowInfo.cannotBeDefinitelyNullOrNonNull(local)) {
575
				return;
611
				return;
576
			}
612
			}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (-2 / +80 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2009 IBM Corporation and others.
2
 * Copyright (c) 2005, 2010 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 12-20 Link Here
12
12
13
import java.util.Map;
13
import java.util.Map;
14
14
15
import junit.framework.Test;
16
15
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
17
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
16
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
18
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
17
import junit.framework.Test;
18
19
19
/* See also NullReferenceImplTests for low level, implementation dependent
20
/* See also NullReferenceImplTests for low level, implementation dependent
20
 * tests. */
21
 * tests. */
Lines 10328-10331 Link Here
10328
				"----------\n");
10329
				"----------\n");
10329
	}
10330
	}
10330
}
10331
}
10332
10333
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=253896
10334
// Test whether Null pointer access warnings are being reported correctly when auto-unboxing
10335
public void testBug253896a() {
10336
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
10337
		this.runNegativeTest(
10338
				new String[] {
10339
						"X.java",
10340
						"public class X {\n" +
10341
						"  public void foo() {\n" +
10342
						"    Integer f1 = null;\n" +
10343
						"	 if(f1 == 1)\n" +
10344
						" 	 	System.out.println(\"f1 is 1\");\n" +
10345
						"    Integer f2 = null;\n" +
10346
						"	 int abc = (f2 != 1)? 1 : 0;\n" +
10347
						"    Float f3 = null;\n" +
10348
						"	 if(f3 == null)\n" +
10349
						" 	 	System.out.println(\"i1 is null\");\n" +
10350
						"    Byte f4 = null;\n" +
10351
						"	 if(f4 != null)\n" +
10352
						" 	 	System.out.println(\"i2 is not null\");\n" +
10353
						"  }\n" +
10354
						"}"},
10355
				"----------\n" +
10356
				"1. ERROR in X.java (at line 4)\n" +
10357
				"	if(f1 == 1)\n" +
10358
				"	   ^^\n" +
10359
				"Null pointer access: The variable f1 can only be null at this location\n" +
10360
				"----------\n" +
10361
				"2. ERROR in X.java (at line 7)\n" +
10362
				"	int abc = (f2 != 1)? 1 : 0;\n" +
10363
				"	           ^^\n" +
10364
				"Null pointer access: The variable f2 can only be null at this location\n" +
10365
				"----------\n" +
10366
				"3. ERROR in X.java (at line 9)\n" +
10367
				"	if(f3 == null)\n" +
10368
				"	   ^^\n" +
10369
				"Redundant null check: The variable f3 can only be null at this location\n" +
10370
				"----------\n" +
10371
				"4. ERROR in X.java (at line 12)\n" +
10372
				"	if(f4 != null)\n" +
10373
				"	   ^^\n" +
10374
				"Null comparison always yields false: The variable f4 can only be null at this location\n" +
10375
				"----------\n");
10376
	}
10377
}
10378
10379
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=253896
10380
// To test whether null pointer access and potential null pointer access warnings are correctly reported when auto-unboxing
10381
public void testBug253896b() {
10382
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
10383
		this.runNegativeTest(
10384
				new String[] {
10385
						"X.java",
10386
						"public class X {\n" +
10387
						"  public void foo(Integer i1, Integer i2) {\n" +
10388
						"	 if(i1 == null && i2 == null){\n" +
10389
						"		if(i1 == 1)\n" +
10390
						" 	 	System.out.println(i1);}\n" +	//i1 is definitely null here
10391
						"	 else {\n" +
10392
						"		if(i1 == 0) {}\n" +		//i1 may be null here.
10393
						"	 }\n" +
10394
						"  }\n" +
10395
						"}"},
10396
				"----------\n" +
10397
				"1. ERROR in X.java (at line 4)\n" +
10398
				"	if(i1 == 1)\n" +
10399
				"	   ^^\n" +
10400
				"Null pointer access: The variable i1 can only be null at this location\n" +
10401
				"----------\n" +
10402
				"2. ERROR in X.java (at line 7)\n" +
10403
				"	if(i1 == 0) {}\n" +
10404
				"	   ^^\n" +
10405
				"Potential null pointer access: The variable i1 may be null at this location\n" +
10406
				"----------\n");
10407
	}
10408
}
10331
}
10409
}

Return to bug 253896