package com.logicbig.example.method;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
public class GetGenericExceptionTypesExample {
private static class Processor {
private void init() {}
private void process() throws IOException {}
}
public static void main(String... args) throws NoSuchMethodException {
Method m = Processor.class.getDeclaredMethod("init");
Type[] t = m.getGenericExceptionTypes();
System.out.println(Arrays.toString(t));
Method m2 = Processor.class.getDeclaredMethod("process");
Type[] t2 = m2.getGenericExceptionTypes();
System.out.println(Arrays.toString(t2));
}
}
Output
[]
[class java.io.IOException]
package com.logicbig.example.method;
import java.io.IOException;
import java.lang.reflect.*;
import java.util.Arrays;
public class GetGenericExceptionTypesExample2 {
private static class Processor<E extends IOException> {
private void process() throws E {
}
}
public static void main(String... args) throws NoSuchMethodException {
Method m = Processor.class.getDeclaredMethod("process");
Type[] t = m.getGenericExceptionTypes();
System.out.println(Arrays.toString(t));
for (Type type : t) {
if (type instanceof TypeVariable) {
TypeVariable typeVar = (TypeVariable) type;
GenericDeclaration gd = typeVar.getGenericDeclaration();
System.out.println("GenericDeclaration: " + gd);
System.out.println("TypeVariable#getBounds():");
for (Type type1 : typeVar.getBounds()) {
System.out.println(type1);
}
}
}
}
}
Output
[E]
GenericDeclaration: class com.logicbig.example.method.GetGenericExceptionTypesExample2$Processor
TypeVariable#getBounds():
class java.io.IOException