use of java.lang.reflect.GenericDeclaration in project XobotOS by xamarin.
the class ImplForVariable method resolve.
void resolve() {
if (formalVar != null) {
return;
}
GenericDeclaration curLayer = declOfVarUser;
TypeVariable var;
while ((var = findFormalVar(curLayer, name)) == null) {
curLayer = nextLayer(curLayer);
if (curLayer == null) {
throw new AssertionError("illegal type variable reference");
}
}
formalVar = (ImplForVariable<D>) var;
this.genericDeclaration = formalVar.genericDeclaration;
this.bounds = formalVar.bounds;
}
use of java.lang.reflect.GenericDeclaration in project commons-lang by apache.
the class TypeUtils method toLongString.
/**
* Format a {@link TypeVariable} including its {@link GenericDeclaration}.
*
* @param var the type variable to create a String representation for, not {@code null}
* @return String
* @since 3.2
*/
public static String toLongString(final TypeVariable<?> var) {
Validate.notNull(var, "var is null");
final StringBuilder buf = new StringBuilder();
final GenericDeclaration d = var.getGenericDeclaration();
if (d instanceof Class<?>) {
Class<?> c = (Class<?>) d;
while (true) {
if (c.getEnclosingClass() == null) {
buf.insert(0, c.getName());
break;
}
buf.insert(0, c.getSimpleName()).insert(0, '.');
c = c.getEnclosingClass();
}
} else if (d instanceof Type) {
// not possible as of now
buf.append(toString((Type) d));
} else {
buf.append(d);
}
return buf.append(':').append(typeVariableToString(var)).toString();
}
use of java.lang.reflect.GenericDeclaration in project Bytecoder by mirkosertic.
the class TypeVariableImpl method equals.
@Override
public boolean equals(Object o) {
if (o instanceof TypeVariable && o.getClass() == TypeVariableImpl.class) {
TypeVariable<?> that = (TypeVariable<?>) o;
GenericDeclaration thatDecl = that.getGenericDeclaration();
String thatName = that.getName();
return Objects.equals(genericDeclaration, thatDecl) && Objects.equals(name, thatName);
} else
return false;
}
use of java.lang.reflect.GenericDeclaration in project tomee by apache.
the class GenericsUtils method resolveTypeVariable.
private static Type resolveTypeVariable(final Type variable, final Class<?> clazz) {
// as there is nothing to resolve
if (!(variable instanceof TypeVariable)) {
return variable;
}
final TypeVariable<?> typeVariable = (TypeVariable<?>) variable;
// Where was this type variable declared?
final GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
// so if it isn't of type Class, return the unresolved variable
if (!(genericDeclaration instanceof Class)) {
return variable;
}
final Class<?> declaringClass = (Class<?>) genericDeclaration;
// Get the position of the variable in the generic signature
// where variable names are specified
final int typePosition = positionOf(variable, declaringClass.getTypeParameters());
// This shouldn't happen, but it did. Return the unresolved variable
if (typePosition == -1) {
return variable;
}
// Get the actual type arguments passed from the place where the declaringClass
// was used by clazz in either an 'extends' or 'implements' context
final Type[] actualTypes = genericTypes(clazz).filter(type -> type instanceof ParameterizedType).map(ParameterizedType.class::cast).filter(parameterizedType -> declaringClass.equals(parameterizedType.getRawType())).map(ParameterizedType::getActualTypeArguments).findFirst().orElse(null);
// feature gap in our code. Return the unresolved variable
if (actualTypes == null) {
return variable;
}
// Return the unresolved variable
if (actualTypes.length != declaringClass.getTypeParameters().length) {
return variable;
}
final Type resolvedType = actualTypes[typePosition];
return resolvedType;
}
use of java.lang.reflect.GenericDeclaration in project tomee by apache.
the class InjectionUtils method getSuperType.
public static Type getSuperType(Class<?> serviceClass, TypeVariable<?> var) {
int pos = 0;
GenericDeclaration genericDeclaration = var.getGenericDeclaration();
TypeVariable<?>[] vars = genericDeclaration.getTypeParameters();
for (; pos < vars.length; pos++) {
if (vars[pos].getName().equals(var.getName())) {
break;
}
}
ParameterizedType genericSubtype = findGenericDeclaration(genericDeclaration, serviceClass);
Type result = null;
if (genericSubtype != null) {
result = genericSubtype.getActualTypeArguments()[pos];
}
if (result instanceof TypeVariable) {
result = getSuperType(serviceClass, (TypeVariable<?>) result);
}
if (result == null || result == Object.class) {
for (Type bound : var.getBounds()) {
if (bound != Object.class) {
result = bound;
break;
}
}
}
return result;
}
Aggregations