Search in sources :

Example 11 with TypeVariable

use of java.lang.reflect.TypeVariable in project jodd by oblac.

the class ReflectUtil method getRawType.

/**
	 * Returns raw class for given <code>type</code> when implementation class is known
	 * and it makes difference.
	 * @see #resolveVariable(java.lang.reflect.TypeVariable, Class)
	 */
public static Class<?> getRawType(Type type, Class implClass) {
    if (type instanceof Class) {
        return (Class) type;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        return getRawType(pType.getRawType(), implClass);
    }
    if (type instanceof WildcardType) {
        WildcardType wType = (WildcardType) type;
        Type[] lowerTypes = wType.getLowerBounds();
        if (lowerTypes.length > 0) {
            return getRawType(lowerTypes[0], implClass);
        }
        Type[] upperTypes = wType.getUpperBounds();
        if (upperTypes.length != 0) {
            return getRawType(upperTypes[0], implClass);
        }
        return Object.class;
    }
    if (type instanceof GenericArrayType) {
        Type genericComponentType = ((GenericArrayType) type).getGenericComponentType();
        Class<?> rawType = getRawType(genericComponentType, implClass);
        // this is sort of stupid, but there seems no other way (consider don't creating new instances each time)...
        return Array.newInstance(rawType, 0).getClass();
    }
    if (type instanceof TypeVariable) {
        TypeVariable<?> varType = (TypeVariable<?>) type;
        if (implClass != null) {
            Type resolvedType = resolveVariable(varType, implClass);
            if (resolvedType != null) {
                return getRawType(resolvedType, null);
            }
        }
        Type[] boundsTypes = varType.getBounds();
        if (boundsTypes.length == 0) {
            return Object.class;
        }
        return getRawType(boundsTypes[0], implClass);
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) AccessibleObject(java.lang.reflect.AccessibleObject) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 12 with TypeVariable

use of java.lang.reflect.TypeVariable in project voltdb by VoltDB.

the class TypeToken method getGenericInterfaces.

/**
   * Returns the generic interfaces that this type directly {@code implements}. This method is
   * similar but different from {@link Class#getGenericInterfaces()}. For example, {@code new
   * TypeToken<List<String>>() {}.getGenericInterfaces()} will return a list that contains
   * {@code new TypeToken<Iterable<String>>() {}}; while {@code List.class.getGenericInterfaces()}
   * will return an array that contains {@code Iterable<T>}, where the {@code T} is the type
   * variable declared by interface {@code Iterable}.
   *
   * <p>If this type is a type variable or wildcard, its upper bounds are examined and those that
   * are either an interface or upper-bounded only by interfaces are returned. This means that the
   * returned types could include type variables too.
   */
final ImmutableList<TypeToken<? super T>> getGenericInterfaces() {
    if (runtimeType instanceof TypeVariable) {
        return boundsAsInterfaces(((TypeVariable<?>) runtimeType).getBounds());
    }
    if (runtimeType instanceof WildcardType) {
        return boundsAsInterfaces(((WildcardType) runtimeType).getUpperBounds());
    }
    ImmutableList.Builder<TypeToken<? super T>> builder = ImmutableList.builder();
    for (Type interfaceType : getRawType().getGenericInterfaces()) {
        // interface of T
        @SuppressWarnings("unchecked") TypeToken<? super T> resolvedInterface = (TypeToken<? super T>) resolveSupertype(interfaceType);
        builder.add(resolvedInterface);
    }
    return builder.build();
}
Also used : WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) ImmutableList(com.google_voltpatches.common.collect.ImmutableList)

Example 13 with TypeVariable

use of java.lang.reflect.TypeVariable in project voltdb by VoltDB.

the class TypeToken method toGenericType.

/**
   * Returns the type token representing the generic type declaration of {@code cls}. For example:
   * {@code TypeToken.getGenericType(Iterable.class)} returns {@code Iterable<T>}.
   *
   * <p>If {@code cls} isn't parameterized and isn't a generic array, the type token of the class is
   * returned.
   */
@VisibleForTesting
static <T> TypeToken<? extends T> toGenericType(Class<T> cls) {
    if (cls.isArray()) {
        Type arrayOfGenericType = Types.newArrayType(// If we are passed with int[].class, don't turn it to GenericArrayType
        toGenericType(cls.getComponentType()).runtimeType);
        // array is covariant
        @SuppressWarnings("unchecked") TypeToken<? extends T> result = (TypeToken<? extends T>) of(arrayOfGenericType);
        return result;
    }
    TypeVariable<Class<T>>[] typeParams = cls.getTypeParameters();
    Type ownerType = cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers()) ? toGenericType(cls.getEnclosingClass()).runtimeType : null;
    if ((typeParams.length > 0) || ((ownerType != null) && ownerType != cls.getEnclosingClass())) {
        // Like, it's Iterable<T> for Iterable.class
        @SuppressWarnings("unchecked") TypeToken<? extends T> type = (TypeToken<? extends T>) of(Types.newParameterizedTypeWithOwner(ownerType, cls, typeParams));
        return type;
    } else {
        return of(cls);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) VisibleForTesting(com.google_voltpatches.common.annotations.VisibleForTesting)

Example 14 with TypeVariable

use of java.lang.reflect.TypeVariable in project voltdb by VoltDB.

the class TypeToken method getGenericSuperclass.

/**
   * Returns the generic superclass of this type or {@code null} if the type represents
   * {@link Object} or an interface. This method is similar but different from
   * {@link Class#getGenericSuperclass}. For example, {@code new TypeToken<StringArrayList>()
   * {}.getGenericSuperclass()} will return {@code new TypeToken<ArrayList<String>>() {}}; while
   * {@code StringArrayList.class.getGenericSuperclass()} will return {@code ArrayList<E>}, where
   * {@code E} is the type variable declared by class {@code ArrayList}.
   *
   * <p>If this type is a type variable or wildcard, its first upper bound is examined and returned
   * if the bound is a class or extends from a class. This means that the returned type could be a
   * type variable too.
   */
@Nullable
final TypeToken<? super T> getGenericSuperclass() {
    if (runtimeType instanceof TypeVariable) {
        // First bound is always the super class, if one exists.
        return boundAsSuperclass(((TypeVariable<?>) runtimeType).getBounds()[0]);
    }
    if (runtimeType instanceof WildcardType) {
        // wildcard has one and only one upper bound.
        return boundAsSuperclass(((WildcardType) runtimeType).getUpperBounds()[0]);
    }
    Type superclass = getRawType().getGenericSuperclass();
    if (superclass == null) {
        return null;
    }
    // super class of T
    @SuppressWarnings("unchecked") TypeToken<? super T> superToken = (TypeToken<? super T>) resolveSupertype(superclass);
    return superToken;
}
Also used : WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Nullable(javax.annotation_voltpatches.Nullable)

Example 15 with TypeVariable

use of java.lang.reflect.TypeVariable in project voltdb by VoltDB.

the class TypeParameterMatcher method find0.

private static Class<?> find0(final Object object, Class<?> parameterizedSuperclass, String typeParamName) {
    final Class<?> thisClass = object.getClass();
    Class<?> currentClass = thisClass;
    for (; ; ) {
        if (currentClass.getSuperclass() == parameterizedSuperclass) {
            int typeParamIndex = -1;
            TypeVariable<?>[] typeParams = currentClass.getSuperclass().getTypeParameters();
            for (int i = 0; i < typeParams.length; i++) {
                if (typeParamName.equals(typeParams[i].getName())) {
                    typeParamIndex = i;
                    break;
                }
            }
            if (typeParamIndex < 0) {
                throw new IllegalStateException("unknown type parameter '" + typeParamName + "': " + parameterizedSuperclass);
            }
            Type genericSuperType = currentClass.getGenericSuperclass();
            if (!(genericSuperType instanceof ParameterizedType)) {
                return Object.class;
            }
            Type[] actualTypeParams = ((ParameterizedType) genericSuperType).getActualTypeArguments();
            Type actualTypeParam = actualTypeParams[typeParamIndex];
            if (actualTypeParam instanceof ParameterizedType) {
                actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
            }
            if (actualTypeParam instanceof Class) {
                return (Class<?>) actualTypeParam;
            }
            if (actualTypeParam instanceof GenericArrayType) {
                Type componentType = ((GenericArrayType) actualTypeParam).getGenericComponentType();
                if (componentType instanceof ParameterizedType) {
                    componentType = ((ParameterizedType) componentType).getRawType();
                }
                if (componentType instanceof Class) {
                    return Array.newInstance((Class<?>) componentType, 0).getClass();
                }
            }
            if (actualTypeParam instanceof TypeVariable) {
                // Resolved type parameter points to another type parameter.
                TypeVariable<?> v = (TypeVariable<?>) actualTypeParam;
                currentClass = thisClass;
                if (!(v.getGenericDeclaration() instanceof Class)) {
                    return Object.class;
                }
                parameterizedSuperclass = (Class<?>) v.getGenericDeclaration();
                typeParamName = v.getName();
                if (parameterizedSuperclass.isAssignableFrom(thisClass)) {
                    continue;
                } else {
                    return Object.class;
                }
            }
            return fail(thisClass, typeParamName);
        }
        currentClass = currentClass.getSuperclass();
        if (currentClass == null) {
            return fail(thisClass, typeParamName);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

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