Search in sources :

Example 16 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project mybatis-3 by mybatis.

the class TypeParameterResolverTest method testReturn_Lv2ArrayOfList.

@Test
public void testReturn_Lv2ArrayOfList() throws Exception {
    Class<?> clazz = Level2Mapper.class;
    Method method = clazz.getMethod("selectArrayOfList");
    Type result = TypeParameterResolver.resolveReturnType(method, clazz);
    assertTrue(result instanceof GenericArrayType);
    GenericArrayType genericArrayType = (GenericArrayType) result;
    assertTrue(genericArrayType.getGenericComponentType() instanceof ParameterizedType);
    ParameterizedType paramType = (ParameterizedType) genericArrayType.getGenericComponentType();
    assertEquals(List.class, paramType.getRawType());
    assertEquals(String.class, paramType.getActualTypeArguments()[0]);
}
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) Level2Mapper(org.apache.ibatis.reflection.typeparam.Level2Mapper) Method(java.lang.reflect.Method) GenericArrayType(java.lang.reflect.GenericArrayType) Test(org.junit.Test)

Example 17 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project mybatis-3 by mybatis.

the class TypeParameterResolverTest method testParam_Lv1Array.

@Test
public void testParam_Lv1Array() throws Exception {
    Class<?> clazz = Level1Mapper.class;
    Method method = clazz.getMethod("selectArray", List[].class);
    Type[] result = TypeParameterResolver.resolveParamTypes(method, clazz);
    assertTrue(result[0] instanceof GenericArrayType);
    GenericArrayType genericArrayType = (GenericArrayType) result[0];
    assertTrue(genericArrayType.getGenericComponentType() instanceof ParameterizedType);
    ParameterizedType paramType = (ParameterizedType) genericArrayType.getGenericComponentType();
    assertEquals(List.class, paramType.getRawType());
    assertEquals(String.class, paramType.getActualTypeArguments()[0]);
}
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) List(java.util.List) Method(java.lang.reflect.Method) GenericArrayType(java.lang.reflect.GenericArrayType) Level1Mapper(org.apache.ibatis.reflection.typeparam.Level1Mapper) Test(org.junit.Test)

Example 18 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project netty by netty.

the class TypeParameterMatcher method find0.

private static Class<?> find0(final Object object, Class<?> parameterizedSuperclass, String typeParamName) {
    final Class<?> thisClass = object.getClass();
    Class<?> currentClass = thisClass;
    for (; ; ) {
        if (currentClass.getSuperclass() == parameterizedSuperclass) {
            int typeParamIndex = -1;
            TypeVariable<?>[] typeParams = currentClass.getSuperclass().getTypeParameters();
            for (int i = 0; i < typeParams.length; i++) {
                if (typeParamName.equals(typeParams[i].getName())) {
                    typeParamIndex = i;
                    break;
                }
            }
            if (typeParamIndex < 0) {
                throw new IllegalStateException("unknown type parameter '" + typeParamName + "': " + parameterizedSuperclass);
            }
            Type genericSuperType = currentClass.getGenericSuperclass();
            if (!(genericSuperType instanceof ParameterizedType)) {
                return Object.class;
            }
            Type[] actualTypeParams = ((ParameterizedType) genericSuperType).getActualTypeArguments();
            Type actualTypeParam = actualTypeParams[typeParamIndex];
            if (actualTypeParam instanceof ParameterizedType) {
                actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
            }
            if (actualTypeParam instanceof Class) {
                return (Class<?>) actualTypeParam;
            }
            if (actualTypeParam instanceof GenericArrayType) {
                Type componentType = ((GenericArrayType) actualTypeParam).getGenericComponentType();
                if (componentType instanceof ParameterizedType) {
                    componentType = ((ParameterizedType) componentType).getRawType();
                }
                if (componentType instanceof Class) {
                    return Array.newInstance((Class<?>) componentType, 0).getClass();
                }
            }
            if (actualTypeParam instanceof TypeVariable) {
                // Resolved type parameter points to another type parameter.
                TypeVariable<?> v = (TypeVariable<?>) actualTypeParam;
                currentClass = thisClass;
                if (!(v.getGenericDeclaration() instanceof Class)) {
                    return Object.class;
                }
                parameterizedSuperclass = (Class<?>) v.getGenericDeclaration();
                typeParamName = v.getName();
                if (parameterizedSuperclass.isAssignableFrom(thisClass)) {
                    continue;
                } else {
                    return Object.class;
                }
            }
            return fail(thisClass, typeParamName);
        }
        currentClass = currentClass.getSuperclass();
        if (currentClass == null) {
            return fail(thisClass, typeParamName);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 19 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project neo4j by neo4j.

the class PluginPointFactoryImpl method parameterExtractor.

private static ParameterExtractor parameterExtractor(Type type, Parameter parameter, Description description) {
    if (type instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) type;
        Class<?> raw = (Class<?>) paramType.getRawType();
        Type componentType = paramType.getActualTypeArguments()[0];
        Class<?> component = null;
        if (componentType instanceof Class<?>) {
            component = (Class<?>) componentType;
        }
        if (Set.class == raw) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return new HashSet<Object>(Arrays.asList(result));
                    }
                };
            }
        } else if (List.class == raw || Collection.class == raw || Iterable.class == raw) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return Arrays.asList(result);
                    }
                };
            }
        }
    } else if (type instanceof Class<?>) {
        Class<?> raw = (Class<?>) type;
        if (raw.isArray()) {
            TypeCaster caster = TYPES.get(raw.getComponentType());
            if (caster != null) {
                return new ListParameterExtractor(caster, raw.getComponentType(), parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return result;
                    }
                };
            }
        } else {
            TypeCaster caster = TYPES.get(raw);
            if (caster != null) {
                return new ParameterExtractor(caster, raw, parameter, description);
            }
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType array = (GenericArrayType) type;
        Type component = array.getGenericComponentType();
        if (component instanceof Class<?>) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, (Class<?>) component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return result;
                    }
                };
            }
        }
    }
    throw new IllegalStateException("Unsupported parameter type: " + type);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) List(java.util.List) HashSet(java.util.HashSet)

Example 20 with GenericArrayType

use of java.lang.reflect.GenericArrayType 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

GenericArrayType (java.lang.reflect.GenericArrayType)126 ParameterizedType (java.lang.reflect.ParameterizedType)120 Type (java.lang.reflect.Type)97 TypeVariable (java.lang.reflect.TypeVariable)75 WildcardType (java.lang.reflect.WildcardType)72 Field (java.lang.reflect.Field)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ElementType (java.lang.annotation.ElementType)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GenericType (javax.ws.rs.core.GenericType)3 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)3 EqualsTester (com.google.common.testing.EqualsTester)2 TypeLiteral (com.google.inject.TypeLiteral)2 DBObject (com.mongodb.DBObject)2