Java Code Examples for java.lang.reflect.Method#getGenericExceptionTypes()

The following examples show how to use java.lang.reflect.Method#getGenericExceptionTypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeLiteral.java    From businessworks with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the resolved generic exception types thrown by {@code constructor}.
 *
 * @param methodOrConstructor a method or constructor defined by this or any supertype.
 * @since 2.0
 */
public List<TypeLiteral<?>> getExceptionTypes(Member methodOrConstructor) {
  Type[] genericExceptionTypes;

  if (methodOrConstructor instanceof Method) {
    Method method = (Method) methodOrConstructor;
    checkArgument(method.getDeclaringClass().isAssignableFrom(rawType),
        "%s is not defined by a supertype of %s", method, type);
    genericExceptionTypes = method.getGenericExceptionTypes();

  } else if (methodOrConstructor instanceof Constructor) {
    Constructor<?> constructor = (Constructor<?>) methodOrConstructor;
    checkArgument(constructor.getDeclaringClass().isAssignableFrom(rawType),
        "%s does not construct a supertype of %s", constructor, type);
    genericExceptionTypes = constructor.getGenericExceptionTypes();

  } else {
    throw new IllegalArgumentException("Not a method or a constructor: " + methodOrConstructor);
  }

  return resolveAll(genericExceptionTypes);
}
 
Example 2
Source File: ClassDescription.java    From ldp4j with Apache License 2.0 6 votes vote down vote up
@Override
protected void processType(Method type) {
	super.addTitle("Method");
	super.addBlockField("member", wrapElementAs(type,Member.class));
	super.addBlockField("generic declaration", wrapElementAs(type,GenericDeclaration.class));
	super.addBlockField("annotated element", wrapElementAs(type,AnnotatedElement.class));
	super.addField("accessible", type.isAccessible());
	super.addField("return type",type.getReturnType());
	super.addField("generic return type",type.getGenericReturnType());
	super.addMultiField("parameter types",type.getParameterTypes());
	super.addMultiField("generic parameter types",type.getGenericParameterTypes());
	Annotation[][] parameterAnnotations = type.getParameterAnnotations();
	List<String> annots=new ArrayList<String>();
	for(Annotation[] pa:parameterAnnotations) {
		annots.add(Arrays.toString(pa));
	}
	super.addMultiField("parameter annotations",annots);
	super.addField("bridge", type.isBridge());
	super.addField("var args",type.isVarArgs());
	super.addMultiField("exception types",type.getExceptionTypes());
	super.addMultiField("generic exception types",type.getGenericExceptionTypes());
}
 
Example 3
Source File: TypeLiteral.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the resolved generic exception types thrown by {@code constructor}.
 *
 * @param methodOrConstructor a method or constructor defined by this or any supertype.
 * @since 2.0
 */
public List<TypeLiteral<?>> getExceptionTypes(Member methodOrConstructor) {
    Type[] genericExceptionTypes;

    if (methodOrConstructor instanceof Method) {
        Method method = (Method) methodOrConstructor;
        if (!method.getDeclaringClass().isAssignableFrom(rawType)) {
            throw new IllegalArgumentException(method + " is not defined by a supertype of " + type);
        }

        genericExceptionTypes = method.getGenericExceptionTypes();

    } else if (methodOrConstructor instanceof Constructor) {
        Constructor<?> constructor = (Constructor<?>) methodOrConstructor;
        if (!constructor.getDeclaringClass().isAssignableFrom(rawType)) {
            throw new IllegalArgumentException(constructor + " does not construct a supertype of " + type);
        }
        genericExceptionTypes = constructor.getGenericExceptionTypes();

    } else {
        throw new IllegalArgumentException("Not a method or a constructor: " + methodOrConstructor);
    }

    return resolveAll(genericExceptionTypes);
}
 
Example 4
Source File: FluentBuilderImpl.java    From javageci with Apache License 2.0 5 votes vote down vote up
@Override
public FluentBuilder cloner(String method) {
    assertThatMethodExistsInTheClass(method);
    Method clonerMethod = methods.get(method);
    if (clonerMethod.getGenericExceptionTypes().length > 0) {
        throw new GeciException("The cloner method should not have parameters");
    }
    if (clonerMethod.getReturnType() != klass) {
        throw new GeciException("The cloner method should return the type of the class it is in.");
    }
    final var next = copy();
    next.cloner = clonerMethod;
    return next;
}
 
Example 5
Source File: MethodTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.lang.reflect.Method#getDefaultValue()
 */
public void test_getGenericExceptionTypes() throws Exception {
    Method method = ExceptionTest.class.getDeclaredMethod("exceptionTest");
    Type[] genericExceptionTypes = method.getGenericExceptionTypes();
    assertEquals(1, genericExceptionTypes.length);
    assertTrue(genericExceptionTypes[0] instanceof TypeVariable<?>);
    @SuppressWarnings("unchecked")
    TypeVariable<Class<ExceptionTest<?>>> tv =
        (TypeVariable<Class<ExceptionTest<?>>>) genericExceptionTypes[0];
    assertEquals("T", tv.getName());
}
 
Example 6
Source File: GenericExceptionsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testThrowingMethodThrowsEverything() throws Exception {
    Method method = ThrowerT.class.getMethod("throwsEverything");
    Type[] exceptions = method.getGenericExceptionTypes();
    TypeVariable t = (TypeVariable) exceptions[0];
    assertEquals(3, exceptions.length);
    assertEquals("T", t.getName());
    assertEquals(Arrays.<Type>asList(Throwable.class), Arrays.asList(t.getBounds()));
    assertEquals(Exception.class, exceptions[1]);
    TypeVariable x = (TypeVariable) exceptions[2];
    assertEquals("X", x.getName());
    assertEquals(Arrays.<Type>asList(Exception.class), Arrays.asList(x.getBounds()));
}
 
Example 7
Source File: CxfCdiAutoSetup.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
private static String toSimpleString(final Method mtd) {
    try {
        final StringBuilder sb = new StringBuilder();
        final Type[] typeparms = mtd.getTypeParameters();
        if (typeparms.length > 0) {
            boolean first = true;
            sb.append("<");
            for (Type typeparm : typeparms) {
                if (!first) {
                    sb.append(",");
                }
                sb.append(name(typeparm));
                first = false;
            }
            sb.append("> ");
        }

        final Type genRetType = mtd.getGenericReturnType();
        sb.append(name(genRetType)).append(" ");
        sb.append(mtd.getName()).append("(");
        final Type[] params = mtd.getGenericParameterTypes();
        for (int j = 0; j < params.length; j++) {
            sb.append(name(params[j]));
            if (j < (params.length - 1)) {
                sb.append(", ");
            }
        }
        sb.append(")");
        final Type[] exceptions = mtd.getGenericExceptionTypes();
        if (exceptions.length > 0) {
            sb.append(" throws ");
            for (int k = 0; k < exceptions.length; k++) {
                sb.append(name(exceptions[k]));
                if (k < (exceptions.length - 1)) {
                    sb.append(",");
                }
            }
        }
        return sb.toString();
    } catch (final Exception e) {
        return "<" + e + ">";
    }
}
 
Example 8
Source File: Logs.java    From tomee with Apache License 2.0 4 votes vote down vote up
public static String toSimpleString(final Method mtd) {
    try {
        final StringBuilder sb = new StringBuilder();
        final Type[] typeparms = mtd.getTypeParameters();
        if (typeparms.length > 0) {
            boolean first = true;
            sb.append("<");
            for (Type typeparm : typeparms) {
                if (!first) {
                    sb.append(",");
                }
                sb.append(name(typeparm));
                first = false;
            }
            sb.append("> ");
        }

        final Type genRetType = mtd.getGenericReturnType();
        sb.append(name(genRetType)).append(" ");
        sb.append(mtd.getName()).append("(");
        final Type[] params = mtd.getGenericParameterTypes();
        for (int j = 0; j < params.length; j++) {
            sb.append(name(params[j]));
            if (j < (params.length - 1)) {
                sb.append(", ");
            }
        }
        sb.append(")");
        final Type[] exceptions = mtd.getGenericExceptionTypes();
        if (exceptions.length > 0) {
            sb.append(" throws ");
            for (int k = 0; k < exceptions.length; k++) {
                sb.append(name(exceptions[k]));
                if (k < (exceptions.length - 1)) {
                    sb.append(",");
                }
            }
        }
        return sb.toString();
    } catch (Exception e) {
        return "<" + e + ">";
    }
}