Search in sources :

Example 86 with TypeVariable

use of java.lang.reflect.TypeVariable in project cdap by caskdata.

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.Nullable)

Example 87 with TypeVariable

use of java.lang.reflect.TypeVariable in project jdk8u_jdk by JetBrains.

the class TypeResolver method prepare.

/**
     * Fills the map from type parameters
     * to types as seen by the given {@code type}.
     * The method is recursive because the {@code type}
     * inherits mappings from its parent classes and interfaces.
     * The {@code type} can be either a {@link Class Class}
     * or a {@link ParameterizedType ParameterizedType}.
     * If it is a {@link Class Class}, it is either equivalent
     * to a {@link ParameterizedType ParameterizedType} with no parameters,
     * or it represents the erasure of a {@link ParameterizedType ParameterizedType}.
     *
     * @param map   the mappings of all type variables
     * @param type  the next type in the hierarchy
     */
private static void prepare(Map<Type, Type> map, Type type) {
    Class<?> raw = (Class<?>) ((type instanceof Class<?>) ? type : ((ParameterizedType) type).getRawType());
    TypeVariable<?>[] formals = raw.getTypeParameters();
    Type[] actuals = (type instanceof Class<?>) ? formals : ((ParameterizedType) type).getActualTypeArguments();
    assert formals.length == actuals.length;
    for (int i = 0; i < formals.length; i++) {
        map.put(formals[i], actuals[i]);
    }
    Type gSuperclass = raw.getGenericSuperclass();
    if (gSuperclass != null) {
        prepare(map, gSuperclass);
    }
    for (Type gInterface : raw.getGenericInterfaces()) {
        prepare(map, gInterface);
    }
    // all of its type variables, including inherited ones.
    if (type instanceof Class<?> && formals.length > 0) {
        for (Map.Entry<Type, Type> entry : map.entrySet()) {
            entry.setValue(erase(entry.getValue()));
        }
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) Map(java.util.Map) HashMap(java.util.HashMap)

Example 88 with TypeVariable

use of java.lang.reflect.TypeVariable in project jdk8u_jdk by JetBrains.

the class GenericDeclRepository method getTypeParameters.

// public API
/*
 * When queried for a particular piece of type information, the
 * general pattern is to consult the corresponding cached value.
 * If the corresponding field is non-null, it is returned.
 * If not, it is created lazily. This is done by selecting the appropriate
 * part of the tree and transforming it into a reflective object
 * using a visitor, which is created by feeding it the factory
 * with which the repository was created.
 */
/**
     * Return the formal type parameters of this generic declaration.
     * @return the formal type parameters of this generic declaration
     */
public TypeVariable<?>[] getTypeParameters() {
    TypeVariable<?>[] typeParams = this.typeParams;
    if (typeParams == null) {
        // lazily initialize type parameters
        // first, extract type parameter subtree(s) from AST
        FormalTypeParameter[] ftps = getTree().getFormalTypeParameters();
        // create array to store reified subtree(s)
        typeParams = new TypeVariable<?>[ftps.length];
        // reify all subtrees
        for (int i = 0; i < ftps.length; i++) {
            // obtain visitor
            Reifier r = getReifier();
            // reify subtree
            ftps[i].accept(r);
            // extract result from visitor and store it
            typeParams[i] = (TypeVariable<?>) r.getResult();
        }
        // cache overall result
        this.typeParams = typeParams;
    }
    // return cached result
    return typeParams.clone();
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) Reifier(sun.reflect.generics.visitor.Reifier) FormalTypeParameter(sun.reflect.generics.tree.FormalTypeParameter)

Example 89 with TypeVariable

use of java.lang.reflect.TypeVariable in project jdk8u_jdk by JetBrains.

the class TypeVariableImpl method equals.

@Override
public boolean equals(Object o) {
    if (o instanceof TypeVariable && o.getClass() == TypeVariableImpl.class) {
        TypeVariable<?> that = (TypeVariable<?>) o;
        GenericDeclaration thatDecl = that.getGenericDeclaration();
        String thatName = that.getName();
        return Objects.equals(genericDeclaration, thatDecl) && Objects.equals(name, thatName);
    } else
        return false;
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 90 with TypeVariable

use of java.lang.reflect.TypeVariable in project bnd by bndtools.

the class JSONTest method testGenericsVars.

public void testGenericsVars() {
    ParameterizedType type = (ParameterizedType) new TypeReference<Base<String>>() {
    }.getType();
    System.out.println(type);
    ParameterizedType list = (ParameterizedType) Base.class.getGenericInterfaces()[0];
    System.out.println(list);
    TypeVariable<?> tv = (TypeVariable<?>) list.getActualTypeArguments()[0];
    System.out.println(tv.getGenericDeclaration().getTypeParameters()[0] == tv);
    System.out.println(type.getRawType());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) TypeReference(aQute.lib.converter.TypeReference)

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