Java Reflection Java Java API
Class:
java.lang.reflect.Constructor
Method:
public AnnotatedType[] getAnnotatedParameterTypes()
Returns an array of
AnnotatedType
objects that represent the use of types to specify formal parameter types of the constructor (see also Java 8 Type Annotations).
Examples
 package com.logicbig.example.constructor;
import java.io.Serializable; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedParameterizedType; import java.lang.reflect.AnnotatedType; import java.lang.reflect.AnnotatedTypeVariable; import java.lang.reflect.Constructor; import java.util.Arrays; import java.util.List;
public class GetAnnotatedParameterTypesExample {
public static class Test<T extends @ServerObject Serializable> { Test(List<@Immutable T> inputList) {} }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface Immutable {}
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ServerObject {}
public static void main(String... args) throws NoSuchMethodException { Constructor<Test> c = Test.class.getDeclaredConstructor(List.class); AnnotatedType[] a = c.getAnnotatedParameterTypes(); for (AnnotatedType t : a) { printAnnotatedType(t, 1); } }
private static void printAnnotatedType(AnnotatedType annotatedType, int level) { print(level, "Type = " + annotatedType.getType().getTypeName()); print(level, "Annotations: " + Arrays.toString(annotatedType.getAnnotations())); print(level, "Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 print(level, "Annotated owner type: " + annotatedOwnerType); print(level, "AnnotatedType class: " + annotatedType.getClass().getName()); print(level, "AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); if (annotatedType instanceof AnnotatedParameterizedType) { printAnnotatedParameterizedType((AnnotatedParameterizedType) annotatedType, level); } if (annotatedType instanceof AnnotatedTypeVariable) { printAnnotatedTypeVariable((AnnotatedTypeVariable) annotatedType, level); } }
private static void printAnnotatedTypeVariable(AnnotatedTypeVariable annotatedType, int level) { AnnotatedType[] aType = annotatedType.getAnnotatedBounds(); print(level, "AnnotatedTypeVariable#getAnnotatedBounds(): " + Arrays .toString(aType)); for (AnnotatedType type : aType) { print(level, "AnnotatedTypeVariable#bound : " + type); printAnnotatedType(type, level + 1); } }
private static void printAnnotatedParameterizedType(AnnotatedParameterizedType annotatedType, int level) { AnnotatedType[] aTypes = annotatedType.getAnnotatedActualTypeArguments(); print(level, "AnnotatedParameterizedType#getAnnotatedActualTypeArguments() : " + Arrays.toString(aTypes)); for (AnnotatedType aType : aTypes) { print(level, "AnnotatedParameterizedType#annotatedActualTypeArgument: " + aType); printAnnotatedType(aType, level + 1); } }
private static void print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); }
}
Output ¦- Type = java.util.List<T> ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType] ¦- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() : [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl@3a30a14a] ¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl@3a30a14a ¦- Type = T ¦- Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample$Immutable()] ¦- Declared Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample$Immutable()] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable] ¦- AnnotatedTypeVariable#getAnnotatedBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@67e1ac50] ¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@67e1ac50 ¦- Type = java.io.Serializable ¦- Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample$ServerObject()] ¦- Declared Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample$ServerObject()] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
 package com.logicbig.example.constructor;
import java.io.Serializable; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedParameterizedType; import java.lang.reflect.AnnotatedType; import java.lang.reflect.AnnotatedWildcardType; import java.lang.reflect.Constructor; import java.util.Arrays; import java.util.Map;
public class GetAnnotatedParameterTypesExample2 { public static class Test<T extends @ServerObject Serializable> { Test(@Immutable Map<? super @Immutable Key, ? extends @ServerObject RequestInfo> map) {} }
private static class RequestInfo {}
private static class Key {}
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface Immutable {}
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ServerObject {}
public static void main(String... args) throws NoSuchMethodException { Constructor<Test> c = Test.class.getDeclaredConstructor(Map.class); AnnotatedType[] a = c.getAnnotatedParameterTypes(); for (AnnotatedType annotatedType : a) { printAnnotatedType(annotatedType, 1); } }
private static void printAnnotatedType(AnnotatedType annotatedType, int level) { print(level, "Type = " + annotatedType.getType().getTypeName()); print(level, "Annotations: " + Arrays.toString(annotatedType.getAnnotations())); print(level, "Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 print(level, "Annotated owner type: " + annotatedOwnerType); print(level, "AnnotatedType class: " + annotatedType.getClass().getName()); print(level, "AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); if (annotatedType instanceof AnnotatedParameterizedType) { printAnnotatedParameterizedType((AnnotatedParameterizedType) annotatedType, level); }
if (annotatedType instanceof AnnotatedWildcardType) { printAnnotatedWildcardType((AnnotatedWildcardType) annotatedType, level); } }
private static void printAnnotatedParameterizedType(AnnotatedParameterizedType annotatedType, int level) { AnnotatedType[] aTypes = annotatedType.getAnnotatedActualTypeArguments(); print(level, "AnnotatedParameterizedType#getAnnotatedActualTypeArguments() : " + Arrays.toString(aTypes)); for (AnnotatedType aType : aTypes) { print(level, "AnnotatedParameterizedType#annotatedActualTypeArgument: " + aType); printAnnotatedType(aType, level + 1); } }
private static void printAnnotatedWildcardType(AnnotatedWildcardType annotatedWildcardType, int level) { AnnotatedType[] lBounds = annotatedWildcardType.getAnnotatedLowerBounds(); print(level, "AnnotatedWildcardType#getAnnotatedLowerBounds(): " + Arrays.toString(lBounds)); for (AnnotatedType lBound : lBounds) { print(level, "AnnotatedWildcardType#getAnnotatedLowerBound: " + lBound); printAnnotatedType(lBound, level + 1); }
AnnotatedType[] uBounds = annotatedWildcardType.getAnnotatedUpperBounds(); print(level, "AnnotatedWildcardType#getAnnotatedUpperBounds(): " + Arrays.toString(uBounds)); for (AnnotatedType uBound : uBounds) { print(level, "AnnotatedWildcardType#getAnnotatedUpperBound: " + uBound); printAnnotatedType(uBound, level + 1); } }
private static void print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
Output ¦- Type = java.util.Map<? super com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$Key, ? extends com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$RequestInfo> ¦- Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$Immutable()] ¦- Declared Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$Immutable()] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType] ¦- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() : [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@37cbb3ba, sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@4354341f] ¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@37cbb3ba ¦- Type = ? super com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$Key ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedWildcardType] ¦- AnnotatedWildcardType#getAnnotatedLowerBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@32165ff7] ¦- AnnotatedWildcardType#getAnnotatedLowerBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@32165ff7 ¦- Type = com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$Key ¦- Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$Immutable()] ¦- Declared Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$Immutable()] ¦- Annotated owner type: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@23f029af ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType] ¦- AnnotatedWildcardType#getAnnotatedUpperBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@49cc0808] ¦- AnnotatedWildcardType#getAnnotatedUpperBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@49cc0808 ¦- Type = java.lang.Object ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType] ¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@4354341f ¦- Type = ? extends com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$RequestInfo ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedWildcardType] ¦- AnnotatedWildcardType#getAnnotatedLowerBounds(): [] ¦- AnnotatedWildcardType#getAnnotatedUpperBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@2b5a29e2] ¦- AnnotatedWildcardType#getAnnotatedUpperBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@2b5a29e2 ¦- Type = com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$RequestInfo ¦- Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$ServerObject()] ¦- Declared Annotations: [@com.logicbig.example.constructor.GetAnnotatedParameterTypesExample2$ServerObject()] ¦- Annotated owner type: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@2bb33299 ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
|
|