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

elena votchennikova elena.votchennikova at oracle.com
Thu Nov 21 04:16:41 PST 2013


Please take a look at another case.

I've added constructor with InnerInner annotated parameter in every class.

-----------------------------------------------------------------------------------------
public class MiniTest {
     public MiniTest(@Annot(2) MiniTest. @Annot(3) Inner. @Annot(4) 
InnerInner param) {
     }

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

         class InnerInner {
             public InnerInner(@Annot(2) MiniTest. @Annot(3) Inner. 
@Annot(4) InnerInner param) {
             }
         }
     }
...
-----------------------------------------------------------------------------------------

And the result is strange too:
-----------------------------------------------------------------------------------------
MiniTest constructor1 parameter
- annotations count: 1
   Annot value: 4
Inner constructor1 parameter
- annotations count: 1
   Annot value: 2
InnerInner constructor1 parameter
- annotations count: 1
   Annot value: 3
-----------------------------------------------------------------------------------------

The full mini-test is attached.


Thanks a lot,
Elena


On 20.11.2013 16:06, elena votchennikova wrote:
> 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