Java Reflection Java Java API
Class:
java.lang.reflect.Method
Method:
public AnnotatedType getAnnotatedReturnType()
Returns an
AnnotatedType
object (containing information about Java 8 type annotations)
that represents the use of a type to specify the return type of the method.
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
Return type without any type annotation package com.logicbig.example.method;
import java.io.File; import java.lang.reflect.AnnotatedType; import java.lang.reflect.Method; import java.util.Arrays;
public class GetAnnotatedReturnTypeExample {
public File getFile() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- File getFile(){..};"); Method m = GetAnnotatedReturnTypeExample.class.getDeclaredMethod("getFile"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType); }
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 Method:- File getFile(){..}; Type: java.io.File Annotations: [] Declared Annotations: [] Annotated owner type: null AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Return type with type annotation: package com.logicbig.example.method;
import java.io.File; 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.Method; import java.util.Arrays;
public class GetAnnotatedReturnTypeExample2 {
@Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public @ReadOnly File getFile() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- @ReadOnly File getFile(){..};"); Method m = GetAnnotatedReturnTypeExample2.class.getDeclaredMethod("getFile"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType); }
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 Method:- @ReadOnly File getFile(){..}; Type: java.io.File Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample2$ReadOnly()] Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample2$ReadOnly()] 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.method;
import java.io.File; 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.Method; import java.util.Arrays;
public class GetAnnotatedReturnTypeExample3 { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public @ReadOnly File[] getFile() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- @ReadOnly File[] getFile(){..};"); Method m = GetAnnotatedReturnTypeExample3.class.getDeclaredMethod("getFile"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 AnnotatedArrayType) { printAnnotatedArrayType((AnnotatedArrayType) annotatedType, level); } }
private static void printAnnotatedArrayType(AnnotatedArrayType annotatedType, int level) { AnnotatedType aType = annotatedType.getAnnotatedGenericComponentType(); print(level, "AnnotatedArrayType#getAnnotatedGenericComponentType() : " + aType); printAnnotatedType(aType, level+1); }
private static void print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
OutputExample Method:- @ReadOnly File[] getFile(){..}; ¦- Type: java.io.File[] ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] ¦- AnnotatedArrayType#getAnnotatedGenericComponentType() : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@5636a99f ¦- Type: java.io.File ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample3$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample3$ReadOnly()] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Type Annotation on component type of an array package com.logicbig.example.method;
import java.io.File; 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.Method; import java.util.Arrays;
public class GetAnnotatedReturnTypeExample4 { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public File @ReadOnly [] getFile() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- File @ReadOnly[] getFile(){..};"); Method m = GetAnnotatedReturnTypeExample4.class.getDeclaredMethod("getFile"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 AnnotatedArrayType) { printAnnotatedArrayType((AnnotatedArrayType) annotatedType, level); } }
private static void printAnnotatedArrayType(AnnotatedArrayType annotatedType, int level) { AnnotatedType aType = annotatedType.getAnnotatedGenericComponentType(); print(level, "AnnotatedArrayType#getAnnotatedGenericComponentType() : " + aType); printAnnotatedType(aType, level + 1); }
private static void print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
OutputExample Method:- File @ReadOnly[] getFile(){..}; ¦- Type: java.io.File[] ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample4$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample4$ReadOnly()] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] ¦- AnnotatedArrayType#getAnnotatedGenericComponentType() : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@7549ca0e ¦- Type: java.io.File ¦- 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 multidimensional array package com.logicbig.example.method;
import java.io.File; 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.Method; import java.util.Arrays;
public class GetAnnotatedReturnTypeExample5 { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public File[] @ReadOnly [] getFile() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- File[] @ReadOnly[] getFile(){..};"); Method m = GetAnnotatedReturnTypeExample5.class.getDeclaredMethod("getFile"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 AnnotatedArrayType) { printAnnotatedArrayType((AnnotatedArrayType) annotatedType, level); } }
private static void printAnnotatedArrayType(AnnotatedArrayType annotatedType, int level) { AnnotatedType aType = annotatedType.getAnnotatedGenericComponentType(); print(level, "AnnotatedArrayType#getAnnotatedGenericComponentType() : " + aType); printAnnotatedType(aType, level + 1); }
private static void print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
OutputExample Method:- File[] @ReadOnly[] getFile(){..}; ¦- Type: java.io.File[][] ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] ¦- AnnotatedArrayType#getAnnotatedGenericComponentType() : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl@2054ed1 ¦- Type: java.io.File[] ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample5$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample5$ReadOnly()] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType] ¦- AnnotatedArrayType#getAnnotatedGenericComponentType() : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@6815a89e ¦- Type: java.io.File ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Type annotation on a generic type package com.logicbig.example.method;
import java.io.File; 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.Method; import java.util.Arrays;
public class GetAnnotatedReturnTypeExample6<T extends File> { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public @ReadOnly T getFile() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- File[] @ReadOnly[] getFile(){..};"); Method m = GetAnnotatedReturnTypeExample6.class.getDeclaredMethod("getFile"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 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 print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
OutputExample Method:- File[] @ReadOnly[] getFile(){..}; ¦- Type: T ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample6$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample6$ReadOnly()] ¦- 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@5a9d9229] ¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@5a9d9229 ¦- Type: java.io.File ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Type Annotation on generic array type package com.logicbig.example.method;
import java.io.File; 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.Method; import java.util.Arrays;
public class GetAnnotatedReturnTypeExample7<T extends File> { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public @ReadOnly T[] getFile() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- File[] @ReadOnly[] getFile(){..};"); Method m = GetAnnotatedReturnTypeExample7.class.getDeclaredMethod("getFile"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 AnnotatedArrayType) { printAnnotatedArrayType((AnnotatedArrayType) annotatedType, level); } if (annotatedType instanceof AnnotatedTypeVariable) { printAnnotatedTypeVariable((AnnotatedTypeVariable) annotatedType, level); } }
private static void printAnnotatedArrayType(AnnotatedArrayType annotatedType, int level) { AnnotatedType aType = annotatedType.getAnnotatedGenericComponentType(); print(level, "AnnotatedArrayType#getAnnotatedGenericComponentType() : " + aType); printAnnotatedType(aType, level + 1); }
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 print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
OutputExample Method:- File[] @ReadOnly[] getFile(){..}; ¦- 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] ¦- AnnotatedArrayType#getAnnotatedGenericComponentType() : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl@a6767dd ¦- Type: T ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample7$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample7$ReadOnly()] ¦- 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@280e9645] ¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@280e9645 ¦- Type: java.io.File ¦- 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.method;
import java.io.File; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.*; import java.util.Arrays; import java.util.List;
public class GetAnnotatedReturnTypeExample8<T extends File> { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public @ReadOnly List<T> getFiles() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- @ReadOnly List<T> getFiles(){..};"); Method m = GetAnnotatedReturnTypeExample8.class.getDeclaredMethod("getFiles"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 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 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 print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
OutputExample Method:- @ReadOnly List<T> getFiles(){..}; ¦- Type: java.util.List<T> ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample8$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample8$ReadOnly()] ¦- 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@44819621] ¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl@44819621 ¦- 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] ¦- AnnotatedTypeVariable#getAnnotatedBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@14fe19a] ¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@14fe19a ¦- Type: java.io.File ¦- 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.method;
import java.io.File; 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.Method; import java.util.Arrays; import java.util.Map;
public class GetAnnotatedReturnTypeExample9<K, T extends File> { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public @ReadOnly Map<K, @ReadOnly T> getFiles() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- @ReadOnly Map<K, @ReadOnly T> getFiles(){..};"); Method m = GetAnnotatedReturnTypeExample9.class.getDeclaredMethod("getFiles"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 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 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 print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
OutputExample Method:- @ReadOnly Map<K, @ReadOnly T> getFiles(){..}; ¦- Type: java.util.Map<K, T> ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample9$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample9$ReadOnly()] ¦- 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@7ec03c60, sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl@7ab88b43] ¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl@7ec03c60 ¦- Type: K ¦- Annotations: [] ¦- Declared Annotations: [] ¦- 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@3a73ff8c] ¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@3a73ff8c ¦- 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$AnnotatedTypeVariableImpl@7ab88b43 ¦- Type: T ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample9$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample9$ReadOnly()] ¦- 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@56ef6f7a] ¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@56ef6f7a ¦- Type: java.io.File ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
Type Annotation on return type involving wildcard generics package com.logicbig.example.method;
import java.io.File; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.*; import java.util.Arrays; import java.util.List;
public class GetAnnotatedReturnTypeExample10 { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public @ReadOnly List<? extends File> getFiles() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- @ReadOnly List<? extends File> getFiles(){..};"); Method m = GetAnnotatedReturnTypeExample10.class.getDeclaredMethod("getFiles"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 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 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); } }
OutputExample Method:- @ReadOnly List<? extends File> getFiles(){..}; ¦- Type: java.util.List<? extends java.io.File> ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample10$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample10$ReadOnly()] ¦- 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@41fe367b] ¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@41fe367b ¦- Type: ? extends java.io.File ¦- 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@7bd432f3] ¦- AnnotatedWildcardType#getAnnotatedUpperBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@7bd432f3 ¦- Type: java.io.File ¦- 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.method;
import java.io.File; 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.Method; import java.math.BigDecimal; import java.util.Arrays; import java.util.Map;
public class GetAnnotatedReturnTypeExample11 { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface ReadOnly {}
public Map<? super BigDecimal, @ReadOnly ? extends File> getFiles() {return null;}
public static void main(String... args) throws NoSuchMethodException { System.out.println("Example Method:- Map<? super BigDecimal, @ReadOnly ? extends File> getFiles(){..};"); Method m = GetAnnotatedReturnTypeExample11.class.getDeclaredMethod("getFiles"); AnnotatedType annotatedReturnType = m.getAnnotatedReturnType(); printAnnotatedType(annotatedReturnType, 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 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 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); } }
OutputExample Method:- Map<? super BigDecimal, @ReadOnly ? extends File> getFiles(){..}; ¦- Type: java.util.Map<? super java.math.BigDecimal, ? extends java.io.File> ¦- 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$AnnotatedWildcardTypeImpl@525fceef, sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@425f7c2c] ¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@525fceef ¦- 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] ¦- AnnotatedWildcardType#getAnnotatedLowerBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1a03d441] ¦- AnnotatedWildcardType#getAnnotatedLowerBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1a03d441 ¦- 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(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@3a2d6cf8] ¦- AnnotatedWildcardType#getAnnotatedUpperBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@3a2d6cf8 ¦- 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@425f7c2c ¦- Type: ? extends java.io.File ¦- Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample11$ReadOnly()] ¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReturnTypeExample11$ReadOnly()] ¦- 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@1629e5e4] ¦- AnnotatedWildcardType#getAnnotatedUpperBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1629e5e4 ¦- Type: java.io.File ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
|
|