Java Reflection Java Java API
Class:
java.lang.reflect.Field
Method:
public AnnotatedType getAnnotatedType()
Returns an
AnnotatedType
object that represent the use of Java 8 type annotations
on the fields.
The returned AnnotatedType instance can be an implementation of AnnotatedType itself
or an implementation of one of its sub-interfaces:
AnnotatedArrayType ,
AnnotatedParameterizedType ,
AnnotatedTypeVariable ,
AnnotatedWildcardType .
Examples
Simple type field package com.logicbig.example.field;
import java.lang.reflect.AnnotatedType; import java.lang.reflect.Field; import java.util.Arrays;
public class GetAnnotatedTypeExample { private int anInt;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- int anInt;"); Field field = GetAnnotatedTypeExample.class.getDeclaredField("anInt"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType); }
private static void printAnnotatedType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); } }
OutputExample:- int anInt; Type: int Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on type package com.logicbig.example.field;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedType; import java.lang.reflect.Field; import java.util.Arrays;
public class GetAnnotatedTypeExample2 { private @PrimeNumber int anInt;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- @PrimeNumber int anInt;"); Field field = GetAnnotatedTypeExample2.class.getDeclaredField("anInt"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType); }
private static void printAnnotatedType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface PrimeNumber {} }
OutputExample:- @PrimeNumber int anInt; Type: int Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample2$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample2$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on array type package com.logicbig.example.field;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedArrayType; import java.lang.reflect.AnnotatedType; import java.lang.reflect.Field; import java.util.Arrays;
public class GetAnnotatedTypeExample3 { private @PrimeNumber int[] anInt;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- @PrimeNumber int[] anInt;"); Field field = GetAnnotatedTypeExample3.class.getDeclaredField("anInt"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType);
if (annotatedType instanceof AnnotatedArrayType) { System.out.println("-- casting to AnnotatedArrayType --"); AnnotatedArrayType annotatedArrayType = (AnnotatedArrayType) annotatedType; System.out.println("-- AnnotatedArrayType#getAnnotatedGenericComponentType() --"); AnnotatedType annotatedType2 = annotatedArrayType.getAnnotatedGenericComponentType(); printAnnotatedType(annotatedType2); } }
private static void printAnnotatedType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface PrimeNumber {} }
OutputExample:- @PrimeNumber int[] anInt; Type: int[] Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] -- casting to AnnotatedArrayType -- -- AnnotatedArrayType#getAnnotatedGenericComponentType() -- Type: int Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample3$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample3$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on component type of int[] package com.logicbig.example.field;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedArrayType; import java.lang.reflect.AnnotatedType; import java.lang.reflect.Field; import java.util.Arrays;
public class GetAnnotatedTypeExample4 { private int @PrimeNumber [] anInt;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- int @PrimeNumber [] anInt;"); Field field = GetAnnotatedTypeExample4.class.getDeclaredField("anInt"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType);
if (annotatedType instanceof AnnotatedArrayType) { System.out.println("-- casting to AnnotatedArrayType --"); AnnotatedArrayType annotatedArrayType = (AnnotatedArrayType) annotatedType; System.out.println("-- AnnotatedArrayType.getAnnotatedGenericComponentType() --"); annotatedType = annotatedArrayType.getAnnotatedGenericComponentType(); printAnnotatedType(annotatedType); } }
private static void printAnnotatedType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface PrimeNumber {} }
OutputExample:- int @PrimeNumber [] anInt; Type: int[] Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample4$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample4$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] -- casting to AnnotatedArrayType -- -- AnnotatedArrayType.getAnnotatedGenericComponentType() -- Type: int Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on the component type of int[][] i.e. on int[] package com.logicbig.example.field;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedArrayType; import java.lang.reflect.AnnotatedType; import java.lang.reflect.Field; import java.util.Arrays;
public class GetAnnotatedTypeExample5 { private int[] @PrimeNumber [] anInt;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- int[] @PrimeNumber [] anInt;"); Field field = GetAnnotatedTypeExample5.class.getDeclaredField("anInt"); AnnotatedType annotatedType = field.getAnnotatedType(); printType(annotatedType);
if (annotatedType instanceof AnnotatedArrayType) { System.out.println("-- casting to AnnotatedArrayType --"); AnnotatedArrayType annotatedArrayType = (AnnotatedArrayType) annotatedType; System.out.println("-- AnnotatedArrayType.getAnnotatedGenericComponentType() --"); AnnotatedType annotatedType2 = annotatedArrayType.getAnnotatedGenericComponentType(); printType(annotatedType2);
if (annotatedType2 instanceof AnnotatedArrayType) { System.out.println("-- casting to AnnotatedArrayType --"); AnnotatedArrayType annotatedArrayType2 = (AnnotatedArrayType) annotatedType2; System.out.println("-- AnnotatedArrayType.getAnnotatedGenericComponentType() --"); AnnotatedType annotatedType3 = annotatedArrayType2.getAnnotatedGenericComponentType(); printType(annotatedType3); } } }
private static void printType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface PrimeNumber {} }
OutputExample:- int[] @PrimeNumber [] anInt; Type: int[][] Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] -- casting to AnnotatedArrayType -- -- AnnotatedArrayType.getAnnotatedGenericComponentType() -- Type: int[] Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample5$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample5$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] -- casting to AnnotatedArrayType -- -- AnnotatedArrayType.getAnnotatedGenericComponentType() -- Type: int Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on generic type package com.logicbig.example.field;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedType; import java.lang.reflect.AnnotatedTypeVariable; import java.lang.reflect.Field; import java.util.Arrays;
public class GetAnnotatedTypeExample6<T extends Number> { private @PrimeNumber T number;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- @PrimeNumber T number;"); Field field = GetAnnotatedTypeExample6.class.getDeclaredField("number"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType);
if(annotatedType instanceof AnnotatedTypeVariable) { System.out.println("-- casting to AnnotatedTypeVariable --"); AnnotatedTypeVariable annotatedTypeVariable = (AnnotatedTypeVariable) annotatedType; AnnotatedType[] annotatedBounds = annotatedTypeVariable.getAnnotatedBounds(); System.out.println("-- AnnotatedTypeVariable.getAnnotatedBounds() --"); for (AnnotatedType annotatedBound : annotatedBounds) { System.out.println("-- annotatedBound --"); printAnnotatedType(annotatedBound); } } }
private static void printAnnotatedType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) public @interface PrimeNumber {} }
OutputExample:- @PrimeNumber T number; Type: T Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample6$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample6$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable] -- casting to AnnotatedTypeVariable -- -- AnnotatedTypeVariable.getAnnotatedBounds() -- -- annotatedBound -- Type: java.lang.Number Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on generic array type package com.logicbig.example.field;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedArrayType; import java.lang.reflect.AnnotatedType; import java.lang.reflect.AnnotatedTypeVariable; import java.lang.reflect.Field; import java.util.Arrays;
public class GetAnnotatedTypeExample7<T extends Number> { private @PrimeNumber T[] number;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- int @PrimeNumber T[] number;"); Field field = GetAnnotatedTypeExample7.class.getDeclaredField("number"); AnnotatedType annotatedType = field.getAnnotatedType(); printType(annotatedType);
if(annotatedType instanceof AnnotatedArrayType) { System.out.println("-- casting to AnnotatedArrayType --"); AnnotatedArrayType arrayType = ((AnnotatedArrayType) annotatedType); System.out.println("-- AnnotatedArrayType#.getAnnotatedGenericComponentType() --"); AnnotatedType annotatedComponentType = arrayType.getAnnotatedGenericComponentType(); printType(annotatedComponentType);
if (annotatedComponentType instanceof AnnotatedTypeVariable) { System.out.println("-- casting to AnnotatedTypeVariable --"); AnnotatedTypeVariable annotatedTypeVariable = (AnnotatedTypeVariable) annotatedComponentType; System.out.println("-- AnnotatedTypeVariable#getAnnotatedBounds() --"); AnnotatedType[] annotatedBounds = annotatedTypeVariable.getAnnotatedBounds(); for (AnnotatedType annotatedBound : annotatedBounds) { System.out.println("-- annotatedBound --"); printType(annotatedBound); } } } }
private static void printType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces())); }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) public @interface PrimeNumber {} }
OutputExample:- int @PrimeNumber T[] number; Type: T[] Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] -- casting to AnnotatedArrayType -- -- AnnotatedArrayType#.getAnnotatedGenericComponentType() -- Type: T Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample7$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample7$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable] -- casting to AnnotatedTypeVariable -- -- AnnotatedTypeVariable#getAnnotatedBounds() -- -- annotatedBound -- Type: java.lang.Number Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on a type with generic package com.logicbig.example.field;
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.Field; import java.util.Arrays; import java.util.List; import java.util.Map;
public class GetAnnotatedTypeExample8<T extends Number, L extends List> { private @PrimeNumber Map<T, L> numbers;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- @PrimeNumber Map<T, L> numbers;"); Field field = GetAnnotatedTypeExample8.class.getDeclaredField("numbers"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType);
if (annotatedType instanceof AnnotatedParameterizedType) { System.out.println("-- casting to AnnotatedParameterizedType --"); AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType; System.out.println("-- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() --"); AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments(); for (AnnotatedType actualTypeArgument : actualTypeArguments) { System.out.println("-- actualTypeArgument --"); printAnnotatedType(actualTypeArgument); if (actualTypeArgument instanceof AnnotatedTypeVariable) { System.out.println("-- casting to AnnotatedTypeVariable --"); AnnotatedTypeVariable annotatedTypeVariable = (AnnotatedTypeVariable) actualTypeArgument; System.out.println("-- AnnotatedTypeVariable#getAnnotatedBounds() --"); AnnotatedType[] annotatedBounds = annotatedTypeVariable.getAnnotatedBounds(); for (AnnotatedType annotatedBound : annotatedBounds) { System.out.println("-- annotatedBound --"); printAnnotatedType(annotatedBound); } } } } }
private static void printAnnotatedType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces()));
if (annotatedOwnerType != null) { System.out.println("-- annotatedOwnerType --"); printAnnotatedType(annotatedOwnerType); } }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface PrimeNumber {} }
OutputExample:- @PrimeNumber Map<T, L> numbers; Type: java.util.Map<T, L> Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample8$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample8$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType] -- casting to AnnotatedParameterizedType -- -- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() -- -- actualTypeArgument -- Type: T Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable] -- casting to AnnotatedTypeVariable -- -- AnnotatedTypeVariable#getAnnotatedBounds() -- -- annotatedBound -- Type: java.lang.Number Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType] -- actualTypeArgument -- Type: L Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable] -- casting to AnnotatedTypeVariable -- -- AnnotatedTypeVariable#getAnnotatedBounds() -- -- annotatedBound -- Type: java.util.List Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on generic type parameter package com.logicbig.example.field;
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.Field; import java.util.Arrays; import java.util.List; import java.util.Map;
public class GetAnnotatedTypeExample9<T extends Number, L extends List> { private Map<@PrimeNumber T, L> numbers;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- Map<@PrimeNumber T, L> numbers;"); Field field = GetAnnotatedTypeExample9.class.getDeclaredField("numbers"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType); if (annotatedType instanceof AnnotatedParameterizedType) { System.out.println("-- casting to AnnotatedParameterizedType --"); AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType; System.out.println("-- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() --"); AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments(); for (AnnotatedType actualTypeArgument : actualTypeArguments) { System.out.println("-- actualTypeArgument --"); printAnnotatedType(actualTypeArgument); if (actualTypeArgument instanceof AnnotatedTypeVariable) { System.out.println("-- casting to AnnotatedTypeVariable --"); AnnotatedTypeVariable annotatedTypeVariable = (AnnotatedTypeVariable) actualTypeArgument; System.out.println("-- AnnotatedTypeVariable#getAnnotatedBounds() --"); AnnotatedType[] annotatedBounds = annotatedTypeVariable.getAnnotatedBounds(); for (AnnotatedType annotatedBound : annotatedBounds) { System.out.println("-- annotatedBound --"); printAnnotatedType(annotatedBound); } } } } }
private static void printAnnotatedType(AnnotatedType annotatedType) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations()));
AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces()));
if (annotatedOwnerType != null) { System.out.println("-- annotatedOwnerType --"); printAnnotatedType(annotatedOwnerType); } }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) @interface PrimeNumber {} }
OutputExample:- Map<@PrimeNumber T, L> numbers; Type: java.util.Map<T, L> Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType] -- casting to AnnotatedParameterizedType -- -- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() -- -- actualTypeArgument -- Type: T Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample9$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample9$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable] -- casting to AnnotatedTypeVariable -- -- AnnotatedTypeVariable#getAnnotatedBounds() -- -- annotatedBound -- Type: java.lang.Number Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType] -- actualTypeArgument -- Type: L Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable] -- casting to AnnotatedTypeVariable -- -- AnnotatedTypeVariable#getAnnotatedBounds() -- -- annotatedBound -- Type: java.util.List Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Annotation on type involving wildcard generics package com.logicbig.example.field;
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.Field; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; import java.util.Map;
public class GetAnnotatedTypeExample10 { private @PrimeNumber Map<? extends BigInteger, ? super BigDecimal> numbers;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- @PrimeNumber Map<? extends BigInteger, ? super BigDecimal> numbers;"); Field field = GetAnnotatedTypeExample10.class.getDeclaredField("numbers"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType);
if (annotatedType instanceof AnnotatedParameterizedType) { System.out.println("-- casting to AnnotatedParameterizedType --"); AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType; System.out.println("-- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() --"); AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments(); for (AnnotatedType actualTypeArgument : actualTypeArguments) { System.out.println("-- actualTypeArgument --"); printAnnotatedType(actualTypeArgument);
if (actualTypeArgument instanceof AnnotatedWildcardType) { System.out.println("-- casting to AnnotatedWildcardType --"); AnnotatedWildcardType wildcardType = (AnnotatedWildcardType) actualTypeArgument; System.out.println("-- AnnotatedWildcardType#getAnnotatedLowerBounds() --"); AnnotatedType[] lowerBounds = wildcardType.getAnnotatedLowerBounds(); printAnnotatedType(lowerBounds); System.out.println("-- AnnotatedWildcardType#getAnnotatedUpperBounds() --"); AnnotatedType[] upperBounds = wildcardType.getAnnotatedUpperBounds(); printAnnotatedType(upperBounds); } } } }
private static void printAnnotatedType(AnnotatedType... annotatedTypes) { for (AnnotatedType annotatedType : annotatedTypes) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces()));
if (annotatedOwnerType != null) { System.out.println("-- annotatedOwnerType --"); printAnnotatedType(annotatedOwnerType); } } }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface PrimeNumber {} }
OutputExample:- @PrimeNumber Map<? extends BigInteger, ? super BigDecimal> numbers; Type: java.util.Map<? extends java.math.BigInteger, ? super java.math.BigDecimal> Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample10$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample10$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType] -- casting to AnnotatedParameterizedType -- -- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() -- -- actualTypeArgument -- Type: ? extends java.math.BigInteger Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedWildcardType] -- casting to AnnotatedWildcardType -- -- AnnotatedWildcardType#getAnnotatedLowerBounds() -- -- AnnotatedWildcardType#getAnnotatedUpperBounds() -- Type: java.math.BigInteger Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType] -- actualTypeArgument -- Type: ? super java.math.BigDecimal Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedWildcardType] -- casting to AnnotatedWildcardType -- -- AnnotatedWildcardType#getAnnotatedLowerBounds() -- Type: java.math.BigDecimal Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType] -- AnnotatedWildcardType#getAnnotatedUpperBounds() -- 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]
Annotation on wildcard generics parameter package com.logicbig.example.field;
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.Field; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; import java.util.Map;
public class GetAnnotatedTypeExample11 { private Map<@PrimeNumber ? extends BigInteger, ? super BigDecimal> numbers;
public static void main(String... args) throws NoSuchFieldException { System.out.println("Example:- Map<@PrimeNumber ? extends BigInteger, ? super BigDecimal> numbers;"); Field field = GetAnnotatedTypeExample11.class.getDeclaredField("numbers"); AnnotatedType annotatedType = field.getAnnotatedType(); printAnnotatedType(annotatedType);
if (annotatedType instanceof AnnotatedParameterizedType) { System.out.println("-- casting to AnnotatedParameterizedType --"); AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType; System.out.println("-- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() --"); AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments(); for (AnnotatedType actualTypeArgument : actualTypeArguments) { System.out.println("-- actualTypeArgument --"); printAnnotatedType(actualTypeArgument);
if (actualTypeArgument instanceof AnnotatedWildcardType) { System.out.println("-- casting to AnnotatedWildcardType --"); AnnotatedWildcardType wildcardType = (AnnotatedWildcardType) actualTypeArgument; System.out.println("-- AnnotatedWildcardType#getAnnotatedLowerBounds() --"); AnnotatedType[] lowerBounds = wildcardType.getAnnotatedLowerBounds(); printAnnotatedType(lowerBounds); System.out.println("-- AnnotatedWildcardType#getAnnotatedUpperBounds() --"); AnnotatedType[] upperBounds = wildcardType.getAnnotatedUpperBounds(); printAnnotatedType(upperBounds); } } } }
private static void printAnnotatedType(AnnotatedType... annotatedTypes) { for (AnnotatedType annotatedType : annotatedTypes) { System.out.println("Type: " + annotatedType.getType().getTypeName()); System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations())); System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations())); AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9 System.out.println("Annotated owner type: " + annotatedOwnerType); System.out.println("AnnotatedType class: " + annotatedType.getClass().getName()); System.out.println("AnnotatedType class implementing interfaces: " + Arrays.toString(annotatedType.getClass().getInterfaces()));
if (annotatedOwnerType != null) { System.out.println("-- annotatedOwnerType --"); printAnnotatedType(annotatedOwnerType); } } }
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface PrimeNumber {} }
OutputExample:- Map<@PrimeNumber ? extends BigInteger, ? super BigDecimal> numbers; Type: java.util.Map<? extends java.math.BigInteger, ? super java.math.BigDecimal> Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType] -- casting to AnnotatedParameterizedType -- -- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() -- -- actualTypeArgument -- Type: ? extends java.math.BigInteger Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample11$PrimeNumber()] Declared Annotations: [@com.logicbig.example.field.GetAnnotatedTypeExample11$PrimeNumber()] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedWildcardType] -- casting to AnnotatedWildcardType -- -- AnnotatedWildcardType#getAnnotatedLowerBounds() -- -- AnnotatedWildcardType#getAnnotatedUpperBounds() -- Type: java.math.BigInteger Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType] -- actualTypeArgument -- Type: ? super java.math.BigDecimal Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedWildcardType] -- casting to AnnotatedWildcardType -- -- AnnotatedWildcardType#getAnnotatedLowerBounds() -- Type: java.math.BigDecimal Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType] -- AnnotatedWildcardType#getAnnotatedUpperBounds() -- 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]
|