Search in sources :

Example 76 with TypeVariable

use of java.lang.reflect.TypeVariable in project robovm by robovm.

the class OldGenericTypesTest method testInnerClassTest.

@SuppressWarnings("unchecked")
public void testInnerClassTest() throws Exception {
    Class<? extends InnerClassTest> clazz = InnerClassTest.class;
    TypeVariable<Class> typeVariable = getTypeParameter(clazz);
    Class<?>[] declaredClasses = clazz.getDeclaredClasses();
    assertLenghtOne(declaredClasses);
    Class<?> innerClazz = declaredClasses[0];
    assertEquals(InnerClassTest.InnerClass.class, innerClazz);
    //constructor
    Constructor<?>[] declaredConstructors = innerClazz.getDeclaredConstructors();
    assertLenghtOne(declaredConstructors);
    Constructor<?> declaredConstructor = declaredConstructors[0];
    Type[] genericParameterTypes = declaredConstructor.getGenericParameterTypes();
    assertLenghtOne(genericParameterTypes);
    assertEquals(typeVariable, genericParameterTypes[0]);
    assertInstanceOf(TypeVariable.class, genericParameterTypes[0]);
    TypeVariable<?> constructorTypeVariable = (TypeVariable<?>) genericParameterTypes[0];
    assertEquals(clazz, constructorTypeVariable.getGenericDeclaration());
    //method
    Method declaredMethods = innerClazz.getDeclaredMethod("innerMethod", Object.class);
    Type[] methodParameterTypes = declaredMethods.getGenericParameterTypes();
    assertLenghtOne(methodParameterTypes);
    assertEquals(typeVariable, methodParameterTypes[0]);
    assertInstanceOf(TypeVariable.class, methodParameterTypes[0]);
    TypeVariable<?> methodTypeVariable = (TypeVariable<?>) methodParameterTypes[0];
    assertEquals(clazz, methodTypeVariable.getGenericDeclaration());
}
Also used : Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Example 77 with TypeVariable

use of java.lang.reflect.TypeVariable in project robovm by robovm.

the class OldGenericTypesTest method testSimpleInheritance.

@SuppressWarnings("unchecked")
public void testSimpleInheritance() throws Exception {
    Class<? extends SimpleInheritance> clazz = SimpleInheritance.class;
    TypeVariable<Class> subTypeVariable = getTypeParameter(clazz);
    assertInstanceOf(ParameterizedType.class, clazz.getGenericSuperclass());
    ParameterizedType parameterizedSuperType = (ParameterizedType) clazz.getGenericSuperclass();
    assertInstanceOf(Class.class, parameterizedSuperType.getRawType());
    TypeVariable<Class> superTypeParameter = getTypeParameter((Class<?>) parameterizedSuperType.getRawType());
    TypeVariable<Class> typeParameter = getTypeParameter(GenericType.class);
    assertEquals(superTypeParameter, typeParameter);
    assertNotEquals(subTypeVariable, superTypeParameter);
    Type[] actualTypeArguments = parameterizedSuperType.getActualTypeArguments();
    assertLenghtOne(actualTypeArguments);
    assertInstanceOf(TypeVariable.class, actualTypeArguments[0]);
    TypeVariable actualSuperTypeVariable = (TypeVariable) actualTypeArguments[0];
    assertEquals(subTypeVariable, actualSuperTypeVariable);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Example 78 with TypeVariable

use of java.lang.reflect.TypeVariable in project robovm by robovm.

the class GenericExceptionsTest method testThrowingMethodThrowsEverything.

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()));
}
Also used : Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method) IOException(java.io.IOException)

Example 79 with TypeVariable

use of java.lang.reflect.TypeVariable in project robovm by robovm.

the class GenericExceptionsTest method testThrowingMethodTypeParameter.

public void testThrowingMethodTypeParameter() throws Exception {
    Method method = ThrowerT.class.getMethod("throwsMethodTypeParameter");
    TypeVariable typeVariable = getOnlyValue(method.getGenericExceptionTypes(), TypeVariable.class);
    assertEquals("X", typeVariable.getName());
    assertEquals(Arrays.<Type>asList(Exception.class), Arrays.asList(typeVariable.getBounds()));
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method) IOException(java.io.IOException)

Example 80 with TypeVariable

use of java.lang.reflect.TypeVariable in project robovm by robovm.

the class GenericExceptionsTest method testConstructorThrowingTypeVariable.

public void testConstructorThrowingTypeVariable() throws Exception {
    Constructor constructor = ThrowerT.class.getConstructor();
    TypeVariable typeVariable = getOnlyValue(constructor.getGenericExceptionTypes(), TypeVariable.class);
    assertEquals("T", typeVariable.getName());
    assertEquals(Arrays.<Type>asList(Throwable.class), Arrays.asList(typeVariable.getBounds()));
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) Constructor(java.lang.reflect.Constructor)

Aggregations

TypeVariable (java.lang.reflect.TypeVariable)218 Type (java.lang.reflect.Type)169 ParameterizedType (java.lang.reflect.ParameterizedType)168 GenericArrayType (java.lang.reflect.GenericArrayType)122 WildcardType (java.lang.reflect.WildcardType)100 Method (java.lang.reflect.Method)44 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)11 GenericDeclaration (java.lang.reflect.GenericDeclaration)8 List (java.util.List)8 Map (java.util.Map)8 ElementType (java.lang.annotation.ElementType)7 Field (java.lang.reflect.Field)7 IOException (java.io.IOException)6 AccessibleObject (java.lang.reflect.AccessibleObject)6 MediaType (javax.ws.rs.core.MediaType)6 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)6 TypeExtractionUtils.isClassType (org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType)6 Test (org.junit.Test)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5