Community
Participate
Working Groups
Build ID: M20070212-1330 Steps To Reproduce: Given the following classes: public class Foo <T>{ private T myT; public T getT() { return myT; } public void setT(T aT) { myT = aT; } } public class Bar extends Foo<Bar.Baz> { public static void main(String[] args) { Bar myBar = new Bar(); myBar.setT(new Baz()); System.out.println(myBar.getT().toString()); } private static class Baz { @Override public String toString() { return "Baz"; } } } Eclipse compiles and runs the code even though the Baz inner class is private. javac reports: Bar.java:1: Bar.Baz has private access in Bar public class Bar extends Foo<Bar.Baz> ^ 1 error The problem occurs in 3.2 as well as 3.3m6.
We consider the access ok since we are in Bar. If you move it to a third compilation unit, then we report an error.
That pretty much makes sense to me, especially because in my actual code T is bounded by a public interface, which I'd think should be legal. So is this a bug in javac, or just a different interpretation of the language spec?
This feels a bug in javac. Note that the following code compiles fine (note reference to Bar.Baz) moved to be a field type instead of a bound. I suspect they improperly set the resolution context for a type variable bound to consider itself outside the current compilation unit, and hence miss the fact private access is allowed. You should report this to them. class Foo <T>{ private T myT; public T getT() { return myT; } public void setT(T aT) { myT = aT; } } class Bar extends Foo<Object> { Bar.Baz baz; public static void main(String[] args) { Bar myBar = new Bar(); myBar.setT(new Baz()); System.out.println(myBar.getT().toString()); } private static class Baz { @Override public String toString() { return "Baz"; } } }
Closing as invalid, since working as designed. Added GenericTypeTest#test1135.
Did anybody report this to sun? If so do you have a bug number?
ECJ and Javac may differ on their interpretation of the eligible context for access to private member. Javac would narrow the scope to the body of the type declaration, hence any access in extends/implements clauses, or leading annotations would be forbidden. e.g. @interface Ann { Class value(); } @Ann(X.Inner.class) public class X { private interface Inner{} }
*** Bug 237656 has been marked as a duplicate of this bug. ***
Reopening to clarify the scope for private access.
Extra example: /** * @see X.Inner */ public class X { private interface Inner{} } javadoc complains too.
Hi philippe, I cut&paste my answer to openjdk compiler-dev here: ------------------------------------------------------------------------- In section 6.6.1, the next to last bullet says: "if the member or constructor is declared |private|, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor" If body means body like in the grammar; /NormalClassDeclaration: /| class|/ Identifier [/TypeParameters]/ [/|extends|/ Type] [/|implements|/ TypeList] ClassBody/ Annotations or extends/implements types are not in the body so javac is correct.
Yes, I was looking at the spec, and was coming to a similar conclusion. Thanks also Maurizio for pointing me at it. "...if the member or constructor is declared |private|, then access is permitted if and only if it occurs within the body of the top level class (§7.6) <http://java.sun.com/docs/books/jls/third_edition/html/packages.html#26783> that encloses the declaration of the member or constructor." This is why I reopened this issue. Now it feels strange that the scope is only for a class body, and not entire class definition... dura lex sed lex.
Added GenericTypeTest#test1357-1358 Added LookupTest#test087
*** Bug 254703 has been marked as a duplicate of this bug. ***
Srikanth - pls have a look
Same as bug 273751
*** Bug 273751 has been marked as a duplicate of this bug. ***
Created attachment 134470 [details] Proposed patch and testcases
Olivier - please review for RC1
+1
Released for 3.5RC1.
Verified for 3.5RC1 using I20090511-2000.
Hi people, Maybe I missed something, but this is not fixed in 3.5.2. I can reproduce the result from Bug 86291 which is marked as a duplicate of this bug (JDT allows the access of a private field outside the class body, whereas javac don't). My personal test case was a JPA @NamedQuery annotation with the "query" attribute referencing a private static field: @NamedQuery(name = Client.QUERY_NAME, query = Client.QUERY) public class Client { public static final String QUERY_NAME = "client.query.name"; private static final String QUERY = "from Client"; } Both fields are visible according to JDT, but QUERY is not according to javac 1.6.0_20 (same case as Bug 254703 btw). Environment: Windows XP, Eclipse Galileo SR2 (Build 20100218-1602 with JDT 3.5.2), Sun jdk1.6.0_20
I opened bug 316456 to track this down.