(reflect) Accessing members of inner annotations types

Gunnar Morling gunnar at hibernate.org
Fri Jan 3 13:14:01 UTC 2014


Hi,

In the course of running the Bean Validation TCK on JDK 8, I'm
investigating an issue around reflectively accessing members of annotations
which are declared as inner type of another, package-private annotation
type.

The following shows an example:

    @Retention( RetentionPolicy.RUNTIME )
    /*package-private */ @interface Named {

        @Retention( RetentionPolicy.RUNTIME )
        /*package-private */ @interface List {
            Named[] value();
        }
    }

The @List annotation is used as this on a type in the same package:

    public class Foo {

        @Named.List( @Named )
        public void getBar() {}
    }

I'm then trying to access the @Named annotation using reflection like this:

    Annotation listAnnotation = Foo.class.getMethod( "getBar"
).getAnnotations()[0];

    Method method = listAnnotation.getClass().getMethod( "value" );
    method.setAccessible( true );

    //fails
    Annotation namedAnnotation = ( (Annotation[]) method.invoke(
listAnnotation ) )[0];

This is the exception I get:

    IllegalAccessError: tried to access class com.example.Named from class
com.sun.proxy.$Proxy3
      at com.sun.proxy.$Proxy3.value(Unknown Source)

Interestingly, this only occurs when the List annotation is declared as an
inner type within the Named type; it works when the List annotation is a
top-level package-private type (the Named annotation still being
package-private) as well as when it is declared as an inner type within
another package-private class.

I first thought that this might be related to 8004260 ("dynamic proxy class
should have the same Java language access as the proxy interfaces"), but
the issue occurs on on JDK 7 as well.

Is this a potential issue with the access check implementation or is
something wrong here with the way I'm accessing the annotation member?

Thanks for any help,

--Gunnar



More information about the core-libs-dev mailing list