Search in sources :

Example 21 with TypeVariable

use of java.lang.reflect.TypeVariable in project android_frameworks_base by AOSPA.

the class TypeReference method toString.

private static void toString(Type type, StringBuilder out) {
    if (type == null) {
        return;
    } else if (type instanceof TypeVariable<?>) {
        // T
        out.append(((TypeVariable<?>) type).getName());
    } else if (type instanceof Class<?>) {
        Class<?> klass = (Class<?>) type;
        out.append(klass.getName());
        toString(klass.getTypeParameters(), out);
    } else if (type instanceof ParameterizedType) {
        // "Foo<T1, T2, T3, ... Tn>"
        ParameterizedType p = (ParameterizedType) type;
        out.append(((Class<?>) p.getRawType()).getName());
        toString(p.getActualTypeArguments(), out);
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        toString(gat.getGenericComponentType(), out);
        out.append("[]");
    } else {
        // WildcardType, BoundedType
        // TODO:
        out.append(type.toString());
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 22 with TypeVariable

use of java.lang.reflect.TypeVariable in project spock by spockframework.

the class GenericTypeReflector method getExactDirectSuperTypes.

/**
	 * Returns the direct supertypes of the given type. Resolves type parameters.
	 */
private static Type[] getExactDirectSuperTypes(Type type) {
    if (type instanceof ParameterizedType || type instanceof Class) {
        Class<?> clazz;
        if (type instanceof ParameterizedType) {
            clazz = (Class<?>) ((ParameterizedType) type).getRawType();
        } else {
            clazz = (Class<?>) type;
            if (clazz.isArray())
                return getArrayExactDirectSuperTypes(clazz);
        }
        Type[] superInterfaces = clazz.getGenericInterfaces();
        Type superClass = clazz.getGenericSuperclass();
        Type[] result;
        int resultIndex;
        if (superClass == null) {
            result = new Type[superInterfaces.length];
            resultIndex = 0;
        } else {
            result = new Type[superInterfaces.length + 1];
            resultIndex = 1;
            result[0] = mapTypeParameters(superClass, type);
        }
        for (Type superInterface : superInterfaces) {
            result[resultIndex++] = mapTypeParameters(superInterface, type);
        }
        return result;
    } else if (type instanceof TypeVariable) {
        TypeVariable<?> tv = (TypeVariable<?>) type;
        return tv.getBounds();
    } else if (type instanceof WildcardType) {
        // But it does happen if the upper bound of a type variable contains a wildcard
        return ((WildcardType) type).getUpperBounds();
    } else if (type instanceof CaptureType) {
        return ((CaptureType) type).getUpperBounds();
    } else if (type instanceof GenericArrayType) {
        return getArrayExactDirectSuperTypes(type);
    } else {
        throw new RuntimeException("not implemented type: " + type);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 23 with TypeVariable

use of java.lang.reflect.TypeVariable in project spock by spockframework.

the class GenericTypeReflector method capture.

/**
	 * Applies capture conversion to the given type.
	 */
public static Type capture(Type type) {
    VarMap varMap = new VarMap();
    List<CaptureTypeImpl> toInit = new ArrayList<CaptureTypeImpl>();
    if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        Class<?> clazz = (Class<?>) pType.getRawType();
        Type[] arguments = pType.getActualTypeArguments();
        TypeVariable<?>[] vars = clazz.getTypeParameters();
        Type[] capturedArguments = new Type[arguments.length];
        assert arguments.length == vars.length;
        for (int i = 0; i < arguments.length; i++) {
            Type argument = arguments[i];
            if (argument instanceof WildcardType) {
                CaptureTypeImpl captured = new CaptureTypeImpl((WildcardType) argument, vars[i]);
                argument = captured;
                toInit.add(captured);
            }
            capturedArguments[i] = argument;
            varMap.add(vars[i], argument);
        }
        for (CaptureTypeImpl captured : toInit) {
            captured.init(varMap);
        }
        Type ownerType = (pType.getOwnerType() == null) ? null : capture(pType.getOwnerType());
        return new ParameterizedTypeImpl(clazz, capturedArguments, ownerType);
    } else {
        return type;
    }
}
Also used : ArrayList(java.util.ArrayList) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable)

Example 24 with TypeVariable

use of java.lang.reflect.TypeVariable in project spring-framework by spring-projects.

the class AutowireUtils method resolveReturnTypeForFactoryMethod.

/**
	 * Determine the target type for the generic return type of the given
	 * <em>generic factory method</em>, where formal type variables are declared
	 * on the given method itself.
	 * <p>For example, given a factory method with the following signature, if
	 * {@code resolveReturnTypeForFactoryMethod()} is invoked with the reflected
	 * method for {@code creatProxy()} and an {@code Object[]} array containing
	 * {@code MyService.class}, {@code resolveReturnTypeForFactoryMethod()} will
	 * infer that the target return type is {@code MyService}.
	 * <pre class="code">{@code public static <T> T createProxy(Class<T> clazz)}</pre>
	 * <h4>Possible Return Values</h4>
	 * <ul>
	 * <li>the target return type, if it can be inferred</li>
	 * <li>the {@linkplain Method#getReturnType() standard return type}, if
	 * the given {@code method} does not declare any {@linkplain
	 * Method#getTypeParameters() formal type variables}</li>
	 * <li>the {@linkplain Method#getReturnType() standard return type}, if the
	 * target return type cannot be inferred (e.g., due to type erasure)</li>
	 * <li>{@code null}, if the length of the given arguments array is shorter
	 * than the length of the {@linkplain
	 * Method#getGenericParameterTypes() formal argument list} for the given
	 * method</li>
	 * </ul>
	 * @param method the method to introspect (never {@code null})
	 * @param args the arguments that will be supplied to the method when it is
	 * invoked (never {@code null})
	 * @param classLoader the ClassLoader to resolve class names against,
	 * if necessary (never {@code null})
	 * @return the resolved target return type or the standard method return type
	 * @since 3.2.5
	 */
public static Class<?> resolveReturnTypeForFactoryMethod(Method method, Object[] args, ClassLoader classLoader) {
    Assert.notNull(method, "Method must not be null");
    Assert.notNull(args, "Argument array must not be null");
    Assert.notNull(classLoader, "ClassLoader must not be null");
    TypeVariable<Method>[] declaredTypeVariables = method.getTypeParameters();
    Type genericReturnType = method.getGenericReturnType();
    Type[] methodParameterTypes = method.getGenericParameterTypes();
    Assert.isTrue(args.length == methodParameterTypes.length, "Argument array does not match parameter count");
    // Ensure that the type variable (e.g., T) is declared directly on the method
    // itself (e.g., via <T>), not on the enclosing class or interface.
    boolean locallyDeclaredTypeVariableMatchesReturnType = false;
    for (TypeVariable<Method> currentTypeVariable : declaredTypeVariables) {
        if (currentTypeVariable.equals(genericReturnType)) {
            locallyDeclaredTypeVariableMatchesReturnType = true;
            break;
        }
    }
    if (locallyDeclaredTypeVariableMatchesReturnType) {
        for (int i = 0; i < methodParameterTypes.length; i++) {
            Type methodParameterType = methodParameterTypes[i];
            Object arg = args[i];
            if (methodParameterType.equals(genericReturnType)) {
                if (arg instanceof TypedStringValue) {
                    TypedStringValue typedValue = ((TypedStringValue) arg);
                    if (typedValue.hasTargetType()) {
                        return typedValue.getTargetType();
                    }
                    try {
                        return typedValue.resolveTargetType(classLoader);
                    } catch (ClassNotFoundException ex) {
                        throw new IllegalStateException("Failed to resolve value type [" + typedValue.getTargetTypeName() + "] for factory method argument", ex);
                    }
                }
                // Only consider argument type if it is a simple value...
                if (arg != null && !(arg instanceof BeanMetadataElement)) {
                    return arg.getClass();
                }
                return method.getReturnType();
            } else if (methodParameterType instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) methodParameterType;
                Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
                for (Type typeArg : actualTypeArguments) {
                    if (typeArg.equals(genericReturnType)) {
                        if (arg instanceof Class) {
                            return (Class<?>) arg;
                        } else {
                            String className = null;
                            if (arg instanceof String) {
                                className = (String) arg;
                            } else if (arg instanceof TypedStringValue) {
                                TypedStringValue typedValue = ((TypedStringValue) arg);
                                String targetTypeName = typedValue.getTargetTypeName();
                                if (targetTypeName == null || Class.class.getName().equals(targetTypeName)) {
                                    className = typedValue.getValue();
                                }
                            }
                            if (className != null) {
                                try {
                                    return ClassUtils.forName(className, classLoader);
                                } catch (ClassNotFoundException ex) {
                                    throw new IllegalStateException("Could not resolve class name [" + arg + "] for factory method argument", ex);
                                }
                            }
                            // For now, just fall back...
                            return method.getReturnType();
                        }
                    }
                }
            }
        }
    }
    // Fall back...
    return method.getReturnType();
}
Also used : Method(java.lang.reflect.Method) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue)

Example 25 with TypeVariable

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

the class OldGenericReflectionCornerCases method testMultipleBoundedWildcardUnEquality.

@SuppressWarnings("unchecked")
public void testMultipleBoundedWildcardUnEquality() throws Exception {
    Class<? extends MultipleBoundedWildcardUnEquality> clazz = MultipleBoundedWildcardUnEquality.class;
    // new WildcardEquality<Object>().wildcardEquality(new Pair<String,
    // Integer>());
    Method method = clazz.getDeclaredMethod("multipleBoundedWildcardUnEquality", 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(MultipleBoundedWildcardUnEquality.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;
    assertNotEquals(firstWildcardArgument, secondWildcardArgument);
    Type[] firstWildcardArgumentUpperBounds = firstWildcardArgument.getUpperBounds();
    assertLenghtOne(firstWildcardArgumentUpperBounds);
    Type firstWildcardArgumentUpperBoundsType = firstWildcardArgumentUpperBounds[0];
    Type[] secondWildcardArgumentLowerBounds = secondWildcardArgument.getLowerBounds();
    assertLenghtOne(secondWildcardArgumentLowerBounds);
    Type secondWildcardArgumentLoweroundsType = secondWildcardArgumentLowerBounds[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)

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