Search in sources :

Example 16 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project elasticsearch by elastic.

the class InjectorImpl method createMembersInjectorBinding.

private <T> BindingImpl<MembersInjector<T>> createMembersInjectorBinding(Key<MembersInjector<T>> key, Errors errors) throws ErrorsException {
    Type membersInjectorType = key.getTypeLiteral().getType();
    if (!(membersInjectorType instanceof ParameterizedType)) {
        throw errors.cannotInjectRawMembersInjector().toException();
    }
    // safe because T came from Key<MembersInjector<T>>
    @SuppressWarnings("unchecked") TypeLiteral<T> instanceType = (TypeLiteral<T>) TypeLiteral.get(((ParameterizedType) membersInjectorType).getActualTypeArguments()[0]);
    MembersInjector<T> membersInjector = membersInjectorStore.get(instanceType, errors);
    InternalFactory<MembersInjector<T>> factory = new ConstantFactory<>(Initializables.of(membersInjector));
    return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, emptySet(), membersInjector);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InstanceBindingImpl(org.elasticsearch.common.inject.internal.InstanceBindingImpl)

Example 17 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project elasticsearch by elastic.

the class TypeLiteral method getSuperclassTypeParameter.

/**
     * Returns the type from super class's type parameter in {@link MoreTypes#canonicalize(Type)
     * canonical form}.
     */
static Type getSuperclassTypeParameter(Class<?> subclass) {
    Type superclass = subclass.getGenericSuperclass();
    if (superclass instanceof Class) {
        throw new RuntimeException("Missing type parameter.");
    }
    ParameterizedType parameterized = (ParameterizedType) superclass;
    return canonicalize(parameterized.getActualTypeArguments()[0]);
}
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 18 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project elasticsearch by elastic.

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 19 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project buck by facebook.

the class Types method getContainerClass.

/**
   * @return The raw type of the {@link Collection} a field represents, even if contained in an
   *    {@link Optional}, but without the ParameterizedType information.
   */
@SuppressWarnings("unchecked")
@Nullable
public static Class<? extends Collection<?>> getContainerClass(Field field) {
    Type type = getFirstNonOptionalType(field);
    if (!(type instanceof ParameterizedType)) {
        return null;
    }
    Type rawType = ((ParameterizedType) type).getRawType();
    if (!(rawType instanceof Class)) {
        return null;
    }
    Class<?> clazz = (Class<?>) rawType;
    if (!(Collection.class.isAssignableFrom(clazz))) {
        return null;
    }
    return (Class<? extends Collection<?>>) clazz;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) Collection(java.util.Collection) Nullable(javax.annotation.Nullable)

Example 20 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class Classes method resolveTypeVariable.

@SuppressWarnings("raw")
private static Type resolveTypeVariable(TypeVariable name, Class declaringClass, Map<TypeVariable, Type> mappings, Class current) {
    if (current.equals(declaringClass)) {
        Type resolvedType = name;
        while (resolvedType instanceof TypeVariable) {
            resolvedType = mappings.get(resolvedType);
        }
        return resolvedType;
    }
    List<Type> types = new ArrayList<>();
    for (Type type : current.getGenericInterfaces()) {
        Iterable<Type> interfaces = Classes.INTERFACES_OF.map(type);
        for (Type anInterface : interfaces) {
            if (!types.contains(anInterface)) {
                types.add(anInterface);
            }
        }
        types.add(type);
    }
    if (current.getGenericSuperclass() != null) {
        types.add(current.getGenericSuperclass());
    }
    for (Type type : types) {
        Class subClass;
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type[] args = pt.getActualTypeArguments();
            Class clazz = (Class) pt.getRawType();
            TypeVariable[] vars = clazz.getTypeParameters();
            for (int i = 0; i < vars.length; i++) {
                TypeVariable var = vars[i];
                Type mappedType = args[i];
                mappings.put(var, mappedType);
            }
            subClass = (Class) pt.getRawType();
        } else {
            subClass = (Class) type;
        }
        Type resolvedType = resolveTypeVariable(name, declaringClass, mappings, subClass);
        if (resolvedType != null) {
            return resolvedType;
        }
    }
    return null;
}
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) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)605 Type (java.lang.reflect.Type)456 GenericArrayType (java.lang.reflect.GenericArrayType)203 WildcardType (java.lang.reflect.WildcardType)162 TypeVariable (java.lang.reflect.TypeVariable)126 ArrayList (java.util.ArrayList)79 Method (java.lang.reflect.Method)60 Test (org.junit.Test)55 List (java.util.List)45 Field (java.lang.reflect.Field)43 Map (java.util.Map)39 HashMap (java.util.HashMap)32 Collection (java.util.Collection)18 Annotation (java.lang.annotation.Annotation)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 HashSet (java.util.HashSet)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 GenericType (javax.ws.rs.core.GenericType)10 MediaType (javax.ws.rs.core.MediaType)10