Search in sources :

Example 26 with TypeVariable

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

the class OldGenericReflectionCornerCases method testMultipleBoundedWildcard.

@SuppressWarnings("unchecked")
public void testMultipleBoundedWildcard() throws Exception {
    Class<? extends MultipleBoundedWildcardEquality> clazz = MultipleBoundedWildcardEquality.class;
    Method method = clazz.getDeclaredMethod("multipleBoundedWildcardEquality", Pair.class);
    TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
    assertLenghtOne(typeParameters);
    TypeVariable<?> typeParameter = typeParameters[0];
    Type[] typeParameterBounds = typeParameter.getBounds();
    assertEquals(2, typeParameterBounds.length);
    assertEquals(Object.class, typeParameterBounds[0]);
    assertInstanceOf(ParameterizedType.class, typeParameterBounds[1]);
    ParameterizedType parameterizedType = (ParameterizedType) typeParameterBounds[1];
    assertEquals(Comparable.class, parameterizedType.getRawType());
    Type[] typeArguments = parameterizedType.getActualTypeArguments();
    assertLenghtOne(typeArguments);
    assertInstanceOf(ParameterizedType.class, typeArguments[0]);
    ParameterizedType type = (ParameterizedType) typeArguments[0];
    assertEquals(typeParameter, type.getActualTypeArguments()[0]);
    assertEquals(MultipleBoundedWildcardEquality.class, type.getRawType());
    Type[] parameterTypes = method.getGenericParameterTypes();
    assertLenghtOne(parameterTypes);
    Type parameter = parameterTypes[0];
    assertInstanceOf(ParameterizedType.class, parameter);
    ParameterizedType paramType = (ParameterizedType) parameter;
    Type[] actualTypeArguments = paramType.getActualTypeArguments();
    assertEquals(2, actualTypeArguments.length);
    Type firstArgument = actualTypeArguments[0];
    assertInstanceOf(WildcardType.class, firstArgument);
    WildcardType firstWildcardArgument = (WildcardType) firstArgument;
    Type secondArgument = actualTypeArguments[1];
    assertInstanceOf(WildcardType.class, secondArgument);
    WildcardType secondWildcardArgument = (WildcardType) secondArgument;
    assertEquals(firstWildcardArgument, secondWildcardArgument);
    Type[] firstWildcardArgumentUpperBounds = firstWildcardArgument.getUpperBounds();
    assertLenghtOne(firstWildcardArgumentUpperBounds);
    Type firstWildcardArgumentUpperBoundsType = firstWildcardArgumentUpperBounds[0];
    Type[] secondWildcardArgumentUpperBounds = secondWildcardArgument.getUpperBounds();
    assertLenghtOne(secondWildcardArgumentUpperBounds);
    Type secondWildcardArgumentLoweroundsType = secondWildcardArgumentUpperBounds[0];
    assertEquals(firstWildcardArgumentUpperBoundsType, secondWildcardArgumentLoweroundsType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method)

Example 27 with TypeVariable

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

the class OldGenericTypesTest method testMultipleGenericTypes.

@SuppressWarnings("unchecked")
public void testMultipleGenericTypes() throws Exception {
    //Type parameters
    Class<? extends MultipleGenericTypes> clazz = MultipleGenericTypes.class;
    TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
    assertEquals(2, typeParameters.length);
    TypeVariable<?> typeVariableT = typeParameters[0];
    assertEquals(clazz, typeVariableT.getGenericDeclaration());
    assertEquals("T", typeVariableT.getName());
    TypeVariable<?> typeVariableS = typeParameters[1];
    assertEquals("S", typeVariableS.getName());
    assertEquals(clazz, typeVariableS.getGenericDeclaration());
    // multipleGenericTypesT
    Method multipleGenericTypesT = clazz.getDeclaredMethod("multipleGenericTypesT", new Class[] { Object.class });
    Type[] multipleGenericTypesTTypes = multipleGenericTypesT.getGenericParameterTypes();
    assertLenghtOne(multipleGenericTypesTTypes);
    Type multipleGenericTypesTType = multipleGenericTypesTTypes[0];
    assertEquals(typeVariableT, multipleGenericTypesTType);
    // multipleGenericTypesS
    Method multipleGenericTypesS = clazz.getDeclaredMethod("multipleGenericTypesS", new Class[] { Object.class });
    Type[] multipleGenericTypesSTypes = multipleGenericTypesS.getGenericParameterTypes();
    assertLenghtOne(multipleGenericTypesSTypes);
    Type multipleGenericTypesSType = multipleGenericTypesSTypes[0];
    assertEquals(typeVariableS, multipleGenericTypesSType);
    // multipleGenericTypesS
    Method multipleGenericTypesTS = clazz.getDeclaredMethod("multipleGenericTypesTS", new Class[] { Object.class, Object.class });
    Type[] multipleGenericTypesTSTypes = multipleGenericTypesTS.getGenericParameterTypes();
    assertEquals(2, multipleGenericTypesTSTypes.length);
    Type multipleGenericTypesTSTypeT = multipleGenericTypesTSTypes[0];
    assertEquals(typeVariableT, multipleGenericTypesTSTypeT);
    Type multipleGenericTypesTSTypeS = multipleGenericTypesTSTypes[1];
    assertEquals(typeVariableS, multipleGenericTypesTSTypeS);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method)

Example 28 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 29 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 30 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)

Aggregations

TypeVariable (java.lang.reflect.TypeVariable)424 ParameterizedType (java.lang.reflect.ParameterizedType)342 Type (java.lang.reflect.Type)341 GenericArrayType (java.lang.reflect.GenericArrayType)251 WildcardType (java.lang.reflect.WildcardType)191 Method (java.lang.reflect.Method)63 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)23 GenericDeclaration (java.lang.reflect.GenericDeclaration)19 Test (org.junit.Test)16 Field (java.lang.reflect.Field)15 List (java.util.List)13 Map (java.util.Map)11 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)11 TypeExtractionUtils.isClassType (org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType)11 Member (java.lang.reflect.Member)10 MediaType (javax.ws.rs.core.MediaType)10 CaptureType (com.googlecode.gentyref.CaptureType)8 ElementType (java.lang.annotation.ElementType)8 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)8