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 / +25 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 27-32 Link Here
27
import org.eclipse.jdt.internal.compiler.lookup.Scope;
27
import org.eclipse.jdt.internal.compiler.lookup.Scope;
28
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
28
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
29
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
29
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
30
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
30
import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
31
import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
31
32
32
/**
33
/**
Lines 559-567 Link Here
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 (((checkType & CHECK_MASK) == CAN_ONLY_NULL) && (reference.implicitConversion & TypeIds.UNBOXING) != 0) { // check for auto-unboxing first and report appropriate warning
564
							scope.problemReporter().localVariableNullReference(local, reference);
565
							return;
566
						}
562
						scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
567
						scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
563
						return;
568
						return;
564
					case FlowContext.IN_COMPARISON_NON_NULL:
569
					case FlowContext.IN_COMPARISON_NON_NULL:
570
						if (((checkType & CHECK_MASK) == CAN_ONLY_NULL) && (reference.implicitConversion & TypeIds.UNBOXING) != 0) { // check for auto-unboxing first and report appropriate warning
571
							scope.problemReporter().localVariableNullReference(local, reference);
572
							return;
573
						}
565
						scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
574
						scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
566
						return;
575
						return;
567
					case FlowContext.IN_ASSIGNMENT:
576
					case FlowContext.IN_ASSIGNMENT:
Lines 571-576 Link Here
571
						scope.problemReporter().localVariableNullInstanceof(local, reference);
580
						scope.problemReporter().localVariableNullInstanceof(local, reference);
572
						return;
581
						return;
573
				}
582
				}
583
			} else if (flowInfo.isPotentiallyNull(local)) {
584
				switch(checkType & CONTEXT_MASK) {
585
					case FlowContext.IN_COMPARISON_NULL:
586
						if (((checkType & CHECK_MASK) == CAN_ONLY_NULL) && (reference.implicitConversion & TypeIds.UNBOXING) != 0) { // check for auto-unboxing first and report appropriate warning
587
							scope.problemReporter().localVariablePotentialNullReference(local, reference);
588
							return;
589
						}
590
						break;
591
					case FlowContext.IN_COMPARISON_NON_NULL:
592
						if (((checkType & CHECK_MASK) == CAN_ONLY_NULL) && (reference.implicitConversion & TypeIds.UNBOXING) != 0) { // check for auto-unboxing first and report appropriate warning
593
							scope.problemReporter().localVariablePotentialNullReference(local, reference);
594
							return;
595
						}
596
						break;
597
					}
574
			} else if (flowInfo.cannotBeDefinitelyNullOrNonNull(local)) {
598
			} else if (flowInfo.cannotBeDefinitelyNullOrNonNull(local)) {
575
				return;
599
				return;
576
			}
600
			}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (-1 / +78 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 10328-10331 Link Here
10328
				"----------\n");
10328
				"----------\n");
10329
	}
10329
	}
10330
}
10330
}
10331
10332
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=253896
10333
// Test whether Null pointer access warnings are being reported correctly when auto-unboxing
10334
public void testBug253896a() {
10335
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
10336
		this.runNegativeTest(
10337
				new String[] {
10338
						"X.java",
10339
						"public class X {\n" +
10340
						"  public void foo() {\n" +
10341
						"    Integer f1 = null;\n" +
10342
						"	 if(f1 == 1)\n" +
10343
						" 	 	System.out.println(\"f1 is 1\");\n" +
10344
						"    Integer f2 = null;\n" +
10345
						"	 int abc = (f2 != 1)? 1 : 0;\n" +
10346
						"    Float f3 = null;\n" +
10347
						"	 if(f3 == null)\n" +
10348
						" 	 	System.out.println(\"i1 is null\");\n" +
10349
						"    Byte f4 = null;\n" +
10350
						"	 if(f4 != null)\n" +
10351
						" 	 	System.out.println(\"i2 is not null\");\n" +
10352
						"  }\n" +
10353
						"}"},
10354
				"----------\n" +
10355
				"1. ERROR in X.java (at line 4)\n" +
10356
				"	if(f1 == 1)\n" +
10357
				"	   ^^\n" +
10358
				"Null pointer access: The variable f1 can only be null at this location\n" +
10359
				"----------\n" +
10360
				"2. ERROR in X.java (at line 7)\n" +
10361
				"	int abc = (f2 != 1)? 1 : 0;\n" +
10362
				"	           ^^\n" +
10363
				"Null pointer access: The variable f2 can only be null at this location\n" +
10364
				"----------\n" +
10365
				"3. ERROR in X.java (at line 9)\n" +
10366
				"	if(f3 == null)\n" +
10367
				"	   ^^\n" +
10368
				"Redundant null check: The variable f3 can only be null at this location\n" +
10369
				"----------\n" +
10370
				"4. ERROR in X.java (at line 12)\n" +
10371
				"	if(f4 != null)\n" +
10372
				"	   ^^\n" +
10373
				"Null comparison always yields false: The variable f4 can only be null at this location\n" +
10374
				"----------\n");
10375
	}
10376
}
10377
10378
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=253896
10379
// To test whether null pointer access and potential null pointer access warnings are correctly reported when auto-unboxing
10380
public void testBug253896b() {
10381
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
10382
		this.runNegativeTest(
10383
				new String[] {
10384
						"X.java",
10385
						"public class X {\n" +
10386
						"  public void foo(Integer i1, Integer i2) {\n" +
10387
						"	 if(i1 == null && i2 == null){\n" +
10388
						"		if(i1 == 1)\n" +
10389
						" 	 	System.out.println(i1);}\n" +	//i1 is definitely null here
10390
						"	 else {\n" +
10391
						"		if(i1 == 0) {}\n" +		//i1 may be null here.
10392
						"	 }\n" +
10393
						"  }\n" +
10394
						"}"},
10395
				"----------\n" +
10396
				"1. ERROR in X.java (at line 4)\n" +
10397
				"	if(i1 == 1)\n" +
10398
				"	   ^^\n" +
10399
				"Null pointer access: The variable i1 can only be null at this location\n" +
10400
				"----------\n" +
10401
				"2. ERROR in X.java (at line 7)\n" +
10402
				"	if(i1 == 0) {}\n" +
10403
				"	   ^^\n" +
10404
				"Potential null pointer access: The variable i1 may be null at this location\n" +
10405
				"----------\n");
10406
	}
10407
}
10331
}
10408
}

Return to bug 253896