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;
}
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()));
}
}
}
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();
}
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;
}
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());
}
Aggregations