Community
Participate
Working Groups
Build Identifier: M20100211-1343 If a class "A" has a public static class "B" as well as a private field "B", and then a class in another package tries to use "A.B" - Eclipse tries to access the private field. Small example code: ------------------------- package p1; public class Main { public static void main(String[] args) { System.out.println( "value = " + p2.A.B.length); } } -------------- package p2; public class A { public final static class B { public final static String length = "very long"; } private int [] B = new int[5]; } ------------------- This code works from command line.. as well as another IDE, but Eclipse doesn't work. I currently don't know of a work-around. Reproducible: Always Steps to Reproduce: 1. (see example code in details)
javac 1.6 compiles this code fine and the Eclipse compiler is failing. Srikanth, please investigate. The workaround would be to name to private field with a different name :-).
(In reply to comment #1) > javac 1.6 compiles this code fine and the Eclipse compiler is failing. > Srikanth, please investigate. > The workaround would be to name to private field with a different name :-). Well, of course that would work. :-p I'm just saying that if my code used someone else's poorly written Jar, then eclipse wouldn't run it. Another IDE lets me undo "compile on save".. and then it simply uses javac 1.6 instead of the IDE's compiler.
(In reply to comment #2) > (In reply to comment #1) > > javac 1.6 compiles this code fine and the Eclipse compiler is failing. > > Srikanth, please investigate. > > The workaround would be to name to private field with a different name :-). > Well, of course that would work. :-p > I'm just saying that if my code used someone else's poorly written Jar, then > eclipse wouldn't run it. > Another IDE lets me undo "compile on save".. and then it simply uses javac 1.6 > instead of the IDE's compiler. Also, if i have a similar bug.. should i post it here, or make another Bug report
(In reply to comment #3) > Also, if i have a similar bug.. should i post it here, or make another Bug > report You can always post here. If this is not the same issue, we will put it into its own bug report.
(In reply to comment #4) > (In reply to comment #3) > > Also, if i have a similar bug.. should i post it here, or make another Bug > > report > You can always post here. If this is not the same issue, we will put it into > its own bug report. Ok, This bug Eclipse also thinks it is importing a class when it shouldn't be (due to obscuring). I have 2 packages with the following classes: ================================ package p1; import static p2.OuterClass.Inner; public class myRunner { public static void main(String [] args) { System.out.println("Value1 = " + Inner().toString()); System.out.println("Value2 = " + new Inner().toString()); } } ------------------------ package p1; public class Inner { public String toString() { return "The Class -- p1.Inner"; } } ------------------------- package p2; public class OuterClass { public class Inner{ @Override public String toString() { return "The Class -- p2.OuterClass.In"; } } public static String Inner() { return "The Method -- p2.OuterClass.In()"; } } ---------------------------------- ==================================== Since my import statement is STATIC, then it shouldn't be importing the non-static class with the same name as the STATIC method. "Value1" should be "The Method -- p2.OuterClass.In()" and "Value2" should be "The Class -- p1.Inner" The problem is that on the line: System.out.println("Value2 = " + new Inner().toString()); It should be invoking the class p1.Inner, but Eclipse thinks it is referring to p1.OuterClass.Inner (which isnt static and shouldn't be imported) And "new Inner()" is not a valid way to call an non-static inner Class; (thus the error)
Created attachment 172983 [details] Patch under consideration.
(In reply to comment #5) > (In reply to comment #4) > > (In reply to comment #3) > > > Also, if i have a similar bug.. should i post it here, or make another Bug > > > report > > You can always post here. If this is not the same issue, we will put it into > > its own bug report. > > Ok, > > This bug Eclipse also thinks it is importing a class when it shouldn't be (due > to obscuring). Prima facie, it does not look related. Ayush, please triage this case and see if there is a bug here. Open a new defect as needed and assign to yourself for follow up -- Thanks.
(In reply to comment #6) > Created an attachment (id=172983) [details] > Patch under consideration. Passes all tests. Ayush, please review, Thanks.
(In reply to comment #8) > (In reply to comment #6) > > Created an attachment (id=172983) [details] [details] > > Patch under consideration. > Passes all tests. Ayush, please review, Thanks. I got the same bug, abused differently, to actually compile in Eclipse and give a different answer, I thought you might like to see it: ------------------------------------ package p2; import static p1.Bar.B; import p3.Foo.*; public class OuterTest { public static void main(String [] args) { new OuterTest().beginTest(); } public void beginTest(){ System.out.print("1 + 1 = "); if(alwaysTrue()){ System.out.println("2"); } else{ System.out.println("3"); } } public boolean alwaysTrue(){ // Returns FALSE in Eclipse String myB = B.class.getCanonicalName(); String realB = p1.Bar.B.class.getCanonicalName(); return myB.equals(realB); } } ------------------------------------ package p1; public class Bar { public static class B{} final public static String B = new String("random"); } ------------------------------------ package p3; public class Foo { public class B {} } ------------------------------------ Eclipse will print out "1 + 1 = 3"
In the above bug, it appears that in "Bar" the field "B" is obscuring the class with the same name, and then it looks like the import-on-demand from "Foo.*" is shadowing the single-import "Bar.B". Perhaps the JSP is a bit to vague on these corner cases.
(In reply to comment #7) > Ayush, please triage this case and see if there is a bug here. > Open a new defect as needed and assign to yourself for > follow up -- Thanks. I have raised bug 318401 to track the imports shadowing issue and copied the relevant test cases and comments from here to there. Folks, over to bug 318401 for any discussions related to imports, please -- Thanks!
(In reply to comment #8) > (In reply to comment #6) > > Created an attachment (id=172983) [details] [details] > > Patch under consideration. > > Passes all tests. Ayush, please review, Thanks. Patch looks good.
Released in HEAD for 3.7 M1.
Verified for 3.7M1 using build I20100802-1800.