Search in sources :

Example 26 with WildcardType

use of java.lang.reflect.WildcardType in project cxf by apache.

the class ASMHelper method getClassCode.

public static String getClassCode(java.lang.reflect.Type type) {
    if (type instanceof Class) {
        return getClassCode((Class<?>) type);
    } else if (type instanceof GenericArrayType) {
        GenericArrayType at = (GenericArrayType) type;
        return "[" + getClassCode(at.getGenericComponentType());
    } else if (type instanceof TypeVariable) {
        TypeVariable<?> tv = (TypeVariable<?>) type;
        java.lang.reflect.Type[] bounds = tv.getBounds();
        if (bounds != null && bounds.length == 1) {
            return getClassCode(bounds[0]);
        }
        throw new IllegalArgumentException("Unable to determine type for: " + tv);
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        StringBuilder a = new StringBuilder(getClassCode(pt.getRawType()));
        if (!pt.getRawType().equals(Enum.class)) {
            a.setLength(a.length() - 1);
            a.append('<');
            for (java.lang.reflect.Type t : pt.getActualTypeArguments()) {
                a.append(getClassCode(t));
            }
            a.append(">;");
        }
        return a.toString();
    } else if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;
        StringBuilder a = new StringBuilder();
        java.lang.reflect.Type[] lowBounds = wt.getLowerBounds();
        java.lang.reflect.Type[] upBounds = wt.getUpperBounds();
        for (java.lang.reflect.Type t : upBounds) {
            a.append("+");
            a.append(getClassCode(t));
        }
        for (java.lang.reflect.Type t : lowBounds) {
            a.append("-");
            a.append(getClassCode(t));
        }
        return a.toString();
    }
    return null;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable)

Example 27 with WildcardType

use of java.lang.reflect.WildcardType in project SpongeCommon by SpongePowered.

the class TypeTokenHelper method isAssignable.

private static boolean isAssignable(Type type, ParameterizedType toType, @Nullable Type parent, int index) {
    if (type instanceof Class) {
        final Class<?> other = (Class<?>) type;
        final Class<?> toRaw = (Class<?>) toType.getRawType();
        final Type toEnclosing = toType.getOwnerType();
        if (toEnclosing != null && !Modifier.isStatic(toRaw.getModifiers())) {
            final Class<?> otherEnclosing = other.getEnclosingClass();
            if (otherEnclosing == null || !isAssignable(otherEnclosing, toEnclosing, null, 0)) {
                return false;
            }
        }
        if (!toRaw.isAssignableFrom(other)) {
            return false;
        }
        // Check if the default generic parameters match the parameters
        // of the parameterized type
        final Type[] toTypes = toType.getActualTypeArguments();
        final TypeVariable[] types = toRaw.getTypeParameters();
        if (types.length != toTypes.length) {
            return false;
        }
        for (int i = 0; i < types.length; i++) {
            if (!isAssignable(types[i], toTypes[i], other, i)) {
                return false;
            }
        }
        return true;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType other = (ParameterizedType) type;
        final Class<?> otherRaw = (Class<?>) other.getRawType();
        final Class<?> toRaw = (Class<?>) toType.getRawType();
        if (!toRaw.isAssignableFrom(otherRaw)) {
            return false;
        }
        final Type toEnclosing = toType.getOwnerType();
        if (toEnclosing != null && !Modifier.isStatic(toRaw.getModifiers())) {
            final Type otherEnclosing = other.getOwnerType();
            if (otherEnclosing == null || !isAssignable(otherEnclosing, toEnclosing, null, 0)) {
                return false;
            }
        }
        final Type[] types;
        if (otherRaw.equals(toRaw)) {
            types = other.getActualTypeArguments();
        } else {
            // Get the type parameters based on the super class
            other = (ParameterizedType) TypeToken.of(type).getSupertype((Class) toRaw).getType();
            types = other.getActualTypeArguments();
        }
        final Type[] toTypes = toType.getActualTypeArguments();
        if (types.length != toTypes.length) {
            return false;
        }
        for (int i = 0; i < types.length; i++) {
            if (!isAssignable(types[i], toTypes[i], other, i)) {
                return false;
            }
        }
        return true;
    }
    if (type instanceof TypeVariable) {
        final TypeVariable other = (TypeVariable) type;
        return allSupertypes(toType, other.getBounds());
    }
    if (type instanceof WildcardType) {
        final WildcardType other = (WildcardType) type;
        return allWildcardSupertypes(toType, other.getUpperBounds(), parent, index) && allAssignable(toType, other.getLowerBounds());
    }
    if (type instanceof GenericArrayType) {
        final GenericArrayType other = (GenericArrayType) type;
        final Class<?> rawType = (Class<?>) toType.getRawType();
        return rawType.equals(Object.class) || (rawType.isArray() && isAssignable(other.getGenericComponentType(), rawType.getComponentType(), parent, index));
    }
    throw new IllegalStateException("Unsupported type: " + type);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) CatalogType(org.spongepowered.api.CatalogType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 28 with WildcardType

use of java.lang.reflect.WildcardType in project BaseProject by feer921.

the class TypeUtils 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 : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof GenericArrayType) {
            GenericArrayType original = (GenericArrayType) toResolve;
            Type componentType = original.getGenericComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(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 ? new ParameterizedTypeImpl(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 new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { lowerBound });
                }
            } else if (originalUpperBound.length == 1) {
                Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
                if (upperBound != originalUpperBound[0]) {
                    return new WildcardTypeImpl(new Type[] { upperBound }, EMPTY_TYPE_ARRAY);
                }
            }
            return original;
        } else {
            return toResolve;
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 29 with WildcardType

use of java.lang.reflect.WildcardType in project BaseProject by feer921.

the class TypeUtils method getParameterUpperBound.

public static Type getParameterUpperBound(int index, ParameterizedType type) {
    Type[] types = type.getActualTypeArguments();
    if (index < 0 || index >= types.length) {
        throw new IllegalArgumentException("Index " + index + " not in range [0," + types.length + ") for " + type);
    }
    Type paramType = types[index];
    if (paramType instanceof WildcardType) {
        return ((WildcardType) paramType).getUpperBounds()[0];
    }
    return paramType;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType)

Example 30 with WildcardType

use of java.lang.reflect.WildcardType in project BaseProject by feer921.

the class TypeUtils method getRawType.

public static Class<?> getRawType(Type type) {
    if (type == null)
        throw new NullPointerException("type == null");
    if (type instanceof Class<?>) {
        // Type is a normal class.
        return (Class<?>) type;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        // I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
        // suspects some pathological case related to nested classes exists.
        Type rawType = parameterizedType.getRawType();
        if (!(rawType instanceof Class))
            throw new IllegalArgumentException();
        return (Class<?>) rawType;
    }
    if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        return Array.newInstance(getRawType(componentType), 0).getClass();
    }
    if (type instanceof TypeVariable) {
        // type that's more general than necessary is okay.
        return Object.class;
    }
    if (type instanceof WildcardType) {
        return getRawType(((WildcardType) type).getUpperBounds()[0]);
    }
    throw new IllegalArgumentException("Expected a Class, ParameterizedType, or " + "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

WildcardType (java.lang.reflect.WildcardType)236 ParameterizedType (java.lang.reflect.ParameterizedType)208 Type (java.lang.reflect.Type)180 GenericArrayType (java.lang.reflect.GenericArrayType)160 TypeVariable (java.lang.reflect.TypeVariable)134 Test (org.junit.Test)24 GenericClass (org.evosuite.utils.generic.GenericClass)14 Method (java.lang.reflect.Method)10 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 CaptureType (com.googlecode.gentyref.CaptureType)7 JSONException (com.alibaba.fastjson.JSONException)5 JSONType (com.alibaba.fastjson.annotation.JSONType)5 CatalogType (org.spongepowered.api.CatalogType)4 GenericDeclaration (java.lang.reflect.GenericDeclaration)3 HashSet (java.util.HashSet)3 List (java.util.List)3 MediaType (javax.ws.rs.core.MediaType)3