Search in sources :

Example 21 with WildcardType

use of java.lang.reflect.WildcardType in project uavstack by uavorg.

the class DefaultJSONParser method parseArrayWithType.

public Object parseArrayWithType(Type collectionType) {
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    Type[] actualTypes = ((ParameterizedType) collectionType).getActualTypeArguments();
    if (actualTypes.length != 1) {
        throw new JSONException("not support type " + collectionType);
    }
    Type actualTypeArgument = actualTypes[0];
    if (actualTypeArgument instanceof Class) {
        List<Object> array = new ArrayList<Object>();
        this.parseArray((Class<?>) actualTypeArgument, array);
        return array;
    }
    if (actualTypeArgument instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) actualTypeArgument;
        // assert wildcardType.getUpperBounds().length == 1;
        Type upperBoundType = wildcardType.getUpperBounds()[0];
        // assert upperBoundType instanceof Class;
        if (Object.class.equals(upperBoundType)) {
            if (wildcardType.getLowerBounds().length == 0) {
                // Collection<?>
                return parse();
            } else {
                throw new JSONException("not support type : " + collectionType);
            }
        }
        List<Object> array = new ArrayList<Object>();
        this.parseArray((Class<?>) upperBoundType, array);
        return array;
    // throw new JSONException("not support type : " +
    // collectionType);return parse();
    }
    if (actualTypeArgument instanceof TypeVariable) {
        TypeVariable<?> typeVariable = (TypeVariable<?>) actualTypeArgument;
        Type[] bounds = typeVariable.getBounds();
        if (bounds.length != 1) {
            throw new JSONException("not support : " + typeVariable);
        }
        Type boundType = bounds[0];
        if (boundType instanceof Class) {
            List<Object> array = new ArrayList<Object>();
            this.parseArray((Class<?>) boundType, array);
            return array;
        }
    }
    if (actualTypeArgument instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) actualTypeArgument;
        List<Object> array = new ArrayList<Object>();
        this.parseArray(parameterizedType, array);
        return array;
    }
    throw new JSONException("TODO : " + collectionType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList) JSONException(com.alibaba.fastjson.JSONException) JSONObject(com.alibaba.fastjson.JSONObject)

Example 22 with WildcardType

use of java.lang.reflect.WildcardType in project uavstack by uavorg.

the class ParserConfig method getDeserializer.

public ObjectDeserializer getDeserializer(Class<?> clazz, Type type) {
    ObjectDeserializer derializer = derializers.get(type);
    if (derializer != null) {
        return derializer;
    }
    if (type == null) {
        type = clazz;
    }
    derializer = derializers.get(type);
    if (derializer != null) {
        return derializer;
    }
    {
        JSONType annotation = clazz.getAnnotation(JSONType.class);
        if (annotation != null) {
            Class<?> mappingTo = annotation.mappingTo();
            if (mappingTo != Void.class) {
                return getDeserializer(mappingTo, mappingTo);
            }
        }
    }
    if (type instanceof WildcardType || type instanceof TypeVariable || type instanceof ParameterizedType) {
        derializer = derializers.get(clazz);
    }
    if (derializer != null) {
        return derializer;
    }
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {
        for (AutowiredObjectDeserializer autowired : ServiceLoader.load(AutowiredObjectDeserializer.class, classLoader)) {
            for (Type forType : autowired.getAutowiredFor()) {
                derializers.put(forType, autowired);
            }
        }
    } catch (Exception ex) {
    // skip
    }
    derializer = derializers.get(type);
    if (derializer != null) {
        return derializer;
    }
    if (clazz.isEnum()) {
        derializer = new EnumDeserializer(clazz);
    } else if (clazz.isArray()) {
        derializer = ArrayDeserializer.instance;
    } else if (clazz == Set.class || clazz == HashSet.class || clazz == Collection.class || clazz == List.class || clazz == ArrayList.class) {
        derializer = CollectionDeserializer.instance;
    } else if (Collection.class.isAssignableFrom(clazz)) {
        derializer = CollectionDeserializer.instance;
    } else if (Map.class.isAssignableFrom(clazz)) {
        derializer = MapDeserializer.instance;
    } else if (Throwable.class.isAssignableFrom(clazz)) {
        derializer = new ThrowableDeserializer(this, clazz);
    } else {
        derializer = createJavaBeanDeserializer(clazz, type);
    }
    putDeserializer(type, derializer);
    return derializer;
}
Also used : ThrowableDeserializer(com.alibaba.fastjson.parser.deserializer.ThrowableDeserializer) ArrayList(java.util.ArrayList) JSONException(com.alibaba.fastjson.JSONException) ASMException(com.alibaba.fastjson.asm.ASMException) AccessControlException(java.security.AccessControlException) ParameterizedType(java.lang.reflect.ParameterizedType) EnumDeserializer(com.alibaba.fastjson.parser.deserializer.EnumDeserializer) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JSONType(com.alibaba.fastjson.annotation.JSONType) AutowiredObjectDeserializer(com.alibaba.fastjson.parser.deserializer.AutowiredObjectDeserializer) TypeVariable(java.lang.reflect.TypeVariable) Collection(java.util.Collection) JSONType(com.alibaba.fastjson.annotation.JSONType) JSONObjectDeserializer(com.alibaba.fastjson.parser.deserializer.JSONObjectDeserializer) AutowiredObjectDeserializer(com.alibaba.fastjson.parser.deserializer.AutowiredObjectDeserializer) JavaObjectDeserializer(com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 23 with WildcardType

use of java.lang.reflect.WildcardType in project BIMserver by opensourceBIM.

the class SServicesMap method getGenericType.

@SuppressWarnings("restriction")
public Class<?> getGenericType(Method method) {
    Type genericReturnType = method.getGenericReturnType();
    if (method.getGenericReturnType() instanceof ParameterizedType) {
        ParameterizedType parameterizedTypeImpl = (ParameterizedType) genericReturnType;
        Type first = parameterizedTypeImpl.getActualTypeArguments()[0];
        if (first instanceof WildcardType) {
            return null;
        } else if (first instanceof ParameterizedType) {
            return null;
        } else if (first instanceof sun.reflect.generics.reflectiveObjects.TypeVariableImpl) {
            sun.reflect.generics.reflectiveObjects.TypeVariableImpl<?> typeVariableImpl = (sun.reflect.generics.reflectiveObjects.TypeVariableImpl<?>) first;
            GenericDeclaration genericDeclaration = typeVariableImpl.getGenericDeclaration();
            if (genericDeclaration instanceof Class) {
                return (Class<?>) genericDeclaration;
            }
        } else {
            return (Class<?>) first;
        }
    }
    return (Class<?>) method.getGenericReturnType();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 24 with WildcardType

use of java.lang.reflect.WildcardType in project bnd by bndtools.

the class ClassParserTest method print.

public static void print(Appendable sb, Type t) throws Exception {
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        Class<?> c = (Class<?>) pt.getRawType();
        sb.append("L");
        sb.append(c.getCanonicalName().replace('.', '/'));
        sb.append("<");
        for (Type arg : pt.getActualTypeArguments()) {
            print(sb, arg);
        }
        sb.append(">");
        sb.append(";");
        return;
    } else if (t instanceof WildcardType) {
        sb.append("yyyy");
    } else if (t instanceof GenericArrayType) {
        sb.append("[" + ((GenericArrayType) t).getGenericComponentType());
    } else if (t instanceof TypeVariable) {
        TypeVariable<?> tv = (TypeVariable<?>) t;
        sb.append("T");
        sb.append(tv.getName());
        for (Type upper : tv.getBounds()) {
            if (upper != Object.class) {
                sb.append(":");
                print(sb, upper);
            }
        }
        sb.append(";");
    } else {
        Class<?> c = (Class<?>) t;
        sb.append("L");
        sb.append(c.getCanonicalName().replace('.', '/'));
        if (c instanceof GenericDeclaration) {
            GenericDeclaration gd = c;
            if (gd.getTypeParameters().length != 0) {
                sb.append("<");
                for (Type arg : gd.getTypeParameters()) {
                    print(sb, arg);
                }
                sb.append(">");
            }
        }
        sb.append(";");
    }
}
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) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 25 with WildcardType

use of java.lang.reflect.WildcardType in project gradle by gradle.

the class AsmBackedClassGeneratorTest method includesGenericTypeInformationForOverriddenConstructor.

@Test
public void includesGenericTypeInformationForOverriddenConstructor() throws Exception {
    Class<?> generatedClass = generator.generate(BeanWithComplexConstructor.class);
    Constructor<?> constructor = generatedClass.getDeclaredConstructors()[0];
    assertThat(constructor.getTypeParameters().length, equalTo(3));
    assertThat(constructor.getGenericParameterTypes().length, equalTo(12));
    // Callable
    Type paramType = constructor.getGenericParameterTypes()[0];
    assertThat(paramType, equalTo((Type) Callable.class));
    // Callable<String>
    paramType = constructor.getGenericParameterTypes()[1];
    assertThat(paramType, instanceOf(ParameterizedType.class));
    ParameterizedType parameterizedType = (ParameterizedType) paramType;
    assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
    assertThat(parameterizedType.getActualTypeArguments()[0], equalTo((Type) String.class));
    // Callable<? extends String>
    paramType = constructor.getGenericParameterTypes()[2];
    assertThat(paramType, instanceOf(ParameterizedType.class));
    parameterizedType = (ParameterizedType) paramType;
    assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
    assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    WildcardType wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
    assertThat(wildcard.getUpperBounds().length, equalTo(1));
    assertThat(wildcard.getUpperBounds()[0], equalTo((Type) String.class));
    assertThat(wildcard.getLowerBounds().length, equalTo(0));
    // Callable<? super String>
    paramType = constructor.getGenericParameterTypes()[3];
    assertThat(paramType, instanceOf(ParameterizedType.class));
    parameterizedType = (ParameterizedType) paramType;
    assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
    assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
    assertThat(wildcard.getUpperBounds().length, equalTo(1));
    assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
    assertThat(wildcard.getLowerBounds().length, equalTo(1));
    assertThat(wildcard.getLowerBounds()[0], equalTo((Type) String.class));
    // Callable<?>
    paramType = constructor.getGenericParameterTypes()[4];
    assertThat(paramType, instanceOf(ParameterizedType.class));
    parameterizedType = (ParameterizedType) paramType;
    assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
    assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
    assertThat(wildcard.getUpperBounds().length, equalTo(1));
    assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
    assertThat(wildcard.getLowerBounds().length, equalTo(0));
    // Callable<? extends Callable<?>>
    paramType = constructor.getGenericParameterTypes()[5];
    assertThat(paramType, instanceOf(ParameterizedType.class));
    parameterizedType = (ParameterizedType) paramType;
    assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
    assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
    assertThat(wildcard.getUpperBounds().length, equalTo(1));
    assertThat(wildcard.getLowerBounds().length, equalTo(0));
    assertThat(wildcard.getUpperBounds()[0], instanceOf(ParameterizedType.class));
    parameterizedType = (ParameterizedType) wildcard.getUpperBounds()[0];
    assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
    assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
    assertThat(wildcard.getUpperBounds().length, equalTo(1));
    assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
    assertThat(wildcard.getLowerBounds().length, equalTo(0));
    // Callable<S>
    paramType = constructor.getGenericParameterTypes()[6];
    assertThat(paramType, instanceOf(ParameterizedType.class));
    parameterizedType = (ParameterizedType) paramType;
    assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
    assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(TypeVariable.class));
    TypeVariable typeVariable = (TypeVariable) parameterizedType.getActualTypeArguments()[0];
    assertThat(typeVariable.getName(), equalTo("S"));
    assertThat(typeVariable.getBounds()[0], instanceOf(ParameterizedType.class));
    // Callable<? extends T>
    paramType = constructor.getGenericParameterTypes()[7];
    assertThat(paramType, instanceOf(ParameterizedType.class));
    parameterizedType = (ParameterizedType) paramType;
    assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
    assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
    assertThat(wildcard.getUpperBounds().length, equalTo(1));
    assertThat(wildcard.getLowerBounds().length, equalTo(0));
    assertThat(wildcard.getUpperBounds()[0], instanceOf(TypeVariable.class));
    typeVariable = (TypeVariable) wildcard.getUpperBounds()[0];
    assertThat(typeVariable.getName(), equalTo("T"));
    assertThat(typeVariable.getBounds()[0], equalTo((Type) IOException.class));
    // V
    paramType = constructor.getGenericParameterTypes()[8];
    assertThat(paramType, instanceOf(TypeVariable.class));
    typeVariable = (TypeVariable) paramType;
    assertThat(typeVariable.getName(), equalTo("V"));
    assertThat(typeVariable.getBounds()[0], equalTo((Type) Object.class));
    GenericArrayType arrayType;
    // String[]
    paramType = constructor.getGenericParameterTypes()[9];
    assertThat(paramType, equalTo((Type) String[].class));
    assertThat(((Class<?>) paramType).getComponentType(), equalTo((Type) String.class));
    // List<? extends String>[]
    paramType = constructor.getGenericParameterTypes()[10];
    assertThat(paramType, instanceOf(GenericArrayType.class));
    arrayType = (GenericArrayType) paramType;
    assertThat(arrayType.getGenericComponentType(), instanceOf(ParameterizedType.class));
    parameterizedType = (ParameterizedType) arrayType.getGenericComponentType();
    assertThat(parameterizedType.getRawType(), equalTo((Type) List.class));
    assertThat(parameterizedType.getActualTypeArguments().length, equalTo(1));
    assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    // boolean
    paramType = constructor.getGenericParameterTypes()[11];
    assertThat(paramType, equalTo((Type) Boolean.TYPE));
    assertThat(constructor.getGenericExceptionTypes().length, equalTo(2));
    // throws Exception
    Type exceptionType = constructor.getGenericExceptionTypes()[0];
    assertThat(exceptionType, equalTo((Type) Exception.class));
    // throws T
    exceptionType = constructor.getGenericExceptionTypes()[1];
    assertThat(exceptionType, instanceOf(TypeVariable.class));
    typeVariable = (TypeVariable) exceptionType;
    assertThat(typeVariable.getName(), equalTo("T"));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ElementType(java.lang.annotation.ElementType) 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) Test(org.junit.Test)

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