Access Modifier Issues
Peter Levart
peter.levart at gmail.com
Fri Jan 9 14:47:46 UTC 2015
On 01/09/2015 03:11 PM, Richard Warburton wrote:
> Hi gents,
>
> Not sure if its a high priority to fix the access modifier problems at the
> moment, but there still seems to be a bug with Peter Levart's default hack
> which I didn't see reported in the other thread.
Yes, Maurizio already warned about currently buggy access to private
members from specialized methods. I think you can work-around by using
package-private access for the time being.
>
> public class Default<any T> {
>
> private T value;
>
> private Default() {}
>
> public static <any T> T value() {
> return new Default<T>().value;
> }
>
> public static void main(String[] args) {
> int i = Default.value();
> System.out.println(i);
> long l = Default.value();
> System.out.println(l);
> Object o = Default.value();
> System.out.println(o);
> }
>
> }
>
> When running:
>
> Specializing method Default$value${0=I}.value()Ljava/lang/Object; with
> class=[] and method=[I]
> Specializing Default${0=I}; searching for Default.class (not found)
> Specializing Default${0=I}; searching for Default.class (found)
> Exception in thread "main" java.lang.IllegalAccessError: tried to access
> method Default${0=I}.<init>()V from class Default$value${0=I}/793589513
> at Default$value${0=I}/793589513.value(Default.java:8)
> at Default.main(Default.java:12)
>
> Looks like its workaround-able by making both the constructor and value
> field public.
>
> I think this also exposes something I don't understand here. My impression
> was that "Default${0=I}" was the specialisation of the class Default s.t.
> its first type parameter is an int. So what is "Default$value${0=I}"?
I think Default$value${0=I}/793589513 is a special (VM-annonymous)
class, containing the specialization of static method Default.value()
for T=int.
The problem seems to be that private constructor of specialized class
Default for T=int is not accessible from it. The specialized static
method does have access to private members of nonspecialized class
Default (since this is the host class of it's VM-anonymous class where
it is defined), but not to the specialized Default class for T=int which
is a separate class. Perhaps something similar to how outer class has
access to private members of inner classes and vice-versa would have to
be devised among non-specialized classes and their specializations.
Regards, Peter
>
> regards,
>
> Richard Warburton
>
> http://insightfullogic.com
> @RichardWarburto <http://twitter.com/richardwarburto>
More information about the valhalla-dev
mailing list