Close

Java Reflection - Method.getAnnotatedExceptionTypes() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Method

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Methodjava.lang.reflect.MethodLogicBig

Method:

public AnnotatedType[] getAnnotatedExceptionTypes()

Returns an array of AnnotatedType objects that represent the use of types to specify the declared exceptions of the method. (see also Java 8 Type annotations)


Examples


package com.logicbig.example.method;

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 GetAnnotatedExceptionTypesExample {
private static void process() throws @Recoverable Exception {
}

public static void main(String... args) throws NoSuchMethodException {
Method m = GetAnnotatedExceptionTypesExample.class.getDeclaredMethod("process");
for (AnnotatedType annotatedType : m.getAnnotatedExceptionTypes()) {
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);
}

@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
private @interface Recoverable {}

}

Output

 ¦- Type = java.lang.Exception
¦- Annotations: [@com.logicbig.example.method.GetAnnotatedExceptionTypesExample$Recoverable()]
¦- Declared Annotations: [@com.logicbig.example.method.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.method;

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.Method;
import java.util.Arrays;

public class GetAnnotatedExceptionTypesExample2 {
private static <E extends IOException> void process() throws @Recoverable E {
}

public static void main(String... args) throws NoSuchMethodException {
Method m = GetAnnotatedExceptionTypesExample2.class.getDeclaredMethod("process");
for (AnnotatedType annotatedType : m.getAnnotatedExceptionTypes()) {
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);
}

@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
private @interface Recoverable {}
}

Output

 ¦- Type = E
¦- Annotations: [@com.logicbig.example.method.GetAnnotatedExceptionTypesExample2$Recoverable()]
¦- Declared Annotations: [@com.logicbig.example.method.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@2e8ff125]
¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@2e8ff125
¦- 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]




See Also