use of java.lang.reflect.GenericArrayType in project guava by google.
the class TypeVisitorTest method testVisitGenericArrayType.
public <T> void testVisitGenericArrayType() {
Type type = new TypeCapture<T[]>() {
}.capture();
assertVisited(type);
new BaseTypeVisitor() {
@Override
void visitGenericArrayType(GenericArrayType t) {
}
}.visit(type);
}
use of java.lang.reflect.GenericArrayType in project guava by google.
the class TypesTest method testNewArrayType.
public void testNewArrayType() {
Type jvmType1 = new TypeCapture<List<String>[]>() {
}.capture();
GenericArrayType ourType1 = (GenericArrayType) Types.newArrayType(Types.newParameterizedType(List.class, String.class));
Type jvmType2 = new TypeCapture<List[]>() {
}.capture();
Type ourType2 = Types.newArrayType(List.class);
new EqualsTester().addEqualityGroup(jvmType1, ourType1).addEqualityGroup(jvmType2, ourType2).testEquals();
assertEquals(new TypeCapture<List<String>>() {
}.capture(), ourType1.getGenericComponentType());
assertEquals(jvmType1.toString(), ourType1.toString());
assertEquals(jvmType2.toString(), ourType2.toString());
}
use of java.lang.reflect.GenericArrayType in project guice by google.
the class InjectorImpl method createTypeLiteralBinding.
/**
* Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's
* a bit awkward because we have to pull out the inner type in the type literal.
*/
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException {
Type typeLiteralType = key.getTypeLiteral().getType();
if (!(typeLiteralType instanceof ParameterizedType)) {
throw errors.cannotInjectRawTypeLiteral().toException();
}
ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType;
Type innerType = parameterizedType.getActualTypeArguments()[0];
// this proves problematic, we can probably fix TypeLiteral to support type variables
if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) {
throw errors.cannotInjectTypeLiteralOf(innerType).toException();
}
// by definition, innerType == T, so this is safe
@SuppressWarnings("unchecked") TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType);
InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<TypeLiteral<T>>(Initializables.of(value));
return new InstanceBindingImpl<TypeLiteral<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), value);
}
use of java.lang.reflect.GenericArrayType in project guava by google.
the class TypeResolver method resolveGenericArrayType.
private Type resolveGenericArrayType(GenericArrayType type) {
Type componentType = type.getGenericComponentType();
Type resolvedComponentType = resolveType(componentType);
return Types.newArrayType(resolvedComponentType);
}
use of java.lang.reflect.GenericArrayType in project guice by google.
the class TypeLiteral method resolveType.
Type resolveType(Type toResolve) {
// this implementation is made a little more complicated in an attempt to avoid object-creation
while (true) {
if (toResolve instanceof TypeVariable) {
TypeVariable original = (TypeVariable) toResolve;
toResolve = MoreTypes.resolveTypeVariable(type, rawType, original);
if (toResolve == original) {
return toResolve;
}
} else if (toResolve instanceof GenericArrayType) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType();
Type newComponentType = resolveType(componentType);
return componentType == newComponentType ? original : Types.arrayOf(newComponentType);
} else if (toResolve instanceof ParameterizedType) {
ParameterizedType original = (ParameterizedType) toResolve;
Type ownerType = original.getOwnerType();
Type newOwnerType = resolveType(ownerType);
boolean changed = newOwnerType != ownerType;
Type[] args = original.getActualTypeArguments();
for (int t = 0, length = args.length; t < length; t++) {
Type resolvedTypeArgument = resolveType(args[t]);
if (resolvedTypeArgument != args[t]) {
if (!changed) {
args = args.clone();
changed = true;
}
args[t] = resolvedTypeArgument;
}
}
return changed ? Types.newParameterizedTypeWithOwner(newOwnerType, original.getRawType(), args) : original;
} else if (toResolve instanceof WildcardType) {
WildcardType original = (WildcardType) toResolve;
Type[] originalLowerBound = original.getLowerBounds();
Type[] originalUpperBound = original.getUpperBounds();
if (originalLowerBound.length == 1) {
Type lowerBound = resolveType(originalLowerBound[0]);
if (lowerBound != originalLowerBound[0]) {
return Types.supertypeOf(lowerBound);
}
} else if (originalUpperBound.length == 1) {
Type upperBound = resolveType(originalUpperBound[0]);
if (upperBound != originalUpperBound[0]) {
return Types.subtypeOf(upperBound);
}
}
return original;
} else {
return toResolve;
}
}
}
Aggregations