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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java (-3 / +24 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 381-387 Link Here
381
			ReferenceBinding conflictingType = null;
381
			ReferenceBinding conflictingType = null;
382
			if (importBinding instanceof MethodBinding) {
382
			if (importBinding instanceof MethodBinding) {
383
				conflictingType = (ReferenceBinding) getType(compoundName, compoundName.length);
383
				conflictingType = (ReferenceBinding) getType(compoundName, compoundName.length);
384
				if (!conflictingType.isValidBinding())
384
				if (!conflictingType.isValidBinding() || (importReference.isStatic() && !conflictingType.isStatic()))
385
					conflictingType = null;
385
					conflictingType = null;
386
			}
386
			}
387
			// collisions between an imported static field & a type should be checked according to spec... but currently not by javac
387
			// collisions between an imported static field & a type should be checked according to spec... but currently not by javac
Lines 396-403 Link Here
396
				ReferenceBinding existingType = typesBySimpleNames.get(compoundName[compoundName.length - 1]);
396
				ReferenceBinding existingType = typesBySimpleNames.get(compoundName[compoundName.length - 1]);
397
				if (existingType != null) {
397
				if (existingType != null) {
398
					// duplicate test above should have caught this case, but make sure
398
					// duplicate test above should have caught this case, but make sure
399
					if (existingType == referenceBinding)
399
					if (existingType == referenceBinding) {
400
						for (int j = 0; j < index; j++) {
401
							ImportBinding resolved = resolvedImports[j];
402
							if (resolved instanceof ImportConflictBinding) {
403
								ImportConflictBinding importConflictBinding = (ImportConflictBinding) resolved;
404
								if (importConflictBinding.conflictingTypeBinding == referenceBinding) {
405
									if (!importReference.isStatic()) {
406
										// resolved is implicitly static
407
										problemReporter().duplicateImport(importReference);
408
										resolvedImports[index++] = new ImportBinding(compoundName, false, importBinding, importReference);
409
									}
410
								}
411
							} else if (resolved.resolvedImport == referenceBinding) {
412
								if ((importReference.isStatic() && !resolved.isStatic())
413
										|| (!importReference.isStatic() && resolved.isStatic())){
414
									problemReporter().duplicateImport(importReference);
415
									resolvedImports[index++] = new ImportBinding(compoundName, false, importBinding, importReference);
416
								}
417
							}
418
						}
419
						// https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
400
						continue nextImport;
420
						continue nextImport;
421
					}
401
					// either the type collides with a top level type or another imported type
422
					// either the type collides with a top level type or another imported type
402
					for (int j = 0, length = this.topLevelTypes.length; j < length; j++) {
423
					for (int j = 0, length = this.topLevelTypes.length; j < length; j++) {
403
						if (CharOperation.equals(this.topLevelTypes[j].sourceName, existingType.sourceName)) {
424
						if (CharOperation.equals(this.topLevelTypes[j].sourceName, existingType.sourceName)) {
(-)src/org/eclipse/jdt/core/tests/compiler/regression/StaticImportTest.java (-64 / +221 lines)
Lines 18-25 Link Here
18
	// All specified tests which do not belong to the class are skipped...
18
	// All specified tests which do not belong to the class are skipped...
19
	static {
19
	static {
20
//		TESTS_NAMES = new String[] { "test036" };
20
//		TESTS_NAMES = new String[] { "test036" };
21
//		TESTS_NUMBERS = new int[] { 46 };
21
//		TESTS_NUMBERS = new int[] { 79 };
22
//		TESTS_RANGE = new int[] { 169, 180 };
22
//		TESTS_RANGE = new int[] { 75, -1 };
23
	}
23
	}
24
24
25
	public StaticImportTest(String name) {
25
	public StaticImportTest(String name) {
Lines 488-533 Link Here
488
				"	public static class WW { public static final int WWW = 0; }\n" +
488
				"	public static class WW { public static final int WWW = 0; }\n" +
489
				"}\n",
489
				"}\n",
490
			},
490
			},
491
			"----------\n" +
491
			"----------\n" + 
492
			"1. ERROR in X.java (at line 6)\r\n" +
492
			"1. ERROR in X.java (at line 6)\n" + 
493
			"	import static p.Y.Zz;\r\n" +
493
			"	import static p.Y.Zz;\n" + 
494
			"	              ^^^^^^\n" +
494
			"	              ^^^^^^\n" + 
495
			"The import p.Y.Zz cannot be resolved\n" +
495
			"The import p.Y.Zz cannot be resolved\n" + 
496
			"----------\n" +
496
			"----------\n" + 
497
			"2. ERROR in X.java (at line 7)\r\n" +
497
			"2. ERROR in X.java (at line 7)\n" + 
498
			"	import static p.Z.Zz.WW.*;\r\n" +
498
			"	import static p.Z.Zz.WW.*;\n" + 
499
			"	              ^^^^^^^^^\n" +
499
			"	              ^^^^^^^^^\n" + 
500
			"The import p.Z.Zz.WW cannot be resolved\n" +
500
			"The import p.Z.Zz.WW cannot be resolved\n" + 
501
			"----------\n" +
501
			"----------\n" + 
502
			"3. ERROR in X.java (at line 8)\r\n" +
502
			"3. ERROR in X.java (at line 8)\n" + 
503
			"	import p.Y.ZZ;\r\n" +
503
			"	import p.Y.ZZ;\n" + 
504
			"	       ^^^^^^\n" +
504
			"	       ^^^^^^\n" + 
505
			"The import p.Y.ZZ cannot be resolved\n" +
505
			"The import p.Y.ZZ cannot be resolved\n" + 
506
			"----------\n" +
506
			"----------\n" + 
507
			"4. ERROR in X.java (at line 9)\r\n" +
507
			"4. ERROR in X.java (at line 9)\n" + 
508
			"	import static p.Y.ZZ.*;\r\n" +
508
			"	import static p.Y.ZZ.*;\n" + 
509
			"	              ^^^^^^\n" +
509
			"	              ^^^^^^\n" + 
510
			"The import p.Y.ZZ cannot be resolved\n" +
510
			"The import p.Y.ZZ cannot be resolved\n" + 
511
			"----------\n" +
511
			"----------\n" + 
512
			"5. ERROR in X.java (at line 10)\r\n" +
512
			"5. ERROR in X.java (at line 10)\n" + 
513
			"	import static p.Y.ZZ.WW;\r\n" +
513
			"	import static p.Y.ZZ.WW;\n" + 
514
			"	              ^^^^^^\n" +
514
			"	              ^^^^^^\n" + 
515
			"The import p.Y.ZZ cannot be resolved\n" +
515
			"The import p.Y.ZZ cannot be resolved\n" + 
516
			"----------\n" +
516
			"----------\n" + 
517
			"6. ERROR in X.java (at line 11)\r\n" +
517
			"6. ERROR in X.java (at line 11)\n" + 
518
			"	import static p.Y.ZZ.WW.*;\r\n" +
518
			"	import static p.Y.ZZ.WW.*;\n" + 
519
			"	              ^^^^^^\n" +
519
			"	              ^^^^^^\n" + 
520
			"The import p.Y.ZZ cannot be resolved\n" +
520
			"The import p.Y.ZZ cannot be resolved\n" + 
521
			"----------\n" +
521
			"----------\n" + 
522
			"7. ERROR in X.java (at line 12)\r\n" +
522
			"7. ERROR in X.java (at line 12)\n" + 
523
			"	import static p.Y.ZZ.ZZZ;\r\n" +
523
			"	import static p.Y.ZZ.ZZZ;\n" + 
524
			"	              ^^^^^^\n" +
524
			"	              ^^^^^^\n" + 
525
			"The import p.Y.ZZ cannot be resolved\n" +
525
			"The import p.Y.ZZ cannot be resolved\n" + 
526
			"----------\n" +
526
			"----------\n" + 
527
			"8. ERROR in X.java (at line 13)\r\n" +
527
			"8. ERROR in X.java (at line 13)\n" + 
528
			"	import static p.Y.ZZ.WW.WWW;\r\n" +
528
			"	import static p.Y.ZZ.WW.WWW;\n" + 
529
			"	              ^^^^^^\n" +
529
			"	              ^^^^^^\n" + 
530
			"The import p.Y.ZZ cannot be resolved\n" +
530
			"The import p.Y.ZZ cannot be resolved\n" + 
531
			"----------\n"
531
			"----------\n"
532
		);
532
		);
533
	}
533
	}
Lines 590-597 Link Here
590
				"public class X {}\n"
590
				"public class X {}\n"
591
			},
591
			},
592
			"----------\n" +
592
			"----------\n" +
593
			"1. ERROR in X.java (at line 1)\r\n" +
593
			"1. ERROR in X.java (at line 1)\n" +
594
			"	import static java.lang.*;\r\n" +
594
			"	import static java.lang.*;\n" +
595
			"	              ^^^^^^^^^\n" +
595
			"	              ^^^^^^^^^\n" +
596
			"Only a type can be imported. java.lang resolves to a package\n" +
596
			"Only a type can be imported. java.lang resolves to a package\n" +
597
			"----------\n"
597
			"----------\n"
Lines 887-894 Link Here
887
				"public class C { public static void m() {} }\n"
887
				"public class C { public static void m() {} }\n"
888
			},
888
			},
889
			"----------\n" +
889
			"----------\n" +
890
			"1. ERROR in p\\B.java (at line 5)\r\n" +
890
			"1. ERROR in p\\B.java (at line 5)\n" +
891
			"	public class B { public static void main(String[] args) { m(); } }\r\n" +
891
			"	public class B { public static void main(String[] args) { m(); } }\n" +
892
			"	                                                          ^\n" +
892
			"	                                                          ^\n" +
893
			"The method m() is ambiguous for the type B\n" +
893
			"The method m() is ambiguous for the type B\n" +
894
			"----------\n"
894
			"----------\n"
Lines 1154-1161 Link Here
1154
				"}\n"
1154
				"}\n"
1155
			},
1155
			},
1156
			"----------\n" +
1156
			"----------\n" +
1157
			"1. ERROR in X2.java (at line 3)\r\n" +
1157
			"1. ERROR in X2.java (at line 3)\n" +
1158
			"	public class X2 { void test() {foo();} }\r\n" +
1158
			"	public class X2 { void test() {foo();} }\n" +
1159
			"	                               ^^^\n" +
1159
			"	                               ^^^\n" +
1160
			"The method foo() is ambiguous for the type X2\n" +
1160
			"The method foo() is ambiguous for the type X2\n" +
1161
			"----------\n"
1161
			"----------\n"
Lines 1313-1320 Link Here
1313
				"}\n"
1313
				"}\n"
1314
			},
1314
			},
1315
			"----------\n" +
1315
			"----------\n" +
1316
			"1. ERROR in C.java (at line 6)\r\n" +
1316
			"1. ERROR in C.java (at line 6)\n" +
1317
			"	void test() { foo(1); }\r\n" +
1317
			"	void test() { foo(1); }\n" +
1318
			"	              ^^^\n" +
1318
			"	              ^^^\n" +
1319
			"The method foo(Object, String) in the type A.B is not applicable for the arguments (int)\n" +
1319
			"The method foo(Object, String) in the type A.B is not applicable for the arguments (int)\n" +
1320
			"----------\n");
1320
			"----------\n");
Lines 1464-1481 Link Here
1464
				"class B { public static int foo_B() { return 2;} }",
1464
				"class B { public static int foo_B() { return 2;} }",
1465
			},
1465
			},
1466
			"----------\n" +
1466
			"----------\n" +
1467
			"1. ERROR in X.java (at line 1)\r\n" +
1467
			"1. ERROR in X.java (at line 1)\n" +
1468
			"	import static p.A.foo_I;\r\n" +
1468
			"	import static p.A.foo_I;\n" +
1469
			"	              ^^^^^^^^^\n" +
1469
			"	              ^^^^^^^^^\n" +
1470
			"The import p.A.foo_I cannot be resolved\n" +
1470
			"The import p.A.foo_I cannot be resolved\n" +
1471
			"----------\n" +
1471
			"----------\n" +
1472
			"2. ERROR in X.java (at line 4)\r\n" +
1472
			"2. ERROR in X.java (at line 4)\n" +
1473
			"	static int i = p.A.foo_I();\r\n" +
1473
			"	static int i = p.A.foo_I();\n" +
1474
			"	               ^^^^^^^^^^^\n" +
1474
			"	               ^^^^^^^^^^^\n" +
1475
			"Cannot make a static reference to the non-static method foo_I() from the type I\n" +
1475
			"Cannot make a static reference to the non-static method foo_I() from the type I\n" +
1476
			"----------\n" +
1476
			"----------\n" +
1477
			"3. ERROR in X.java (at line 6)\r\n" +
1477
			"3. ERROR in X.java (at line 6)\n" +
1478
			"	static int m = foo_I();\r\n" +
1478
			"	static int m = foo_I();\n" +
1479
			"	               ^^^^^\n" +
1479
			"	               ^^^^^\n" +
1480
			"The method foo_I() is undefined for the type X\n" +
1480
			"The method foo_I() is undefined for the type X\n" +
1481
			"----------\n");
1481
			"----------\n");
Lines 1567-1574 Link Here
1567
				"}\n",
1567
				"}\n",
1568
			},
1568
			},
1569
			"----------\n" +
1569
			"----------\n" +
1570
			"1. ERROR in ImportTest.java (at line 4)\r\n" +
1570
			"1. ERROR in ImportTest.java (at line 4)\n" +
1571
			"	static void printArgs(Object... args) { toString2(args); }\r\n" +
1571
			"	static void printArgs(Object... args) { toString2(args); }\n" +
1572
			"	                                        ^^^^^^^^^\n" +
1572
			"	                                        ^^^^^^^^^\n" +
1573
			"The method toString2() in the type SuperTest is not applicable for the arguments (Object[])\n" +
1573
			"The method toString2() in the type SuperTest is not applicable for the arguments (Object[])\n" +
1574
			"----------\n"
1574
			"----------\n"
Lines 1715-1722 Link Here
1715
				"public class Three extends Two { public static void test(String s) { arrayList(s); } }\n",
1715
				"public class Three extends Two { public static void test(String s) { arrayList(s); } }\n",
1716
			},
1716
			},
1717
			"----------\n" +
1717
			"----------\n" +
1718
			"1. ERROR in test\\Three.java (at line 3)\r\n" +
1718
			"1. ERROR in test\\Three.java (at line 3)\n" +
1719
			"	public class Three extends Two { public static void test(String s) { arrayList(s); } }\r\n" +
1719
			"	public class Three extends Two { public static void test(String s) { arrayList(s); } }\n" +
1720
			"	                                                                     ^^^^^^^^^\n" +
1720
			"	                                                                     ^^^^^^^^^\n" +
1721
			"The method arrayList(int) in the type Two is not applicable for the arguments (String)\n" +
1721
			"The method arrayList(int) in the type Two is not applicable for the arguments (String)\n" +
1722
			"----------\n"
1722
			"----------\n"
Lines 2221-2228 Link Here
2221
				"}\n",
2221
				"}\n",
2222
			},
2222
			},
2223
			"----------\n" +
2223
			"----------\n" +
2224
			"1. ERROR in p\\X.java (at line 2)\r\n" +
2224
			"1. ERROR in p\\X.java (at line 2)\n" +
2225
			"	import static q.A.a;\r\n" +
2225
			"	import static q.A.a;\n" +
2226
			"	              ^^^^^\n" +
2226
			"	              ^^^^^\n" +
2227
			"The import q.A.a cannot be resolved\n" +
2227
			"The import q.A.a cannot be resolved\n" +
2228
			"----------\n");
2228
			"----------\n");
Lines 2358-2365 Link Here
2358
				"}",
2358
				"}",
2359
			},
2359
			},
2360
			"----------\n" +
2360
			"----------\n" +
2361
			"1. ERROR in X.java (at line 1)\r\n" +
2361
			"1. ERROR in X.java (at line 1)\n" +
2362
			"	import static p.I.E.C;\r\n" +
2362
			"	import static p.I.E.C;\n" +
2363
			"	              ^^^^^^^\n" +
2363
			"	              ^^^^^^^\n" +
2364
			"The import p.I.E.C cannot be resolved\n" +
2364
			"The import p.I.E.C cannot be resolved\n" +
2365
			"----------\n");
2365
			"----------\n");
Lines 2533-2537 Link Here
2533
			},
2533
			},
2534
			"");
2534
			"");
2535
	}		
2535
	}		
2536
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
2537
	// To verify that a static import importing a type which has already been
2538
	// imported by a single type import is reported as duplicate
2539
	// while the other static members imported by it are not shadowed.
2540
	public void test075() {
2541
		this.runNegativeTest(
2542
			new String[] {
2543
				"A/A.java",
2544
				"package A;\n" +
2545
				"import B.B.C1;\n" +
2546
				"import static B.B.C1;\n" +
2547
				"public abstract class A {\n" +
2548
				"	protected void A1(Object task) {\n" +
2549
				"		C1 c = C1(task);\n" +
2550
				"	}\n" +
2551
				"}\n",
2552
				"B/B.java",
2553
				"package B;\n" +
2554
				"final public class B {\n" +
2555
				"	private B() {}\n" +
2556
				"	public static class C1 {}\n" +
2557
				"	public static C1 C1(Object o) {\n" +
2558
				"		return new C1();\n" +
2559
				"	}\n" +
2560
				"}\n",
2561
			},
2562
			"----------\n" +
2563
			"1. ERROR in A\\A.java (at line 3)\n" +
2564
			"	import static B.B.C1;\n" +
2565
			"	              ^^^^^^\n" +
2566
			"The import B.B.C1 collides with another import statement\n" +
2567
			"----------\n"
2568
		);
2569
	}
2570
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
2571
	// To verify that a static import importing a static method doesn't collide
2572
	// with a single type import importing a non-static type with the same name as the method
2573
	public void test076() {
2574
		this.runConformTest(
2575
			new String[] {
2576
				"A/A.java",
2577
				"package A;\n" +
2578
				"import B.B.C1;\n" +
2579
				"import static B.B.C1;\n" +
2580
				"public class A {\n" +
2581
				"	protected void A1(Object task) {\n" +
2582
				"		C1 c1;\n" +
2583
				"		int c = C1(task);\n" +
2584
				"	}\n" +
2585
				"}\n",
2586
				"B/B.java",
2587
				"package B;\n" +
2588
				"final public class B {\n" +
2589
				"	private B() {}\n" +
2590
				"	public class C1 {}\n" +
2591
				"	public static int C1(Object o) {\n" +
2592
				"		return 1;\n" +
2593
				"	}\n" +
2594
				"}\n",
2595
			},
2596
			"");
2597
	}
2598
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
2599
	// To verify that two static imports importing the same type don't collide
2600
	public void test077() {
2601
		this.runConformTest(
2602
			new String[] {
2603
				"p1/X.java",
2604
				"package p1;\n" + 
2605
				"import p2.A;\n" + 
2606
				"import static p2.A.C;\n" + 
2607
				"import static p1.B.C;\n" + 
2608
				"public class X {\n" + 
2609
				"	public static void main(String[] args) {\n" + 
2610
				"		foo();\n" + 
2611
				"	}\n" + 
2612
				"	public static void foo() {\n" + 
2613
				"		if (C.CONST == 1) {\n" + 
2614
				"			System.out.println(\"SUCCESS\");\n" + 
2615
				"			return;\n" + 
2616
				"		}\n" + 
2617
				"		System.out.println(\"FAILED\");\n" + 
2618
				"	}\n" + 
2619
				"}\n" + 
2620
				"class B extends A {}",
2621
				"p2/A.java",
2622
				"package p2;\n" + 
2623
				"public class A {\n" + 
2624
				"	public static class C {\n" + 
2625
				"		public static int CONST = 1;\n" + 
2626
				"	}\n" + 
2627
				"}"
2628
			},
2629
			"SUCCESS");
2630
	}
2631
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
2632
	// To verify that a static import importing a type which has already been
2633
	// imported by a single type import is reported as duplicate
2634
	// while the other static members imported by it are not shadowed.
2635
	public void test078() {
2636
		this.runNegativeTest(
2637
			new String[] {
2638
				"A/A.java",
2639
				"package A;\n" +
2640
				"import static B.B.C1;\n" +
2641
				"import B.B.C1;\n" +
2642
				"public abstract class A {\n" +
2643
				"	protected void A1(Object task) {\n" +
2644
				"		C1 c = C1(task);\n" +
2645
				"	}\n" +
2646
				"}\n",
2647
				"B/B.java",
2648
				"package B;\n" +
2649
				"final public class B {\n" +
2650
				"	private B() {}\n" +
2651
				"	public static class C1 {}\n" +
2652
				"	public static C1 C1(Object o) {\n" +
2653
				"		return new C1();\n" +
2654
				"	}\n" +
2655
				"}\n",
2656
			},
2657
			"----------\n" + 
2658
			"1. ERROR in A\\A.java (at line 3)\n" + 
2659
			"	import B.B.C1;\n" + 
2660
			"	       ^^^^^^\n" + 
2661
			"The import B.B.C1 collides with another import statement\n" + 
2662
			"----------\n"
2663
		);
2664
	}
2665
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
2666
	// To verify that a static import importing a type which has already been
2667
	// imported by a single type import is reported as duplicate
2668
	public void test079() {
2669
		this.runNegativeTest(
2670
			new String[] {
2671
				"A/A.java",
2672
				"package A;\n" +
2673
				"import static B.B.C1;\n" +
2674
				"import B.B.C1;\n" +
2675
				"public abstract class A {\n" +
2676
				"	protected void A1(C1 c) {\n" +
2677
				"	}\n" +
2678
				"}\n",
2679
				"B/B.java",
2680
				"package B;\n" +
2681
				"final public class B {\n" +
2682
				"	public static class C1 {}\n" +
2683
				"}\n",
2684
			},
2685
			"----------\n" + 
2686
			"1. ERROR in A\\A.java (at line 3)\n" + 
2687
			"	import B.B.C1;\n" + 
2688
			"	       ^^^^^^\n" + 
2689
			"The import B.B.C1 collides with another import statement\n" + 
2690
			"----------\n"
2691
		);
2692
	}
2536
}
2693
}
2537
2694

Return to bug 302865