Java Reflection Java Java API
Class:
java.lang.reflect.Constructor
Method:
public AnnotatedType[] getAnnotatedExceptionTypes()
Returns an array of
AnnotatedType
objects that represent the use of types to specify the declared exceptions of the constructor. (see also
Java 8 Type annotations)
Examples
package com.logicbig.example.constructor;
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.Constructor; import java.util.Arrays;
public class GetAnnotatedExceptionTypesExample { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface Recoverable { }
private static class Test { public Test() throws @Recoverable Exception {} }
public static void main(String... args) throws NoSuchMethodException { Constructor<Test> c = Test.class.getDeclaredConstructor(null); AnnotatedType[] annotatedReturnType = c.getAnnotatedExceptionTypes(); for (AnnotatedType annotatedType : annotatedReturnType) { 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())); }
private static void print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); }
}
Output ¦- Type = java.lang.Exception ¦- Annotations: [@com.logicbig.example.constructor.GetAnnotatedExceptionTypesExample$Recoverable()] ¦- Declared Annotations: [@com.logicbig.example.constructor.GetAnnotatedExceptionTypesExample$Recoverable()] ¦- 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.IOException; 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.Constructor; import java.util.Arrays;
public class GetAnnotatedExceptionTypesExample2 { @Target({ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) private @interface Recoverable { }
private static class Test { public <E extends IOException> Test() throws @Recoverable E {} }
public static void main(String... args) throws NoSuchMethodException { Constructor<Test> c = Test.class.getDeclaredConstructor(null); AnnotatedType[] annotatedReturnType = c.getAnnotatedExceptionTypes(); for (AnnotatedType annotatedType : annotatedReturnType) { 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 AnnotatedTypeVariable) { printAnnotatedTypeVariable((AnnotatedTypeVariable) annotatedType, 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); } }
Output ¦- Type = E ¦- Annotations: [@com.logicbig.example.constructor.GetAnnotatedExceptionTypesExample2$Recoverable()] ¦- Declared Annotations: [@com.logicbig.example.constructor.GetAnnotatedExceptionTypesExample2$Recoverable()] ¦- 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@57525cae] ¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@57525cae ¦- Type = java.io.IOException ¦- Annotations: [] ¦- Declared Annotations: [] ¦- Annotated owner type: null ¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl ¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
|
|