use of java.lang.reflect.WildcardType in project configuration-as-code-plugin by jenkinsci.
the class BaseConfigurator method detectActualType.
protected Attribute detectActualType(String name, Type type) {
Class c = null;
boolean multiple = false;
if (type instanceof GenericArrayType) {
// type is a parameterized array: <Foo>[]
multiple = true;
GenericArrayType at = (GenericArrayType) type;
type = at.getGenericComponentType();
}
while (type instanceof ParameterizedType) {
// type is parameterized `Some<Foo>`
ParameterizedType pt = (ParameterizedType) type;
Class rawType = (Class) pt.getRawType();
if (Collection.class.isAssignableFrom(rawType)) {
// type is `Collection<Foo>`
multiple = true;
}
type = pt.getActualTypeArguments()[0];
if (type instanceof WildcardType) {
// pt is Some<? extends Foo>
Type t = ((WildcardType) type).getUpperBounds()[0];
if (t == Object.class) {
// pt is Some<?>, so we actually want "Some"
type = pt.getRawType();
} else {
type = t;
}
}
}
if (type instanceof ParameterizedType) {
final Type[] arguments = ((ParameterizedType) type).getActualTypeArguments();
type = ((ParameterizedType) type).getRawType();
}
while (c == null) {
if (type instanceof Class) {
c = (Class) type;
} else if (type instanceof TypeVariable) {
// type is declared as parameterized type
// unfortunately, java reflection doesn't allow to get the actual parameter type
// so, if superclass it parameterized, we assume parameter type match
// i.e target is Foo extends AbtractFoo<Bar> with
// public abstract class AbtractFoo<T> { void setBar(T bar) }
final Type superclass = getTarget().getGenericSuperclass();
if (superclass instanceof ParameterizedType) {
final ParameterizedType psc = (ParameterizedType) superclass;
type = psc.getActualTypeArguments()[0];
continue;
} else {
c = (Class) ((TypeVariable) type).getBounds()[0];
}
TypeVariable tv = (TypeVariable) type;
} else {
throw new IllegalStateException("Unable to detect type of attribute " + getTarget() + '#' + name);
}
}
if (c.isArray()) {
multiple = true;
c = c.getComponentType();
}
Attribute attribute;
if (!c.isPrimitive() && !c.isEnum() && Modifier.isAbstract(c.getModifiers())) {
if (!Describable.class.isAssignableFrom(c)) {
// Not a Describable, so probably not an attribute expected to be selected as sub-component
return null;
}
attribute = new DescribableAttribute<T>(name, c);
} else {
attribute = new Attribute<T>(name, c);
}
attribute.multiple(multiple);
return attribute;
}
use of java.lang.reflect.WildcardType in project PyrCore by PYRRH4.
the class $Gson$Types 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;
}
ParameterizedType pa = (ParameterizedType) a;
ParameterizedType pb = (ParameterizedType) b;
return 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() == 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;
}
}
use of java.lang.reflect.WildcardType in project PyrCore by PYRRH4.
the class $Gson$Types method resolve.
public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
// this implementation is made a little more complicated in an attempt to avoid object-creation
while (true) {
if (toResolve instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
if (toResolve == typeVariable) {
return toResolve;
}
} else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
Class<?> original = (Class<?>) toResolve;
Type componentType = original.getComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(newComponentType);
} else if (toResolve instanceof GenericArrayType) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(newComponentType);
} else if (toResolve instanceof ParameterizedType) {
ParameterizedType original = (ParameterizedType) toResolve;
Type ownerType = original.getOwnerType();
Type newOwnerType = resolve(context, contextRawType, ownerType);
boolean changed = newOwnerType != ownerType;
Type[] args = original.getActualTypeArguments();
for (int t = 0, length = args.length; t < length; t++) {
Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
if (resolvedTypeArgument != args[t]) {
if (!changed) {
args = args.clone();
changed = true;
}
args[t] = resolvedTypeArgument;
}
}
return changed ? 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 = resolve(context, contextRawType, originalLowerBound[0]);
if (lowerBound != originalLowerBound[0]) {
return supertypeOf(lowerBound);
}
} else if (originalUpperBound.length == 1) {
Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
if (upperBound != originalUpperBound[0]) {
return subtypeOf(upperBound);
}
}
return original;
} else {
return toResolve;
}
}
}
use of java.lang.reflect.WildcardType in project records-management by Alfresco.
the class PublicAPITestUtil method getClassesFromType.
/**
* Find all classes that are within the supplied type. For example a {@code Pair<Set<String>, Integer>} contains
* references to four classes.
*
* @param type The type to examine.
* @param processedTypes The set of types which have already been processed. If {@code type} is within this set then
* the method returns an empty set, to prevent analysis of the same type multiple times, and to guard
* against circular references. The underlying set is updated with the given type.
* @return The set of classes used to form the given type.
*/
private static Set<Class<?>> getClassesFromType(Type type, Set<Type> processedTypes) {
Set<Class<?>> returnClasses = new HashSet<>();
if (processedTypes.add(type)) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
returnClasses.add((Class<?>) parameterizedType.getRawType());
for (Type t : parameterizedType.getActualTypeArguments()) {
returnClasses.addAll(getClassesFromType(t, processedTypes));
}
} else if (type instanceof Class) {
Class<?> clazz = (Class<?>) type;
if (clazz.isArray()) {
returnClasses.add(clazz.getComponentType());
}
returnClasses.add(clazz);
} else if (type instanceof WildcardType) {
// No-op - Caller can choose what type to use.
} else if (type instanceof TypeVariable<?>) {
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
for (Type bound : typeVariable.getBounds()) {
returnClasses.addAll(getClassesFromType(bound, processedTypes));
}
} else if (type instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) type;
returnClasses.addAll(getClassesFromType(genericArrayType.getGenericComponentType(), processedTypes));
} else {
throw new IllegalStateException("This test was not written to work with type " + type);
}
}
return returnClasses;
}
use of java.lang.reflect.WildcardType in project java-common-lib by sosy-lab.
the class Classes method extractUpperBoundFromType.
/**
* Simplify a {@link Type} instance: if it is a wildcard generic type, replace it with its upper
* bound.
*
* <p>It does not support wildcards with several upper bounds.
*
* @param type A possibly generic type.
* @return The type or its simplification.
*/
public static Type extractUpperBoundFromType(@Var Type type) {
checkNotNull(type);
if (type instanceof WildcardType) {
WildcardType wcType = (WildcardType) type;
if (wcType.getLowerBounds().length > 0) {
throw new UnsupportedOperationException("Currently wildcard types with a lower bound like \"" + type + "\" are not supported ");
}
Type[] upperBounds = ((WildcardType) type).getUpperBounds();
if (upperBounds.length != 1) {
throw new UnsupportedOperationException("Currently only type bounds with one upper bound are supported, not \"" + type + "\"");
}
type = upperBounds[0];
}
return type;
}
Aggregations