Search in sources :

Example 71 with GenericArrayType

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

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)

Example 72 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project silk by jbee.

the class Type method type.

private static Type<?> type(java.lang.reflect.Type type, Map<String, Type<?>> actualTypeArguments) {
    if (type instanceof Class<?>) {
        return raw((Class<?>) type);
    }
    if (type instanceof ParameterizedType) {
        return parameterizedType((ParameterizedType) type, actualTypeArguments);
    }
    if (type instanceof TypeVariable<?>) {
        return actualTypeArguments.get(((TypeVariable<?>) type).getName());
    }
    if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        return type(gat.getGenericComponentType()).addArrayDimension();
    }
    if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;
        java.lang.reflect.Type[] upperBounds = wt.getUpperBounds();
        if (upperBounds.length == 1) {
            return type(upperBounds[0]).asUpperBound();
        }
    }
    throw new UnsupportedOperationException("Type has no support yet: " + type);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 73 with GenericArrayType

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

the class ReflectionHelper method getParameterizedClassArguments.

/**
     * Get the parameterized class arguments for a declaring class that
     * declares a generic interface type.
     *
     * @param p the declaring class
     * @return the parameterized class arguments, or null if the generic
     * interface type is not a parameterized type.
     */
public static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p) {
    if (p.genericInterface instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) p.genericInterface;
        final Type[] as = pt.getActualTypeArguments();
        final Class[] cas = new Class[as.length];
        for (int i = 0; i < as.length; i++) {
            final Type a = as[i];
            if (a instanceof Class) {
                cas[i] = (Class) a;
            } else if (a instanceof ParameterizedType) {
                pt = (ParameterizedType) a;
                cas[i] = (Class) pt.getRawType();
            } else if (a instanceof TypeVariable) {
                final TypeVariable tv = (TypeVariable) a;
                final ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, tv);
                cas[i] = (ctp != null) ? ctp.rawClass() : (Class<?>) (tv.getBounds()[0]);
            } else if (a instanceof GenericArrayType) {
                final GenericArrayType gat = (GenericArrayType) a;
                final Type t = gat.getGenericComponentType();
                if (t instanceof Class) {
                    cas[i] = getArrayForComponentType((Class<?>) t);
                }
            }
        }
        return cas;
    } else {
        return null;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) GenericType(javax.ws.rs.core.GenericType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 74 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project jetty.project by eclipse.

the class TypeTree method dumpTree.

public static void dumpTree(String indent, Type type) {
    if ((type == null) || (type == Object.class)) {
        return;
    }
    if (type instanceof Class<?>) {
        Class<?> ctype = (Class<?>) type;
        System.out.printf("%s (Class) = %s%n", indent, ctype.getName());
        String name = ctype.getName();
        if (name.startsWith("java.lang.") || name.startsWith("java.io.")) {
            // filter away standard classes from tree (otherwise it will go on infinitely)
            return;
        }
        Type superType = ctype.getGenericSuperclass();
        dumpTree(indent + ".genericSuperClass()", superType);
        Type[] ifaces = ctype.getGenericInterfaces();
        if ((ifaces != null) && (ifaces.length > 0)) {
            // System.out.printf("%s.genericInterfaces[].length = %d%n",indent,ifaces.length);
            for (int i = 0; i < ifaces.length; i++) {
                Type iface = ifaces[i];
                dumpTree(indent + ".genericInterfaces[" + i + "]", iface);
            }
        }
        TypeVariable<?>[] typeParams = ctype.getTypeParameters();
        if ((typeParams != null) && (typeParams.length > 0)) {
            // System.out.printf("%s.typeParameters[].length = %d%n",indent,typeParams.length);
            for (int i = 0; i < typeParams.length; i++) {
                TypeVariable<?> typeParam = typeParams[i];
                dumpTree(indent + ".typeParameters[" + i + "]", typeParam);
            }
        }
        return;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType ptype = (ParameterizedType) type;
        System.out.printf("%s (ParameterizedType) = %s%n", indent, ReflectUtils.toShortName(ptype));
        // dumpTree(indent + ".ownerType()",ptype.getOwnerType());
        dumpTree(indent + ".rawType(" + ReflectUtils.toShortName(ptype.getRawType()) + ")", ptype.getRawType());
        Type[] args = ptype.getActualTypeArguments();
        if (args != null) {
            System.out.printf("%s.actualTypeArguments[].length = %d%n", indent, args.length);
            for (int i = 0; i < args.length; i++) {
                Type arg = args[i];
                dumpTree(indent + ".actualTypeArguments[" + i + "]", arg);
            }
        }
        return;
    }
    if (type instanceof GenericArrayType) {
        GenericArrayType gtype = (GenericArrayType) type;
        System.out.printf("%s (GenericArrayType) = %s%n", indent, gtype);
        return;
    }
    if (type instanceof TypeVariable<?>) {
        TypeVariable<?> tvar = (TypeVariable<?>) type;
        System.out.printf("%s (TypeVariable) = %s%n", indent, tvar);
        System.out.printf("%s.getName() = %s%n", indent, tvar.getName());
        System.out.printf("%s.getGenericDeclaration() = %s%n", indent, tvar.getGenericDeclaration());
        return;
    }
    if (type instanceof WildcardType) {
        System.out.printf("%s (WildcardType) = %s%n", indent, type);
        return;
    }
    System.out.printf("%s (?) = %s%n", indent, type);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 75 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project elasticsearch by elastic.

the class MoreTypes 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;
        }
        // TODO: save a .clone() call
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        return Objects.equals(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. Could be a generic array type, wildcard type, etc.
        return false;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

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