use of java.lang.reflect.Type in project jodd by oblac.
the class ReflectUtil method typeToString.
private static void typeToString(StringBuilder sb, Type type, Set<Type> visited) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
final Class<?> rawType = (Class<?>) parameterizedType.getRawType();
sb.append(rawType.getName());
boolean first = true;
for (Type typeArg : parameterizedType.getActualTypeArguments()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append('<');
typeToString(sb, typeArg, visited);
sb.append('>');
}
} else if (type instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) type;
sb.append('?');
// According to JLS(http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1):
// - Lower and upper can't coexist: (for instance, this is not allowed: <? extends List<String> & super MyInterface>)
// - Multiple bounds are not supported (for instance, this is not allowed: <? extends List<String> & MyInterface>)
final Type bound;
if (wildcardType.getLowerBounds().length != 0) {
sb.append(" super ");
bound = wildcardType.getLowerBounds()[0];
} else {
sb.append(" extends ");
bound = wildcardType.getUpperBounds()[0];
}
typeToString(sb, bound, visited);
} else if (type instanceof TypeVariable<?>) {
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
sb.append(typeVariable.getName());
if (!visited.contains(type)) {
visited.add(type);
sb.append(" extends ");
boolean first = true;
for (Type bound : typeVariable.getBounds()) {
if (first) {
first = false;
} else {
sb.append(" & ");
}
typeToString(sb, bound, visited);
}
visited.remove(type);
}
} else if (type instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) type;
typeToString(genericArrayType.getGenericComponentType());
sb.append(genericArrayType.getGenericComponentType());
sb.append("[]");
} else if (type instanceof Class) {
Class<?> typeClass = (Class<?>) type;
sb.append(typeClass.getName());
} else {
throw new IllegalArgumentException("Unsupported type: " + type);
}
}
use of java.lang.reflect.Type 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;
}
use of java.lang.reflect.Type 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();
}
use of java.lang.reflect.Type 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);
}
}
use of java.lang.reflect.Type in project voltdb by VoltDB.
the class Types method newArrayType.
/** Returns the array type of {@code componentType}. */
static Type newArrayType(Type componentType) {
if (componentType instanceof WildcardType) {
WildcardType wildcard = (WildcardType) componentType;
Type[] lowerBounds = wildcard.getLowerBounds();
checkArgument(lowerBounds.length <= 1, "Wildcard cannot have more than one lower bounds.");
if (lowerBounds.length == 1) {
return supertypeOf(newArrayType(lowerBounds[0]));
} else {
Type[] upperBounds = wildcard.getUpperBounds();
checkArgument(upperBounds.length == 1, "Wildcard should have only one upper bound.");
return subtypeOf(newArrayType(upperBounds[0]));
}
}
return JavaVersion.CURRENT.newArrayType(componentType);
}
Aggregations