use of java.lang.reflect.ParameterizedType in project elasticsearch by elastic.
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<>(Initializables.of(value));
return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, emptySet(), value);
}
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);
}
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]);
}
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;
}
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;
}
Aggregations