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);
}
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());
}
}
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();
}
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;
}
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();
}
Aggregations