Search in sources :

Example 31 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project guava by google.

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

Example 32 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project guice by google.

the class ProviderChecker method checkInterface.

static <P extends CheckedProvider<?>> void checkInterface(Class<P> interfaceType, Optional<? extends Type> valueType) {
    checkArgument(interfaceType.isInterface(), "%s must be an interface", interfaceType.getName());
    checkArgument(interfaceType.getGenericInterfaces().length == 1, "%s must extend CheckedProvider (and only CheckedProvider)", interfaceType);
    boolean tpMode = interfaceType.getInterfaces()[0] == ThrowingProvider.class;
    if (!tpMode) {
        checkArgument(interfaceType.getInterfaces()[0] == CheckedProvider.class, "%s must extend CheckedProvider (and only CheckedProvider)", interfaceType);
    }
    // Ensure that T is parameterized and unconstrained.
    ParameterizedType genericThrowingProvider = (ParameterizedType) interfaceType.getGenericInterfaces()[0];
    if (interfaceType.getTypeParameters().length == 1) {
        String returnTypeName = interfaceType.getTypeParameters()[0].getName();
        Type returnType = genericThrowingProvider.getActualTypeArguments()[0];
        checkArgument(returnType instanceof TypeVariable, "%s does not properly extend CheckedProvider, the first type parameter of CheckedProvider" + " (%s) is not a generic type", interfaceType, returnType);
        checkArgument(returnTypeName.equals(((TypeVariable) returnType).getName()), "The generic type (%s) of %s does not match the generic type of CheckedProvider (%s)", returnTypeName, interfaceType, ((TypeVariable) returnType).getName());
    } else {
        checkArgument(interfaceType.getTypeParameters().length == 0, "%s has more than one generic type parameter: %s", interfaceType, Arrays.asList(interfaceType.getTypeParameters()));
        if (valueType.isPresent()) {
            checkArgument(genericThrowingProvider.getActualTypeArguments()[0].equals(valueType.get()), "%s expects the value type to be %s, but it was %s", interfaceType, genericThrowingProvider.getActualTypeArguments()[0], valueType.get());
        }
    }
    if (tpMode) {
        // only validate exception in ThrowingProvider mode.
        Type exceptionType = genericThrowingProvider.getActualTypeArguments()[1];
        checkArgument(exceptionType instanceof Class, "%s has the wrong Exception generic type (%s) when extending CheckedProvider", interfaceType, exceptionType);
    }
    // Skip synthetic/bridge methods because java8 generates
    // a default method on the interface w/ the superinterface type that
    // just delegates directly to the overridden method.
    List<Method> declaredMethods = FluentIterable.from(Arrays.asList(interfaceType.getDeclaredMethods())).filter(NotSyntheticOrBridgePredicate.INSTANCE).toList();
    if (declaredMethods.size() == 1) {
        Method method = declaredMethods.get(0);
        checkArgument(method.getName().equals("get"), "%s may not declare any new methods, but declared %s", interfaceType, method);
        checkArgument(method.getParameterTypes().length == 0, "%s may not declare any new methods, but declared %s", interfaceType, method.toGenericString());
    } else {
        checkArgument(declaredMethods.isEmpty(), "%s may not declare any new methods, but declared %s", interfaceType, Arrays.asList(interfaceType.getDeclaredMethods()));
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method)

Example 33 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project guice by google.

the class MoreTypes method equals.

/** Returns true if {@code a} and {@code b} are equal. */
public static boolean equals(Type a, Type b) {
    if (a == b) {
        // also handles (a == null && b == null)
        return true;
    } else if (a instanceof Class) {
        // Class already specifies equals().
        return a.equals(b);
    } else if (a instanceof ParameterizedType) {
        if (!(b instanceof ParameterizedType)) {
            return false;
        }
        // TODO: save a .clone() call
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        return Objects.equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
    } else if (a instanceof GenericArrayType) {
        if (!(b instanceof GenericArrayType)) {
            return false;
        }
        GenericArrayType ga = (GenericArrayType) a;
        GenericArrayType gb = (GenericArrayType) b;
        return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
    } else if (a instanceof WildcardType) {
        if (!(b instanceof WildcardType)) {
            return false;
        }
        WildcardType wa = (WildcardType) a;
        WildcardType wb = (WildcardType) b;
        return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
    } else if (a instanceof TypeVariable) {
        if (!(b instanceof TypeVariable)) {
            return false;
        }
        TypeVariable<?> va = (TypeVariable) a;
        TypeVariable<?> vb = (TypeVariable) b;
        return va.getGenericDeclaration().equals(vb.getGenericDeclaration()) && va.getName().equals(vb.getName());
    } else {
        // This isn't a type we support. Could be a generic array type, wildcard type, etc.
        return false;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 34 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project guice by google.

the class MoreTypes method resolveTypeVariable.

public static Type resolveTypeVariable(Type type, Class<?> rawType, TypeVariable unknown) {
    Class<?> declaredByRaw = declaringClassOf(unknown);
    // we can't reduce this further
    if (declaredByRaw == null) {
        return unknown;
    }
    Type declaredBy = getGenericSupertype(type, rawType, declaredByRaw);
    if (declaredBy instanceof ParameterizedType) {
        int index = indexOf(declaredByRaw.getTypeParameters(), unknown);
        return ((ParameterizedType) declaredBy).getActualTypeArguments()[index];
    }
    return unknown;
}
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)

Example 35 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project guice by google.

the class MoreTypes method canonicalizeForKey.

/**
   * Returns an type that's appropriate for use in a key.
   *
   * <p>If the raw type of {@code typeLiteral} is a {@code javax.inject.Provider}, this returns a
   * {@code com.google.inject.Provider} with the same type parameters.
   *
   * <p>If the type is a primitive, the corresponding wrapper type will be returned.
   *
   * @throws ConfigurationException if {@code type} contains a type variable
   */
public static <T> TypeLiteral<T> canonicalizeForKey(TypeLiteral<T> typeLiteral) {
    Type type = typeLiteral.getType();
    if (!isFullySpecified(type)) {
        Errors errors = new Errors().keyNotFullySpecified(typeLiteral);
        throw new ConfigurationException(errors.getMessages());
    }
    if (typeLiteral.getRawType() == javax.inject.Provider.class) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        // the following casts are generally unsafe, but com.google.inject.Provider extends
        // javax.inject.Provider and is covariant
        @SuppressWarnings("unchecked") TypeLiteral<T> guiceProviderType = (TypeLiteral<T>) TypeLiteral.get(Types.providerOf(parameterizedType.getActualTypeArguments()[0]));
        return guiceProviderType;
    }
    @SuppressWarnings("unchecked") TypeLiteral<T> wrappedPrimitives = (TypeLiteral<T>) PRIMITIVE_TO_WRAPPER.get(typeLiteral);
    if (wrappedPrimitives != null) {
        return wrappedPrimitives;
    }
    // If we know this isn't a subclass, return as-is.
    if (typeLiteral.getClass() == TypeLiteral.class) {
        return typeLiteral;
    }
    // recreate the TypeLiteral to avoid anonymous TypeLiterals from holding refs to their
    // surrounding classes.
    @SuppressWarnings("unchecked") TypeLiteral<T> recreated = (TypeLiteral<T>) TypeLiteral.get(typeLiteral.getType());
    return recreated;
}
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) TypeLiteral(com.google.inject.TypeLiteral) ConfigurationException(com.google.inject.ConfigurationException)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)704 Type (java.lang.reflect.Type)532 GenericArrayType (java.lang.reflect.GenericArrayType)226 WildcardType (java.lang.reflect.WildcardType)182 TypeVariable (java.lang.reflect.TypeVariable)137 ArrayList (java.util.ArrayList)94 Method (java.lang.reflect.Method)71 Test (org.junit.Test)59 List (java.util.List)55 Field (java.lang.reflect.Field)49 Map (java.util.Map)47 HashMap (java.util.HashMap)42 Collection (java.util.Collection)26 MediaType (javax.ws.rs.core.MediaType)23 Annotation (java.lang.annotation.Annotation)18 HashSet (java.util.HashSet)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 IOException (java.io.IOException)11