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

Collapse All | Expand All

(-)a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StaticImportTest.java (-3 / +130 lines)
Lines 2484-2493 Link Here
2484
				"}\n",
2484
				"}\n",
2485
			},
2485
			},
2486
			"----------\n" + 
2486
			"----------\n" + 
2487
			"1. ERROR in p1\\A.java (at line 7)\n" + 
2487
			"1. ERROR in p1\\A.java (at line 6)\n" + 
2488
			"	int v2 = b.fooC;\n" + 
2488
			"	int v1 = b.fooB;\n" + 
2489
			"	           ^^^^\n" + 
2489
			"	           ^^^^\n" + 
2490
			"fooC cannot be resolved or is not a field\n" + 
2490
			"fooB cannot be resolved or is not a field\n" + 
2491
			"----------\n");
2491
			"----------\n");
2492
	}	
2492
	}	
2493
	//https://bugs.eclipse.org/bugs/show_bug.cgi?id=256375
2493
	//https://bugs.eclipse.org/bugs/show_bug.cgi?id=256375
Lines 2720-2724 Link Here
2720
			"----------\n"
2720
			"----------\n"
2721
		);
2721
		);
2722
	}
2722
	}
2723
	
2724
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318401
2725
	public void test081() {
2726
		this.runConformTest(
2727
			new String[] {
2728
				"Test.java",
2729
				"import static p1.Bar.B;\n" +
2730
				"import p3.Foo.*;\n" +
2731
				"public class Test {\n" +
2732
				"	public static void main(String [] args){\n" +
2733
				"		new Test().beginTest();" +
2734
				"	}\n" +
2735
				"	public void beginTest(){\n" +
2736
				"		System.out.print(\"1 + 1 =  \");\n" +
2737
				"		if(alwaysTrue()) System.out.println(\"2\");\n" +
2738
				"		else System.out.println(\"3\"); " +
2739
				"	}\n" +
2740
				"	public boolean alwaysTrue(){\n" +
2741
				"		String myB   =        B.class.getCanonicalName();;\n" +		// refers to p1.Bar.B (class)
2742
				"		String realB = p1.Bar.B.class.getCanonicalName();;\n" +     // refers to p1.Bar.B (class)
2743
				"		B();\n" +				// refers to p1.Bar.B() (method)
2744
				"		return myB.equals(realB);\n" +
2745
				"	}\n" +
2746
				"}\n",
2747
				"p1/Bar.java",
2748
				"package p1;\n" +
2749
				"public class Bar{\n" +
2750
				"	public static class B{}\n" +
2751
				"	final public static String B = new String(\"random\");\n" +
2752
				"	public static void B(){}\n" +
2753
				"}\n",
2754
				"p3/Foo.java",
2755
				"package p3;\n" +
2756
				"public class Foo {\n" +
2757
				"	public class B{\n" +
2758
				"		public int a;\n" +
2759
				"	}\n" +
2760
				"}\n"
2761
			},
2762
			"1 + 1 =  2");
2763
	}
2764
	
2765
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318401
2766
	public void test082() {
2767
		this.runNegativeTest(
2768
			new String[] {
2769
				"p1/Bar.java",
2770
				"package p1;\n" +
2771
				"public class Bar{\n" +
2772
				"	public static class B{}\n" +
2773
				"	final public static String B = new String(\"random\");\n" +
2774
				"	public static void B(){}\n" +
2775
				"}\n",
2776
				"p3/Foo.java",
2777
				"package p3;\n" +
2778
				"public class Foo {\n" +
2779
				"	public class B{\n" +
2780
				"		public int a;\n" +
2781
				"	}\n" +
2782
				"}\n",
2783
				"p2/Test.java",
2784
				"package p2;\n" +
2785
				"import static p1.Bar.B;\n" +
2786
				"import p3.Foo.*;\n" +
2787
				"public class Test {\n" +
2788
				"	public static void main(String [] args){\n" +
2789
				"		new Test().beginTest();" +
2790
				"	}\n" +
2791
				"	public void beginTest(){\n" +
2792
				"		System.out.print(\"1 + 1 =  \");\n" +
2793
				"		if(alwaysTrue()) System.out.println(\"2\");\n" +
2794
				"		else System.out.println(\"3\"); " +
2795
				"	}\n" +
2796
				"	public boolean alwaysTrue(){\n" +
2797
				"		B b = null;\n" +		// refers to p1.Bar.B (class)
2798
				"		String realB = B;\n" +  // refers to p1.Bar.B (field)
2799
				"		B();\n" +				// refers to p1.Bar.B() (method)
2800
				"		int abc = b.a;\n;" +	// static import for Bar.B overshadows on demand import Foo.B
2801
				"	}\n" +
2802
				"}\n",
2803
			},
2804
			"----------\n" +
2805
			"1. ERROR in p2\\Test.java (at line 15)\n" + 
2806
			"	int abc = b.a;\n" + 
2807
			"	            ^\n" + 
2808
			"a cannot be resolved or is not a field\n" + 
2809
			"----------\n");
2810
	}
2811
	
2812
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318401
2813
	public void test083() {
2814
		this.runConformTest(
2815
			new String[] {
2816
				"Test.java",
2817
				"import static p1.Bar.B;\n" +
2818
				"import p3.Foo.*;\n" +
2819
				"public class Test {\n" +
2820
				"	public static void main(String [] args){\n" +
2821
				"		new Test().test2();" +
2822
				"	}\n" +
2823
				"	public void test2(){\n" +
2824
				"		System.out.println(B.toString());\n" +		// Field obscures class B
2825
				"		System.out.println(p1.Bar.B.toString());\n" +  // Field obscures the class B
2826
				"		System.out.println(B.class.getCanonicalName().toString());\n" +	// the class B
2827
				"		System.out.println(p1.Bar.B.class.getCanonicalName().toString());" +	// class B
2828
				"	}\n" +
2829
				"}\n",
2830
				"p1/Bar.java",
2831
				"package p1;\n" +
2832
				"public class Bar{\n" +
2833
				"	public static class B{}\n" +
2834
				"	final public static String B = new String(\"random\");\n" +
2835
				"	public static void B(){}\n" +
2836
				"}\n",
2837
				"p3/Foo.java",
2838
				"package p3;\n" +
2839
				"public class Foo {\n" +
2840
				"	public class B{\n" +
2841
				"		public int a;\n" +
2842
				"	}\n" +
2843
				"}\n"
2844
			},
2845
			"random\n" + 
2846
			"random\n" + 
2847
			"p1.Bar.B\n" + 
2848
			"p1.Bar.B");
2849
	}
2723
}
2850
}
2724
2851
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java (+71 lines)
Lines 2712-2717 Link Here
2712
					}
2712
					}
2713
				}
2713
				}
2714
			}
2714
			}
2715
			// walk single static imports. A type found here will shadow types with same name in other CU's, or types coming
2716
			// from on-demand imports. JLS 7.5.3
2717
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318401
2718
			if (imports != null) {
2719
				boolean foundInImport = false;
2720
				ReferenceBinding type = null;
2721
				nextImport : for (int i = 0, length = imports.length; i < length; i++) {
2722
					ImportBinding importBinding = imports[i];
2723
					if (importBinding.isStatic()) {
2724
						ReferenceBinding temp = null;
2725
						if (CharOperation.equals(importBinding.compoundName[importBinding.compoundName.length - 1], name)) {
2726
							Binding resolvedImport = importBinding.resolvedImport;
2727
							if (resolvedImport == null) continue nextImport;
2728
							if (resolvedImport instanceof MethodBinding) {
2729
								// check to see if there are also member types with the same name
2730
								MethodBinding staticMethod = (MethodBinding) resolvedImport;
2731
								if (CharOperation.equals(staticMethod.selector, name)) {
2732
									// must find the importRef's type again since the field can be from an inherited type
2733
									char[][] importName = importBinding.reference.tokens;
2734
									TypeBinding referencedType = getType(importName, importName.length - 1);
2735
									if (referencedType != null) {
2736
										temp = findMemberType(name, staticMethod.declaringClass);
2737
										if (temp != null && !temp.isStatic()) temp = null;
2738
									}
2739
								}
2740
							} else if (resolvedImport instanceof FieldBinding) {
2741
								// check to see if there are also member types with the same name
2742
								FieldBinding staticField = (FieldBinding) resolvedImport;
2743
								if (CharOperation.equals(staticField.name, name)) {
2744
									// must find the importRef's type again since the field can be from an inherited type
2745
									char[][] importName = importBinding.reference.tokens;
2746
									TypeBinding referencedType = getType(importName, importName.length - 1);
2747
									if (referencedType != null) {
2748
										temp = findMemberType(name, staticField.declaringClass);
2749
										if (temp != null && !temp.isStatic()) temp = null;
2750
									}
2751
								}
2752
							}
2753
							if (temp != type && temp != null) {
2754
								if (temp.isValidBinding()) {
2755
									if (!temp.canBeSeenBy(unitScope.fPackage)) {
2756
										// Answer error binding - type is not visible
2757
										foundType = new ProblemReferenceBinding(new char[][]{name}, type, ProblemReasons.NotVisible);
2758
									} else {
2759
										ImportReference importReference = importBinding.reference;
2760
										if (importReference != null) {
2761
											importReference.bits |= ASTNode.Used;
2762
										}
2763
										if (foundInImport) {
2764
											// Answer error binding -- import on demand conflict; name found in two import on demand packages.
2765
											temp = new ProblemReferenceBinding(new char[][]{name}, type, ProblemReasons.Ambiguous);
2766
											if (typeOrPackageCache != null)
2767
												typeOrPackageCache.put(name, temp);
2768
											return temp;
2769
										}
2770
										type = temp;
2771
										foundInImport = true;
2772
									}
2773
								} else if (foundType == null) {
2774
									foundType = temp;
2775
								}
2776
							}
2777
						}
2778
					}
2779
				}
2780
				if (type != null) {
2781
					if (typeOrPackageCache != null)
2782
						typeOrPackageCache.put(name, type);
2783
					return type;
2784
				}
2785
			}
2715
2786
2716
			// check if the name is in the current package, skip it if its a sub-package
2787
			// check if the name is in the current package, skip it if its a sub-package
2717
			PackageBinding currentPackage = unitScope.fPackage;
2788
			PackageBinding currentPackage = unitScope.fPackage;

Return to bug 318401