[type-annos-observers] Receivers and parameters annotated type question

elena votchennikova elena.votchennikova at oracle.com
Wed Nov 20 04:06:27 PST 2013


Hi all,

could you please help me to understand behavior of the following cases.

I get annotations of annotated type via reflection API of the following 
parameter (or receiver with the same annotated type)
@Annot(2) MiniTest. @Annot(3) Inner parameter

The results are Ok for all cases except one - parameter of the 
constructor of Inner class.
Please take a look at the example bellow.

The output of the MiniTest run (on the jdk8-b116) will be
-------------------------------------------------------

MiniTest constructor parameter
- annotations count: 1
   Annot value: 3
Inner constructor parameter
- annotations count: 1
   Annot value: 2
InnerInner constructor receiver
- annotations count: 1
   Annot value: 1
InnerInner constructor parameter
- annotations count: 1
   Annot value: 3
-------------------------------------------------------

Looks like 'Annot value' for the 'Inner constructor parameter' case 
should be 3.

Thank you a lot,
Elena


public class MiniTest {

     public MiniTest(@Annot(2) MiniTest. @Annot(3) Inner param) {
     }

     class Inner {

         public Inner(@Annot(2) MiniTest. @Annot(3) Inner param) {
         }

         class InnerInner {
             public InnerInner(@Annot(0) MiniTest. @Annot(1) Inner 
Inner.this, @Annot(2) MiniTest. @Annot(3) Inner param) {
             }
         }
     }

     public static void main(String[] args) throws NoSuchMethodException {
         Constructor constructor = 
MiniTest.class.getConstructor(Inner.class);
         AnnotatedType paramType = 
constructor.getAnnotatedParameterTypes()[0];
         printInfo(paramType, "MiniTest constructor parameter");

         Constructor innerConstructor = 
Inner.class.getConstructor(MiniTest.class, Inner.class);
         AnnotatedType innerParamType = 
innerConstructor.getAnnotatedParameterTypes()[0];
         printInfo(innerParamType, "Inner constructor parameter");

         Constructor innerInnerConstructor = 
Inner.InnerInner.class.getConstructor(Inner.class, Inner.class);
         AnnotatedType innerInnerReceiverType = 
innerInnerConstructor.getAnnotatedReceiverType();
         printInfo(innerInnerReceiverType, "InnerInner constructor 
receiver");
         AnnotatedType innerInnerParamType = 
innerInnerConstructor.getAnnotatedParameterTypes()[0];
         printInfo(innerInnerParamType, "InnerInner constructor 
parameter");
     }

     private static void printInfo(AnnotatedType aType, String name) {
         System.out.println(name);
         System.out.println("- annotations count: " + 
aType.getAnnotations().length);
         System.out.println("  Annot value: " + ((Annot) 
aType.getAnnotations()[0]).value());
     }
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface Annot {
     int value();
}


More information about the type-annotations-spec-observers mailing list