Code example for Method

Methods: getGenericParameterTypes, getGenericReturnType

0
    @SuppressWarnings("unchecked") 
    private static Class<? extends BoundedGenericMethods> clazz = BoundedGenericMethodsTests.BoundedGenericMethods.class;
 
    /** 
     * Tests whether the type parameter is upper bounded by BoundedGenericMethods. 
     * <T extends BoundedGenericMethods>. 
     *  
     * @param method 
     *            the declaring method 
     */ 
    private void checkBoundedTypeParameter(Method method) {
        TypeVariable<Method> typeParameter = getTypeParameter(method);
        assertEquals("T", typeParameter.getName());
        assertEquals(method, typeParameter.getGenericDeclaration());
 
        Type[] bounds = typeParameter.getBounds();
        assertLenghtOne(bounds);
        Type bound = bounds[0];
        assertEquals(BoundedGenericMethods.class, bound);
    } 
 
    /** 
     * Tests whether the specified method declares a parameter with the type of 
     * the type parameter. 
     *  
     * @param method 
     *            the declaring method 
     */ 
    private void parameterType(Method method) {
        TypeVariable<Method> typeParameter = getTypeParameter(method);
        assertLenghtOne(method.getGenericParameterTypes());
        Type genericParameterType = method.getGenericParameterTypes()[0];
        assertEquals(typeParameter, genericParameterType);
        assertTrue(genericParameterType instanceof TypeVariable);
        TypeVariable<?> typeVariable = (TypeVariable<?>) genericParameterType;
        assertEquals(method, typeVariable.getGenericDeclaration());
 
        Type[] paramBounds = typeVariable.getBounds();
        assertLenghtOne(paramBounds);
        Type paramBound = paramBounds[0];
        assertEquals(BoundedGenericMethods.class, paramBound);
    } 
 
    @SuppressWarnings("unchecked") 
    private void checkReturnType(Method method) {
        Type genericReturnType = method.getGenericReturnType();
        assertEquals(getTypeParameter(method), genericReturnType);
        assertTrue(genericReturnType instanceof TypeVariable);
 
        TypeVariable<Method> returnTypeVariable = (TypeVariable<Method>) genericReturnType;