Search in sources :

Example 11 with GenericDeclaration

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;
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 12 with GenericDeclaration

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();
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 13 with GenericDeclaration

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;
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 14 with GenericDeclaration

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;
}
Also used : GenericDeclaration(java.lang.reflect.GenericDeclaration) Objects(java.util.Objects) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Optional(java.util.Optional) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 15 with GenericDeclaration

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Aggregations

GenericDeclaration (java.lang.reflect.GenericDeclaration)19 TypeVariable (java.lang.reflect.TypeVariable)18 Type (java.lang.reflect.Type)14 ParameterizedType (java.lang.reflect.ParameterizedType)13 GenericArrayType (java.lang.reflect.GenericArrayType)9 WildcardType (java.lang.reflect.WildcardType)6 MediaType (javax.ws.rs.core.MediaType)2 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 GenericArrayTypeImpl (sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl)1