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;
}
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;
}
Aggregations