Search in sources :

Example 41 with GenericArrayType

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

the class JavaObjectDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        if (componentType instanceof TypeVariable) {
            TypeVariable<?> componentVar = (TypeVariable<?>) componentType;
            componentType = componentVar.getBounds()[0];
        }
        List<Object> list = new ArrayList<Object>();
        parser.parseArray(componentType, list);
        Class<?> componentClass;
        if (componentType instanceof Class) {
            componentClass = (Class<?>) componentType;
            Object[] array = (Object[]) Array.newInstance(componentClass, list.size());
            list.toArray(array);
            return (T) array;
        } else {
            return (T) list.toArray();
        }
    }
    if (type instanceof Class && type != Object.class && type != Serializable.class) {
        return (T) parser.parseObject(type);
    }
    return (T) parser.parse(fieldName);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) Serializable(java.io.Serializable) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 42 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project android_frameworks_base by AOSPA.

the class TypeReference method toString.

private static void toString(Type type, StringBuilder out) {
    if (type == null) {
        return;
    } else if (type instanceof TypeVariable<?>) {
        // T
        out.append(((TypeVariable<?>) type).getName());
    } else if (type instanceof Class<?>) {
        Class<?> klass = (Class<?>) type;
        out.append(klass.getName());
        toString(klass.getTypeParameters(), out);
    } else if (type instanceof ParameterizedType) {
        // "Foo<T1, T2, T3, ... Tn>"
        ParameterizedType p = (ParameterizedType) type;
        out.append(((Class<?>) p.getRawType()).getName());
        toString(p.getActualTypeArguments(), out);
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        toString(gat.getGenericComponentType(), out);
        out.append("[]");
    } else {
        // WildcardType, BoundedType
        // TODO:
        out.append(type.toString());
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 43 with GenericArrayType

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

the class GenericType method parametersOf.

static GenericType[] parametersOf(Type type) {
    if (type instanceof Class) {
        Class clazz = (Class) type;
        if (clazz.isArray()) {
            GenericType t = new GenericType(clazz.getComponentType());
            if (t.size() > 0) {
                return new GenericType[] { t };
            } else {
                return EMPTY;
            }
        } else {
            return EMPTY;
        }
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type[] parameters = pt.getActualTypeArguments();
        GenericType[] gts = new GenericType[parameters.length];
        for (int i = 0; i < gts.length; i++) {
            gts[i] = new GenericType(parameters[i]);
        }
        return gts;
    }
    if (type instanceof GenericArrayType) {
        return new GenericType[] { new GenericType(((GenericArrayType) type).getGenericComponentType()) };
    }
    if (type instanceof WildcardType) {
        return EMPTY;
    }
    if (type instanceof TypeVariable) {
        return EMPTY;
    }
    throw new IllegalStateException();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) 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 44 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project logging-log4j2 by apache.

the class TypeUtil method isAssignable.

/**
     * Indicates if two {@link Type}s are assignment compatible.
     *
     * @param lhs the left hand side to check assignability to
     * @param rhs the right hand side to check assignability from
     * @return {@code true} if it is legal to assign a variable of type {@code rhs} to a variable of type {@code lhs}
     * @see Class#isAssignableFrom(Class)
     */
public static boolean isAssignable(final Type lhs, final Type rhs) {
    Objects.requireNonNull(lhs, "No left hand side type provided");
    Objects.requireNonNull(rhs, "No right hand side type provided");
    if (lhs.equals(rhs)) {
        return true;
    }
    if (Object.class.equals(lhs)) {
        // everything is assignable to Object
        return true;
    }
    // raw type on left
    if (lhs instanceof Class<?>) {
        final Class<?> lhsClass = (Class<?>) lhs;
        if (rhs instanceof Class<?>) {
            // no generics involved
            final Class<?> rhsClass = (Class<?>) rhs;
            return lhsClass.isAssignableFrom(rhsClass);
        }
        if (rhs instanceof ParameterizedType) {
            // check to see if the parameterized type has the same raw type as the lhs; this is legal
            final Type rhsRawType = ((ParameterizedType) rhs).getRawType();
            if (rhsRawType instanceof Class<?>) {
                return lhsClass.isAssignableFrom((Class<?>) rhsRawType);
            }
        }
        if (lhsClass.isArray() && rhs instanceof GenericArrayType) {
            // check for compatible array component types
            return isAssignable(lhsClass.getComponentType(), ((GenericArrayType) rhs).getGenericComponentType());
        }
    }
    // parameterized type on left
    if (lhs instanceof ParameterizedType) {
        final ParameterizedType lhsType = (ParameterizedType) lhs;
        if (rhs instanceof Class<?>) {
            final Type lhsRawType = lhsType.getRawType();
            if (lhsRawType instanceof Class<?>) {
                return ((Class<?>) lhsRawType).isAssignableFrom((Class<?>) rhs);
            }
        } else if (rhs instanceof ParameterizedType) {
            final ParameterizedType rhsType = (ParameterizedType) rhs;
            return isParameterizedAssignable(lhsType, rhsType);
        }
    }
    // generic array type on left
    if (lhs instanceof GenericArrayType) {
        final Type lhsComponentType = ((GenericArrayType) lhs).getGenericComponentType();
        if (rhs instanceof Class<?>) {
            // raw type on right
            final Class<?> rhsClass = (Class<?>) rhs;
            if (rhsClass.isArray()) {
                return isAssignable(lhsComponentType, rhsClass.getComponentType());
            }
        } else if (rhs instanceof GenericArrayType) {
            return isAssignable(lhsComponentType, ((GenericArrayType) rhs).getGenericComponentType());
        }
    }
    // wildcard type on left
    if (lhs instanceof WildcardType) {
        return isWildcardAssignable((WildcardType) lhs, rhs);
    }
    // strange...
    return false;
}
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) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 45 with GenericArrayType

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

the class GenericType method parametersOf.

static GenericType[] parametersOf(Type type) {
    if (type instanceof Class) {
        Class clazz = (Class) type;
        if (clazz.isArray()) {
            GenericType t = new GenericType(clazz.getComponentType());
            if (t.size() > 0) {
                return new GenericType[] { t };
            } else {
                return EMPTY;
            }
        } else {
            return EMPTY;
        }
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type[] parameters = pt.getActualTypeArguments();
        GenericType[] gts = new GenericType[parameters.length];
        for (int i = 0; i < gts.length; i++) {
            gts[i] = new GenericType(parameters[i]);
        }
        return gts;
    }
    if (type instanceof GenericArrayType) {
        return new GenericType[] { new GenericType(((GenericArrayType) type).getGenericComponentType()) };
    }
    throw new IllegalStateException();
}
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) 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