Example usage for java.lang.reflect Method getExceptionTypes

List of usage examples for java.lang.reflect Method getExceptionTypes

Introduction

In this page you can find the example usage for java.lang.reflect Method getExceptionTypes.

Prototype

@Override
public Class<?>[] getExceptionTypes() 

Source Link

Usage

From source file:X.java

public static void main(String[] args) {
    try {//from  w w  w  .  j  a  va 2  s  .c o  m
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Class<?>[] exp = method.getExceptionTypes();
        for (Class anno : exp) {
            System.out.println(anno);
        }
        System.out.println();

    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:ReflectionUtils.java

/**
 * Determine whether the given method explicitly declares the given exception
 * or one of its superclasses, which means that an exception of that type
 * can be propagated as-is within a reflective invocation.
 * @param method the declaring method/*from   w ww. ja va2s .c  o m*/
 * @param exceptionType the exception to throw
 * @return <code>true</code> if the exception can be thrown as-is;
 * <code>false</code> if it needs to be wrapped
 */
public static boolean declaresException(Method method, Class exceptionType) {

    Class[] declaredExceptions = method.getExceptionTypes();
    for (int i = 0; i < declaredExceptions.length; i++) {
        Class declaredException = declaredExceptions[i];
        if (declaredException.isAssignableFrom(exceptionType)) {
            return true;
        }
    }
    return false;
}

From source file:ShowClass.java

public static void printMethodOrConstructor(Member member) {
    Class returntype = null, parameters[], exceptions[];
    if (member instanceof Method) {
        Method m = (Method) member;
        returntype = m.getReturnType();/*from www  . ja  v  a 2s . c o  m*/
        parameters = m.getParameterTypes();
        exceptions = m.getExceptionTypes();
        System.out.print(
                "  " + modifiers(member.getModifiers()) + typeName(returntype) + " " + member.getName() + "(");
    } else {
        Constructor c = (Constructor) member;
        parameters = c.getParameterTypes();
        exceptions = c.getExceptionTypes();
        System.out.print("  " + modifiers(member.getModifiers()) + typeName(c.getDeclaringClass()) + "(");
    }

    for (int i = 0; i < parameters.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typeName(parameters[i]));
    }
    System.out.print(")");
    if (exceptions.length > 0)
        System.out.print(" throws ");
    for (int i = 0; i < exceptions.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typeName(exceptions[i]));
    }
    System.out.println(";");
}

From source file:Main.java

public static void print_method_or_constructor(Member member) {
    Class returntype = null, parameters[], exceptions[];
    if (member instanceof Method) {
        Method m = (Method) member;
        returntype = m.getReturnType();/* w w  w.j a  v a2 s .c  o m*/
        parameters = m.getParameterTypes();
        exceptions = m.getExceptionTypes();
        System.out.print(
                "  " + modifiers(member.getModifiers()) + typename(returntype) + " " + member.getName() + "(");
    } else {
        Constructor c = (Constructor) member;
        parameters = c.getParameterTypes();
        exceptions = c.getExceptionTypes();
        System.out.print("  " + modifiers(member.getModifiers()) + typename(c.getDeclaringClass()) + "(");
    }
    for (int i = 0; i < parameters.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(parameters[i]));
    }
    System.out.print(")");
    if (exceptions.length > 0)
        System.out.print(" throws ");
    for (int i = 0; i < exceptions.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(exceptions[i]));
    }
    System.out.println(";");
}

From source file:com.infinities.skyport.timeout.ServiceProviderTimeLimiter.java

private static boolean declaresInterruptedEx(Method method) {
    for (Class<?> exType : method.getExceptionTypes()) {
        // debate: == or isAssignableFrom?
        if (exType == InterruptedException.class) {
            return true;
        }/*w w w.ja v  a2 s  .  c  o m*/
    }
    return false;
}

From source file:ShowClass.java

/**
 * Print the modifiers, return type, name, parameter types and exception type
 * of a method or constructor. Note the use of the Member interface to allow
 * this method to work with both Method and Constructor objects
 *//* w w  w  .  j av  a2 s .  c  o  m*/
public static void print_method_or_constructor(Member member) {
    Class returntype = null, parameters[], exceptions[];
    if (member instanceof Method) {
        Method m = (Method) member;
        returntype = m.getReturnType();
        parameters = m.getParameterTypes();
        exceptions = m.getExceptionTypes();
        System.out.print(
                "  " + modifiers(member.getModifiers()) + typename(returntype) + " " + member.getName() + "(");
    } else {
        Constructor c = (Constructor) member;
        parameters = c.getParameterTypes();
        exceptions = c.getExceptionTypes();
        System.out.print("  " + modifiers(member.getModifiers()) + typename(c.getDeclaringClass()) + "(");
    }

    for (int i = 0; i < parameters.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(parameters[i]));
    }
    System.out.print(")");
    if (exceptions.length > 0)
        System.out.print(" throws ");
    for (int i = 0; i < exceptions.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(exceptions[i]));
    }
    System.out.println(";");
}

From source file:be.fgov.kszbcss.rhq.websphere.mbean.MBeanClient.java

private static boolean throwsException(Method method, Class<?> exceptionType) {
    for (Class<?> candidate : method.getExceptionTypes()) {
        if (candidate.isAssignableFrom(exceptionType)) {
            return true;
        }/*  w  w w .  j a  v a2  s  .c  o m*/
    }
    return false;
}

From source file:Main.java

/**
 * Check if the given <code>java.lang.Throwable</code> is a declared
 * throwable of the given method./*from   w ww  . ja  v a  2s  .  c o  m*/
 * @param p_e The trowable.
 * @param p_method The method.
 * @return <code>true</code> if <code>p_method</code> declares to throw
 *    <code>p_e</code>, or <code>false</code> otherwise.
 * @throws IllegalArgumentException
 *    If either of the method parameters is <code>null</code>.
 */
public static boolean isDeclaredThrowable(Throwable p_e, Method p_method) {
    final Class<? extends Throwable> l_clsThrowable;

    if (p_e == null)
        throw new IllegalArgumentException("No throwable.");

    if (p_method == null)
        throw new IllegalArgumentException("No method.");

    l_clsThrowable = p_e.getClass();

    for (final Class<?> l_clsException : p_method.getExceptionTypes()) {
        if (l_clsException.isAssignableFrom(l_clsThrowable))
            return true;
    }

    return false;
}

From source file:Mopex.java

/**
 * Returns a String that represents the suffix of the header of a method.
 * The suffix of a header is not a standard Java term. We use the term to
 * mean the Java header without the method modifiers.
 * /*from  w w  w .ja  v  a  2 s  .c  o  m*/
 * @return String
 * @param m
 *            java.lang.Method
 */
//start extract headerToString
public static String headerSuffixToString(Method m) {
    String header = getTypeName(m.getReturnType()) + " " + signatureToString(m);
    Class[] eTypes = m.getExceptionTypes();
    if (eTypes.length != 0) {
        header += " throws " + classArrayToString(eTypes);
    }
    return header;
}

From source file:Mopex.java

/**
 * Turns true if and only if the header suffixes of the two specified
 * methods are equal. The header suffix is defined to be the signature, the
 * return type, and the exception types.
 * /*from   w  ww  .ja v  a2 s.co  m*/
 * @return boolean
 * @param m1
 *            java.lang.Method
 * @param m2
 *            java.lang.Method
 */
public static boolean equalsHeaderSuffixes(Method m1, Method m2) {
    if (m1.getReturnType() != m2.getReturnType())
        return false;
    if (!Arrays.equals(m1.getExceptionTypes(), m2.getExceptionTypes()))
        return false;
    return equalSignatures(m1, m2);
}