use of java.lang.reflect.Type in project voltdb by VoltDB.
the class Types method disallowPrimitiveType.
private static void disallowPrimitiveType(Type[] types, String usedAs) {
for (Type type : types) {
if (type instanceof Class) {
Class<?> cls = (Class<?>) type;
checkArgument(!cls.isPrimitive(), "Primitive type '%s' used as %s", cls, usedAs);
}
}
}
use of java.lang.reflect.Type in project voltdb by VoltDB.
the class Invokable method getExceptionTypes.
/** Returns all declared exception types of this {@code Invokable}. */
public final ImmutableList<TypeToken<? extends Throwable>> getExceptionTypes() {
ImmutableList.Builder<TypeToken<? extends Throwable>> builder = ImmutableList.builder();
for (Type type : getGenericExceptionTypes()) {
// getGenericExceptionTypes() will never return a type that's not exception
@SuppressWarnings("unchecked") TypeToken<? extends Throwable> exceptionType = (TypeToken<? extends Throwable>) TypeToken.of(type);
builder.add(exceptionType);
}
return builder.build();
}
use of java.lang.reflect.Type in project voltdb by VoltDB.
the class TypeResolver method resolveParameterizedType.
private ParameterizedType resolveParameterizedType(ParameterizedType type) {
Type owner = type.getOwnerType();
Type resolvedOwner = (owner == null) ? null : resolveType(owner);
Type resolvedRawType = resolveType(type.getRawType());
Type[] args = type.getActualTypeArguments();
Type[] resolvedArgs = resolveTypes(args);
return Types.newParameterizedTypeWithOwner(resolvedOwner, (Class<?>) resolvedRawType, resolvedArgs);
}
use of java.lang.reflect.Type 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;
}
use of java.lang.reflect.Type 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);
}
}
}
Aggregations