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 / +173 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
	}
2850
	
2851
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318401
2852
	// Check if we're able to find the correct static member type being imported,
2853
	// even though the import originally resolved to the static field of the same name,
2854
	// coming from the supertype
2855
	public void test084() {
2856
		this.runConformTest(
2857
			new String[] {
2858
				"Test.java",
2859
				"import static p1.Bar.B;\n" +
2860
				"import p3.Foo.*;\n" +
2861
				"public class Test {\n" +
2862
				"	public static void main(String [] args){\n" +
2863
				"		new Test().test2();" +
2864
				"	}\n" +
2865
				"	public void test2(){\n" +
2866
				"		System.out.println(B.class.getCanonicalName().toString());\n" +	// the class B
2867
				"		System.out.println(p1.Bar.B.class.getCanonicalName().toString());" +	// class B
2868
				"	}\n" +
2869
				"}\n",
2870
				"p1/Bar.java",
2871
				"package p1;\n" +
2872
				"public class Bar extends SuperBar{\n" +
2873
				"	public static class B{}\n" +
2874
				"	public static void B(){}\n" +
2875
				"}\n",
2876
				"p1/SuperBar.java",
2877
				"package p1;\n" +
2878
				"public class SuperBar {\n" +
2879
				"	final public static String B = new String(\"random\");\n" +
2880
				"}\n",
2881
				"p3/Foo.java",
2882
				"package p3;\n" +
2883
				"public class Foo {\n" +
2884
				"	public class B{\n" +
2885
				"		public int a;\n" +
2886
				"	}\n" +
2887
				"}\n"
2888
			},
2889
			"p1.Bar.B\n" + 
2890
			"p1.Bar.B");
2891
	}
2892
2723
}
2893
}
2724
2894
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java (-1 / +47 lines)
Lines 2712-2718 Link Here
2712
					}
2712
					}
2713
				}
2713
				}
2714
			}
2714
			}
2715
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
				ReferenceBinding type = null;
2720
				nextImport : for (int i = 0, length = imports.length; i < length; i++) {
2721
					ImportBinding importBinding = imports[i];
2722
					if (importBinding.isStatic()) {
2723
						ReferenceBinding temp = null;
2724
						if (CharOperation.equals(importBinding.compoundName[importBinding.compoundName.length - 1], name)) {
2725
							Binding resolvedImport = importBinding.resolvedImport;
2726
							if (resolvedImport == null) continue nextImport;
2727
							if (resolvedImport instanceof MethodBinding || resolvedImport instanceof FieldBinding) {
2728
								// check to see if there are also member types with the same name
2729
								// must find the importRef's type again since the method/field can be from an inherited type
2730
								// see StaticImportTest#test084 for more clarity
2731
								char[][] importName = importBinding.reference.tokens;
2732
								TypeBinding referencedType = getType(importName, importName.length - 1);
2733
								if (referencedType != null && referencedType instanceof ReferenceBinding) {
2734
									temp = findMemberType(name, (ReferenceBinding) referencedType);
2735
								}
2736
							}
2737
							if (temp != null && temp.isStatic() && temp != type) {
2738
								if (temp.isValidBinding()) {
2739
									if (!temp.canBeSeenBy(unitScope.fPackage)) {
2740
										// Answer error binding - type is not visible
2741
										foundType = new ProblemReferenceBinding(new char[][]{name}, type, ProblemReasons.NotVisible);
2742
									} else {
2743
										ImportReference importReference = importBinding.reference;
2744
										if (importReference != null) {
2745
											importReference.bits |= ASTNode.Used;
2746
										}
2747
										type = temp;
2748
									}
2749
								} else if (foundType == null) {
2750
									foundType = temp;
2751
								}
2752
							}
2753
						}
2754
					}
2755
				}
2756
				if (type != null) {
2757
					if (typeOrPackageCache != null)
2758
						typeOrPackageCache.put(name, type);
2759
					return type;
2760
				}
2761
			}
2716
			// check if the name is in the current package, skip it if its a sub-package
2762
			// check if the name is in the current package, skip it if its a sub-package
2717
			PackageBinding currentPackage = unitScope.fPackage;
2763
			PackageBinding currentPackage = unitScope.fPackage;
2718
			unitScope.recordReference(currentPackage.compoundName, name);
2764
			unitScope.recordReference(currentPackage.compoundName, name);

Return to bug 318401