Search in sources :

Example 66 with TypeVariable

use of java.lang.reflect.TypeVariable in project aries by apache.

the class GenericType method parametersOf.

static GenericType[] parametersOf(Type type) {
    if (type instanceof Class) {
        Class clazz = (Class) type;
        if (clazz.isArray()) {
            GenericType t = new GenericType(clazz.getComponentType());
            if (t.size() > 0) {
                return new GenericType[] { t };
            } else {
                return EMPTY;
            }
        } else {
            return EMPTY;
        }
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type[] parameters = pt.getActualTypeArguments();
        GenericType[] gts = new GenericType[parameters.length];
        for (int i = 0; i < gts.length; i++) {
            gts[i] = new GenericType(parameters[i]);
        }
        return gts;
    }
    if (type instanceof GenericArrayType) {
        return new GenericType[] { new GenericType(((GenericArrayType) type).getGenericComponentType()) };
    }
    if (type instanceof WildcardType) {
        return EMPTY;
    }
    if (type instanceof TypeVariable) {
        return EMPTY;
    }
    throw new IllegalStateException();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) 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 67 with TypeVariable

use of java.lang.reflect.TypeVariable in project beam by apache.

the class CoderRegistry method verifyCompatible.

/**
   * Returns {@code true} if the given {@link Coder} can possibly encode elements
   * of the given type.
   */
@VisibleForTesting
static <T, CoderT extends Coder<T>, CandidateT> void verifyCompatible(CoderT coder, Type candidateType) throws IncompatibleCoderException {
    // Various representations of the coder's class
    @SuppressWarnings("unchecked") Class<CoderT> coderClass = (Class<CoderT>) coder.getClass();
    TypeDescriptor<CoderT> coderDescriptor = TypeDescriptor.of(coderClass);
    // Various representations of the actual coded type
    @SuppressWarnings("unchecked") TypeDescriptor<T> codedDescriptor = CoderUtils.getCodedType(coderDescriptor);
    @SuppressWarnings("unchecked") Class<T> codedClass = (Class<T>) codedDescriptor.getRawType();
    Type codedType = codedDescriptor.getType();
    // Various representations of the candidate type
    @SuppressWarnings("unchecked") TypeDescriptor<CandidateT> candidateDescriptor = (TypeDescriptor<CandidateT>) TypeDescriptor.of(candidateType);
    @SuppressWarnings("unchecked") Class<CandidateT> candidateClass = (Class<CandidateT>) candidateDescriptor.getRawType();
    // to erasure, then we cannot rule it out.
    if (candidateType instanceof TypeVariable) {
        return;
    }
    // coder compatibility
    if (!codedClass.isAssignableFrom(candidateClass)) {
        throw new IncompatibleCoderException(String.format("Cannot encode elements of type %s with coder %s because the" + " coded type %s is not assignable from %s", candidateType, coder, codedClass, candidateType), coder, candidateType);
    }
    // we have established that this is a covariant upcast... though
    // coders are invariant, we are just checking one direction
    @SuppressWarnings("unchecked") TypeDescriptor<T> candidateOkDescriptor = (TypeDescriptor<T>) candidateDescriptor;
    // compatible.
    if ((codedType instanceof ParameterizedType) && !isNullOrEmpty(coder.getCoderArguments())) {
        ParameterizedType parameterizedSupertype = ((ParameterizedType) candidateOkDescriptor.getSupertype(codedClass).getType());
        Type[] typeArguments = parameterizedSupertype.getActualTypeArguments();
        List<? extends Coder<?>> typeArgumentCoders = coder.getCoderArguments();
        if (typeArguments.length < typeArgumentCoders.size()) {
            throw new IncompatibleCoderException(String.format("Cannot encode elements of type %s with coder %s:" + " the generic supertype %s has %s type parameters, which is less than the" + " number of coder arguments %s has (%s).", candidateOkDescriptor, coder, parameterizedSupertype, typeArguments.length, coder, typeArgumentCoders.size()), coder, candidateOkDescriptor.getType());
        }
        for (int i = 0; i < typeArgumentCoders.size(); i++) {
            try {
                verifyCompatible(typeArgumentCoders.get(i), candidateDescriptor.resolveType(typeArguments[i]).getType());
            } catch (IncompatibleCoderException exn) {
                throw new IncompatibleCoderException(String.format("Cannot encode elements of type %s with coder %s" + " because some component coder is incompatible", candidateType, coder), coder, candidateType, exn);
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) TypeVariable(java.lang.reflect.TypeVariable) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 68 with TypeVariable

use of java.lang.reflect.TypeVariable in project deltaspike by apache.

the class HierarchyDiscovery method resolveType.

private Type resolveType(Class<?> clazz) {
    if (clazz.getTypeParameters().length > 0) {
        TypeVariable<?>[] actualTypeParameters = clazz.getTypeParameters();
        @SuppressWarnings("UnnecessaryLocalVariable") ParameterizedType parameterizedType = new ParameterizedTypeImpl(clazz, actualTypeParameters, clazz.getDeclaringClass());
        return parameterizedType;
    } else {
        return clazz;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable)

Example 69 with TypeVariable

use of java.lang.reflect.TypeVariable in project deltaspike by apache.

the class HierarchyDiscovery method resolveTypeParameter.

private Type resolveTypeParameter(ParameterizedType type, Type beanType, TypeVariable<?> typeVariable) {
    // step1. raw type
    Class<?> actualType = (Class<?>) type.getRawType();
    TypeVariable<?>[] typeVariables = actualType.getTypeParameters();
    Type[] actualTypes = type.getActualTypeArguments();
    for (int i = 0; i < typeVariables.length; i++) {
        if (typeVariables[i].equals(typeVariable) && !actualTypes[i].equals(typeVariable)) {
            return resolveType(this.type, beanType, actualTypes[i]);
        }
    }
    // step2. generic super class
    Type genericSuperType = actualType.getGenericSuperclass();
    Type resolvedGenericSuperType = resolveType(genericSuperType, beanType, typeVariable);
    if (!(resolvedGenericSuperType instanceof TypeVariable<?>)) {
        return resolvedGenericSuperType;
    }
    // step3. generic interfaces
    if (beanType instanceof ParameterizedType) {
        for (Type interfaceType : ((Class<?>) ((ParameterizedType) beanType).getRawType()).getGenericInterfaces()) {
            Type resolvedType = resolveType(interfaceType, interfaceType, typeVariable);
            if (!(resolvedType instanceof TypeVariable<?>)) {
                return resolvedType;
            }
        }
    }
    // don't resolve type variable
    return typeVariable;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Example 70 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)

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