Search in sources :

Example 56 with GenericArrayType

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

use of java.lang.reflect.GenericArrayType in project dubbo by alibaba.

the class ClassUtils method getGenericClass.

public static Class<?> getGenericClass(Class<?> cls, int i) {
    try {
        ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
        Object genericClass = parameterizedType.getActualTypeArguments()[i];
        if (genericClass instanceof ParameterizedType) {
            return (Class<?>) ((ParameterizedType) genericClass).getRawType();
        } else if (genericClass instanceof GenericArrayType) {
            return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
        } else if (genericClass != null) {
            return (Class<?>) genericClass;
        }
    } catch (Throwable e) {
    }
    if (cls.getSuperclass() != null) {
        return getGenericClass(cls.getSuperclass(), i);
    } else {
        throw new IllegalArgumentException(cls.getName() + " generic type undefined!");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 58 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project dubbo by alibaba.

the class ReflectUtils method getGenericClass.

public static Class<?> getGenericClass(Class<?> cls, int i) {
    try {
        ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
        Object genericClass = parameterizedType.getActualTypeArguments()[i];
        if (genericClass instanceof ParameterizedType) {
            // handle nested generic type
            return (Class<?>) ((ParameterizedType) genericClass).getRawType();
        } else if (genericClass instanceof GenericArrayType) {
            // handle array generic type
            return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
        } else if (((Class) genericClass).isArray()) {
            // Requires JDK 7 or higher, Foo<int[]> is no longer GenericArrayType
            return ((Class) genericClass).getComponentType();
        } else {
            return (Class<?>) genericClass;
        }
    } catch (Throwable e) {
        throw new IllegalArgumentException(cls.getName() + " generic type undefined!", e);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CtClass(javassist.CtClass) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 59 with GenericArrayType

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

Example 60 with GenericArrayType

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

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)137 ParameterizedType (java.lang.reflect.ParameterizedType)131 Type (java.lang.reflect.Type)105 TypeVariable (java.lang.reflect.TypeVariable)78 WildcardType (java.lang.reflect.WildcardType)76 Field (java.lang.reflect.Field)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)4 Collection (java.util.Collection)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ElementType (java.lang.annotation.ElementType)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GenericType (javax.ws.rs.core.GenericType)3 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)3 XmlType (javax.xml.bind.annotation.XmlType)3 Holder (javax.xml.ws.Holder)3 EqualsTester (com.google.common.testing.EqualsTester)2