Search in sources :

Example 61 with GenericArrayType

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

the class JAXBContextInitializer method addType.

private void addType(Type cls, boolean allowArray) {
    if (cls instanceof Class) {
        if (globalAdapters.contains(cls)) {
            return;
        }
        if (((Class<?>) cls).isArray() && !allowArray) {
            addClass(((Class<?>) cls).getComponentType());
        } else {
            addClass((Class<?>) cls);
        }
    } else if (cls instanceof ParameterizedType) {
        final ParameterizedType parameterizedType = (ParameterizedType) cls;
        addType(parameterizedType.getRawType());
        if (!parameterizedType.getRawType().equals(Enum.class)) {
            for (Type t2 : parameterizedType.getActualTypeArguments()) {
                if (shouldTypeBeAdded(t2, parameterizedType)) {
                    addType(t2);
                }
            }
        }
    } else if (cls instanceof GenericArrayType) {
        Class<?> ct;
        GenericArrayType gt = (GenericArrayType) cls;
        Type componentType = gt.getGenericComponentType();
        if (componentType instanceof Class) {
            ct = (Class<?>) componentType;
        } else if (componentType instanceof ParameterizedType) {
            final ParameterizedType parameterizedType = (ParameterizedType) componentType;
            final Type rawType = parameterizedType.getRawType();
            if (rawType instanceof Class) {
                ct = (Class<?>) rawType;
            } else {
                throw new IllegalArgumentException("Unable to determine type for " + rawType);
            }
            if (!parameterizedType.getRawType().equals(Enum.class)) {
                for (Type t2 : parameterizedType.getActualTypeArguments()) {
                    if (shouldTypeBeAdded(t2, parameterizedType)) {
                        addType(t2);
                    }
                }
            }
        } else {
            TypeVariable<?> tv = (TypeVariable<?>) componentType;
            Type[] bounds = tv.getBounds();
            if (bounds != null && bounds.length == 1) {
                if (bounds[0] instanceof Class) {
                    ct = (Class<?>) bounds[0];
                } else {
                    throw new IllegalArgumentException("Unable to determine type for: " + tv);
                }
            } else {
                throw new IllegalArgumentException("Unable to determine type for: " + tv);
            }
        }
        ct = Array.newInstance(ct, 0).getClass();
        addClass(ct);
    } else if (cls instanceof WildcardType) {
        for (Type t : ((WildcardType) cls).getUpperBounds()) {
            addType(t);
        }
        for (Type t : ((WildcardType) cls).getLowerBounds()) {
            addType(t);
        }
    } else if (cls instanceof TypeVariable) {
        for (Type t : ((TypeVariable<?>) cls).getBounds()) {
            addType(t);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) XmlType(javax.xml.bind.annotation.XmlType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) 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 62 with GenericArrayType

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

the class JAXBSchemaInitializer method getBeanInfo.

public JAXBBeanInfo getBeanInfo(Type cls) {
    if (cls instanceof Class) {
        if (((Class<?>) cls).isArray()) {
            return getBeanInfo(((Class<?>) cls).getComponentType());
        }
        return getBeanInfo((Class<?>) cls);
    } else if (cls instanceof ParameterizedType) {
        for (Type t2 : ((ParameterizedType) cls).getActualTypeArguments()) {
            return getBeanInfo(t2);
        }
    } else if (cls instanceof GenericArrayType) {
        GenericArrayType gt = (GenericArrayType) cls;
        Class<?> ct = (Class<?>) gt.getGenericComponentType();
        ct = Array.newInstance(ct, 0).getClass();
        return getBeanInfo(ct);
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 63 with GenericArrayType

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

the class JAXBSchemaInitializer method getArrayComponentType.

static Class<?> getArrayComponentType(Type cls) {
    if (cls instanceof Class) {
        if (((Class<?>) cls).isArray()) {
            return ((Class<?>) cls).getComponentType();
        }
        return (Class<?>) cls;
    } else if (cls instanceof ParameterizedType) {
        for (Type t2 : ((ParameterizedType) cls).getActualTypeArguments()) {
            return getArrayComponentType(t2);
        }
    } else if (cls instanceof GenericArrayType) {
        GenericArrayType gt = (GenericArrayType) cls;
        Class<?> ct = (Class<?>) gt.getGenericComponentType();
        return Array.newInstance(ct, 0).getClass();
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 64 with GenericArrayType

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

the class TypeUtil method getTypeRelatedClass.

/**
 * If a Type is a class, return it as a class.
 * If it is a ParameterizedType, return the raw type as a class.
 * Otherwise return null.
 * @param type
 * @return
 */
public static Class<?> getTypeRelatedClass(Type type) {
    Class<?> directClass = getTypeClass(type, false);
    if (directClass != null) {
        return directClass;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        return getTypeRelatedClass(pType.getRawType());
    }
    if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        Type compType = gat.getGenericComponentType();
        Class<?> arrayBaseType = getTypeRelatedClass(compType);
        // believe it or not, this seems to be the only way to get the
        // Class object for an array of primitive type.
        Object instance = Array.newInstance(arrayBaseType, 0);
        return instance.getClass();
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 65 with GenericArrayType

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

the class InjectionUtils method getActualType.

public static Class<?> getActualType(Type genericType, int pos) {
    if (genericType == null) {
        return null;
    }
    if (genericType == Object.class) {
        return (Class<?>) genericType;
    }
    if (!ParameterizedType.class.isAssignableFrom(genericType.getClass())) {
        if (genericType instanceof TypeVariable) {
            genericType = getType(((TypeVariable<?>) genericType).getBounds(), pos);
        } else if (genericType instanceof WildcardType) {
            WildcardType wildcardType = (WildcardType) genericType;
            Type[] bounds = wildcardType.getLowerBounds();
            if (bounds.length == 0) {
                bounds = wildcardType.getUpperBounds();
            }
            genericType = getType(bounds, pos);
        } else if (genericType instanceof GenericArrayType) {
            genericType = ((GenericArrayType) genericType).getGenericComponentType();
        }
        Class<?> cls = null;
        if (!(genericType instanceof ParameterizedType)) {
            cls = (Class<?>) genericType;
        } else {
            cls = (Class<?>) ((ParameterizedType) genericType).getRawType();
        }
        return cls.isArray() ? cls.getComponentType() : cls;
    }
    ParameterizedType paramType = (ParameterizedType) genericType;
    Type t = getType(paramType.getActualTypeArguments(), pos);
    return t instanceof Class ? (Class<?>) t : getActualType(t, 0);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) 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