Search in sources :

Example 86 with GenericArrayType

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);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 87 with GenericArrayType

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());
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) EqualsTester(com.google.common.testing.EqualsTester) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 88 with GenericArrayType

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);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(com.google.inject.TypeLiteral) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 89 with GenericArrayType

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);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 90 with GenericArrayType

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;
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)137 ParameterizedType (java.lang.reflect.ParameterizedType)131 Type (java.lang.reflect.Type)105 TypeVariable (java.lang.reflect.TypeVariable)78 WildcardType (java.lang.reflect.WildcardType)76 Field (java.lang.reflect.Field)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)4 Collection (java.util.Collection)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ElementType (java.lang.annotation.ElementType)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GenericType (javax.ws.rs.core.GenericType)3 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)3 XmlType (javax.xml.bind.annotation.XmlType)3 Holder (javax.xml.ws.Holder)3 EqualsTester (com.google.common.testing.EqualsTester)2