Search in sources :

Example 1 with WildcardType

use of java.lang.reflect.WildcardType in project morphia by mongodb.

the class MappedField method toClass.

protected Class toClass(final Type t) {
    if (t == null) {
        return null;
    } else if (t instanceof Class) {
        return (Class) t;
    } else if (t instanceof GenericArrayType) {
        final Type type = ((GenericArrayType) t).getGenericComponentType();
        Class aClass;
        if (type instanceof ParameterizedType) {
            aClass = (Class) ((ParameterizedType) type).getRawType();
        } else if (type instanceof TypeVariable) {
            aClass = ReflectionUtils.getTypeArgument(persistedClass, (TypeVariable<?>) type);
            if (aClass == null) {
                aClass = Object.class;
            }
        } else {
            aClass = (Class) type;
        }
        return Array.newInstance(aClass, 0).getClass();
    } else if (t instanceof ParameterizedType) {
        return (Class) ((ParameterizedType) t).getRawType();
    } else if (t instanceof WildcardType) {
        return (Class) ((WildcardType) t).getUpperBounds()[0];
    }
    throw new RuntimeException("Generic TypeVariable not supported!");
}
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) DBObject(com.mongodb.DBObject) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 2 with WildcardType

use of java.lang.reflect.WildcardType in project okhttp-OkGo by jeasonlzy.

the class TypeUtils 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!
        return false;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 3 with WildcardType

use of java.lang.reflect.WildcardType in project okhttp-OkGo by jeasonlzy.

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 4 with WildcardType

use of java.lang.reflect.WildcardType in project okhttp-OkGo by jeasonlzy.

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 5 with WildcardType

use of java.lang.reflect.WildcardType in project jodd by oblac.

the class ReflectUtil method typeToString.

private static void typeToString(StringBuilder sb, Type type, Set<Type> visited) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        final Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        sb.append(rawType.getName());
        boolean first = true;
        for (Type typeArg : parameterizedType.getActualTypeArguments()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append('<');
            typeToString(sb, typeArg, visited);
            sb.append('>');
        }
    } else if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        sb.append('?');
        // According to JLS(http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1):
        // - Lower and upper can't coexist: (for instance, this is not allowed: <? extends List<String> & super MyInterface>)
        // - Multiple bounds are not supported (for instance, this is not allowed: <? extends List<String> & MyInterface>)
        final Type bound;
        if (wildcardType.getLowerBounds().length != 0) {
            sb.append(" super ");
            bound = wildcardType.getLowerBounds()[0];
        } else {
            sb.append(" extends ");
            bound = wildcardType.getUpperBounds()[0];
        }
        typeToString(sb, bound, visited);
    } else if (type instanceof TypeVariable<?>) {
        TypeVariable<?> typeVariable = (TypeVariable<?>) type;
        sb.append(typeVariable.getName());
        if (!visited.contains(type)) {
            visited.add(type);
            sb.append(" extends ");
            boolean first = true;
            for (Type bound : typeVariable.getBounds()) {
                if (first) {
                    first = false;
                } else {
                    sb.append(" & ");
                }
                typeToString(sb, bound, visited);
            }
            visited.remove(type);
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        typeToString(genericArrayType.getGenericComponentType());
        sb.append(genericArrayType.getGenericComponentType());
        sb.append("[]");
    } else if (type instanceof Class) {
        Class<?> typeClass = (Class<?>) type;
        sb.append(typeClass.getName());
    } else {
        throw new IllegalArgumentException("Unsupported type: " + type);
    }
}
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