Close

Java Reflection - Method.getGenericExceptionTypes() 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 Type[] getGenericExceptionTypes()

Returns an array of Type objects that represent the exceptions declared to be thrown by this method.


Examples


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




See Also