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

The following examples show how to use java.lang.reflect.Method#getGenericExceptionTypes() . These examples are extracted from open source projects. 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 Project: businessworks   File: TypeLiteral.java    License: 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 Project: ldp4j   File: ClassDescription.java    License: 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 Project: crate   File: TypeLiteral.java    License: 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 Project: javageci   File: FluentBuilderImpl.java    License: 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 Project: j2objc   File: MethodTest.java    License: 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 Project: j2objc   File: GenericExceptionsTest.java    License: 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
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 Project: tomee   File: Logs.java    License: 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 + ">";
    }
}