Tests if a method throws a checked exception from enterprise annotation method. - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

Tests if a method throws a checked exception from enterprise annotation method.

Demo Code


import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedParameter;
import javax.enterprise.inject.spi.AnnotatedType;

public class Main{
    /**/*w ww.  j av a 2 s  .  co m*/
     * Tests if a method throws a checked exception.
     */
    public static boolean hasException(AnnotatedMethod<?> method,
            Class<?> exn) {
        Class<?>[] methodExceptions = method.getJavaMember()
                .getExceptionTypes();

        for (int j = 0; j < methodExceptions.length; j++) {
            if (methodExceptions[j].isAssignableFrom(exn))
                return true;
        }

        return false;
    }
}

Related Tutorials