Search in sources :

Example 66 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project cxf by apache.

the class Wrapper method getTypeString.

protected String getTypeString(Type t) {
    String type = "Object";
    if (t instanceof Class) {
        Class<?> clz = (Class<?>) t;
        if (clz.isArray()) {
            if (isBuiltInTypes(clz.getComponentType())) {
                type = clz.getComponentType().getSimpleName() + "[]";
            } else {
                type = clz.getComponentType().getName() + "[]";
            }
        } else {
            type = clz.getName();
        }
    } else if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        Class<?> c = (Class<?>) pt.getRawType();
        if (Holder.class.isAssignableFrom(c) && pt.getActualTypeArguments().length == 1 && pt.getActualTypeArguments()[0] instanceof Class) {
            type = getTypeString(pt.getActualTypeArguments()[0]);
        } else {
            type = t.toString();
        }
    } else if (t instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) t;
        type = gat.toString();
    }
    type = type.replace('$', '.');
    return type;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Holder(javax.xml.ws.Holder) WrapperBeanClass(org.apache.cxf.tools.java2wsdl.generator.wsdl11.model.WrapperBeanClass) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 67 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project appsly-android-rest by 47deg.

the class Types 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 68 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project jdk8u_jdk by JetBrains.

the class TypeResolver method fixGenericArray.

/**
     * Replaces a {@link GenericArrayType GenericArrayType}
     * with plain array class where it is possible.
     * Bug <a href="http://bugs.sun.com/view_bug.do?bug_id=5041784">5041784</a>
     * is that arrays of non-generic type sometimes show up
     * as {@link GenericArrayType GenericArrayType} when using reflection.
     * For example, a {@code String[]} might show up
     * as a {@link GenericArrayType GenericArrayType}
     * where {@link GenericArrayType#getGenericComponentType getGenericComponentType}
     * is {@code String.class}.  This violates the specification,
     * which says that {@link GenericArrayType GenericArrayType}
     * is used when the component type is a type variable or parameterized type.
     * We fit the specification here.
     *
     * @param type  the type to fix
     * @return a corresponding type for the generic array type,
     *         or the same type as {@code type}
     */
private static Type fixGenericArray(Type type) {
    if (type instanceof GenericArrayType) {
        Type comp = ((GenericArrayType) type).getGenericComponentType();
        comp = fixGenericArray(comp);
        if (comp instanceof Class) {
            return Array.newInstance((Class<?>) comp, 0).getClass();
        }
    }
    return type;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 69 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project jdk8u_jdk by JetBrains.

the class TypeResolver method erase.

/**
     * Converts the given {@code type} to the corresponding class.
     * This method implements the concept of type erasure,
     * that is described in section 4.6 of
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * @param type  the array of types to convert
     * @return a corresponding class
     */
public static Class<?> erase(Type type) {
    if (type instanceof Class) {
        return (Class<?>) type;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        return (Class<?>) pt.getRawType();
    }
    if (type instanceof TypeVariable) {
        TypeVariable tv = (TypeVariable) type;
        Type[] bounds = tv.getBounds();
        return (0 < bounds.length) ? erase(bounds[0]) : Object.class;
    }
    if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;
        Type[] bounds = wt.getUpperBounds();
        return (0 < bounds.length) ? erase(bounds[0]) : Object.class;
    }
    if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        return Array.newInstance(erase(gat.getGenericComponentType()), 0).getClass();
    }
    throw new IllegalArgumentException("Unknown Type kind: " + type.getClass());
}
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 70 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project jdk8u_jdk by JetBrains.

the class TypeResolver method resolve.

/**
     * Replaces type variables of the given {@code formal} type
     * with the types they stand for in the given {@code actual} type.
     *
     * <p>A ParameterizedType is a class with type parameters, and the values
     * of those parameters.  For example, Map&lt;K,V> is a generic class, and
     * a corresponding ParameterizedType might look like
     * Map&lt;K=String,V=Integer>.  Given such a ParameterizedType, this method
     * will replace K with String, or List&lt;K> with List&ltString;, or
     * List&lt;? super K> with List&lt;? super String>.</p>
     *
     * <p>The {@code actual} argument to this method can also be a Class.
     * In this case, either it is equivalent to a ParameterizedType with
     * no parameters (for example, Integer.class), or it is equivalent to
     * a "raw" ParameterizedType (for example, Map.class).  In the latter
     * case, every type parameter declared or inherited by the class is replaced
     * by its "erasure".  For a type parameter declared as &lt;T>, the erasure
     * is Object.  For a type parameter declared as &lt;T extends Number>,
     * the erasure is Number.</p>
     *
     * <p>Although type parameters are not inherited by subclasses in the Java
     * language, they <em>are</em> effectively inherited when using reflection.
     * For example, if you declare an interface like this...</p>
     *
     * <pre>
     * public interface StringToIntMap extends Map&lt;String,Integer> {}
     * </pre>
     *
     * <p>...then StringToIntMap.class.getMethods() will show that it has methods
     * like put(K,V) even though StringToIntMap has no type parameters.  The K
     * and V variables are the ones declared by Map, so
     * {@link TypeVariable#getGenericDeclaration()} will return {@link Map Map.class}.</p>
     *
     * <p>For this reason, this method replaces inherited type parameters too.
     * Therefore if this method is called with {@code actual} being
     * StringToIntMap.class and {@code formal} being the K from Map,
     * it will return {@link String String.class}.</p>
     *
     * <p>In the case where {@code actual} is a "raw" ParameterizedType, the
     * inherited type parameters will also be replaced by their erasures.
     * The erasure of a Class is the Class itself, so a "raw" subinterface of
     * StringToIntMap will still show the K from Map as String.class.  But
     * in a case like this...
     *
     * <pre>
     * public interface StringToIntListMap extends Map&lt;String,List&lt;Integer>> {}
     * public interface RawStringToIntListMap extends StringToIntListMap {}
     * </pre>
     *
     * <p>...the V inherited from Map will show up as List&lt;Integer> in
     * StringToIntListMap, but as plain List in RawStringToIntListMap.</p>
     *
     * @param actual  the type that supplies bindings for type variables
     * @param formal  the type where occurrences of the variables
     *                in {@code actual} will be replaced by the corresponding bound values
     * @return a resolved type
     */
public static Type resolve(Type actual, Type formal) {
    if (formal instanceof Class) {
        return formal;
    }
    if (formal instanceof GenericArrayType) {
        Type comp = ((GenericArrayType) formal).getGenericComponentType();
        comp = resolve(actual, comp);
        return (comp instanceof Class) ? Array.newInstance((Class<?>) comp, 0).getClass() : GenericArrayTypeImpl.make(comp);
    }
    if (formal instanceof ParameterizedType) {
        ParameterizedType fpt = (ParameterizedType) formal;
        Type[] actuals = resolve(actual, fpt.getActualTypeArguments());
        return ParameterizedTypeImpl.make((Class<?>) fpt.getRawType(), actuals, fpt.getOwnerType());
    }
    if (formal instanceof WildcardType) {
        WildcardType fwt = (WildcardType) formal;
        Type[] upper = resolve(actual, fwt.getUpperBounds());
        Type[] lower = resolve(actual, fwt.getLowerBounds());
        return new WildcardTypeImpl(upper, lower);
    }
    if (formal instanceof TypeVariable) {
        Map<Type, Type> map;
        synchronized (CACHE) {
            map = CACHE.get(actual);
            if (map == null) {
                map = new HashMap<>();
                prepare(map, actual);
                CACHE.put(actual, map);
            }
        }
        Type result = map.get(formal);
        if (result == null || result.equals(formal)) {
            return formal;
        }
        result = fixGenericArray(result);
        // So if we have to resolve T, we need the tail recursion here.
        return resolve(actual, result);
    }
    throw new IllegalArgumentException("Bad Type kind: " + formal.getClass());
}
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)

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