use of java.lang.reflect.GenericArrayType in project moshi by square.
the class Types method equals.
/** Returns true if {@code a} and {@code b} are equal. */
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;
Type[] aTypeArguments = pa instanceof ParameterizedTypeImpl ? ((ParameterizedTypeImpl) pa).typeArguments : pa.getActualTypeArguments();
Type[] bTypeArguments = pb instanceof ParameterizedTypeImpl ? ((ParameterizedTypeImpl) pb).typeArguments : pb.getActualTypeArguments();
return equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(aTypeArguments, bTypeArguments);
} 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 supported type.
return false;
}
}
use of java.lang.reflect.GenericArrayType in project retrofit by square.
the class Utils method equals.
/** Returns true if {@code a} and {@code b} are equal. */
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;
}
}
use of java.lang.reflect.GenericArrayType in project retrofit by square.
the class Utils method getRawType.
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());
}
use of java.lang.reflect.GenericArrayType in project spring-framework by spring-projects.
the class TypeUtils method isAssignable.
/**
* Check if the right-hand side type may be assigned to the left-hand side
* type following the Java generics rules.
* @param lhsType the target type
* @param rhsType the value type that should be assigned to the target type
* @return true if rhs is assignable to lhs
*/
public static boolean isAssignable(Type lhsType, Type rhsType) {
Assert.notNull(lhsType, "Left-hand side type must not be null");
Assert.notNull(rhsType, "Right-hand side type must not be null");
// all types are assignable to themselves and to class Object
if (lhsType.equals(rhsType) || Object.class == lhsType) {
return true;
}
if (lhsType instanceof Class) {
Class<?> lhsClass = (Class<?>) lhsType;
// just comparing two classes
if (rhsType instanceof Class) {
return ClassUtils.isAssignable(lhsClass, (Class<?>) rhsType);
}
if (rhsType instanceof ParameterizedType) {
Type rhsRaw = ((ParameterizedType) rhsType).getRawType();
// a parameterized type is always assignable to its raw class type
if (rhsRaw instanceof Class) {
return ClassUtils.isAssignable(lhsClass, (Class<?>) rhsRaw);
}
} else if (lhsClass.isArray() && rhsType instanceof GenericArrayType) {
Type rhsComponent = ((GenericArrayType) rhsType).getGenericComponentType();
return isAssignable(lhsClass.getComponentType(), rhsComponent);
}
}
// parameterized types are only assignable to other parameterized types and class types
if (lhsType instanceof ParameterizedType) {
if (rhsType instanceof Class) {
Type lhsRaw = ((ParameterizedType) lhsType).getRawType();
if (lhsRaw instanceof Class) {
return ClassUtils.isAssignable((Class<?>) lhsRaw, (Class<?>) rhsType);
}
} else if (rhsType instanceof ParameterizedType) {
return isAssignable((ParameterizedType) lhsType, (ParameterizedType) rhsType);
}
}
if (lhsType instanceof GenericArrayType) {
Type lhsComponent = ((GenericArrayType) lhsType).getGenericComponentType();
if (rhsType instanceof Class) {
Class<?> rhsClass = (Class<?>) rhsType;
if (rhsClass.isArray()) {
return isAssignable(lhsComponent, rhsClass.getComponentType());
}
} else if (rhsType instanceof GenericArrayType) {
Type rhsComponent = ((GenericArrayType) rhsType).getGenericComponentType();
return isAssignable(lhsComponent, rhsComponent);
}
}
if (lhsType instanceof WildcardType) {
return isAssignable((WildcardType) lhsType, rhsType);
}
return false;
}
use of java.lang.reflect.GenericArrayType in project morphia by mongodb.
the class MappedField method discoverType.
@SuppressWarnings("unchecked")
protected void discoverType(final Mapper mapper) {
if (genericType instanceof TypeVariable) {
realType = extractTypeVariable((TypeVariable) genericType);
} else if (genericType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericType;
final Type[] types = pt.getActualTypeArguments();
realType = toClass(pt);
for (Type type : types) {
if (type instanceof ParameterizedType) {
typeParameters.add(new EphemeralMappedField((ParameterizedType) type, this, mapper));
} else {
if (type instanceof WildcardType) {
type = ((WildcardType) type).getUpperBounds()[0];
}
typeParameters.add(new EphemeralMappedField(type, this, mapper));
}
}
} else if (genericType instanceof WildcardType) {
final WildcardType wildcardType = (WildcardType) genericType;
final Type[] types = wildcardType.getUpperBounds();
realType = toClass(types[0]);
} else if (genericType instanceof Class) {
realType = (Class) genericType;
} else if (genericType instanceof GenericArrayType) {
final Type genericComponentType = ((GenericArrayType) genericType).getGenericComponentType();
if (genericComponentType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericComponentType;
realType = toClass(genericType);
final Type[] types = pt.getActualTypeArguments();
for (Type type : types) {
if (type instanceof ParameterizedType) {
typeParameters.add(new EphemeralMappedField((ParameterizedType) type, this, mapper));
} else {
if (type instanceof WildcardType) {
type = ((WildcardType) type).getUpperBounds()[0];
}
typeParameters.add(new EphemeralMappedField(type, this, mapper));
}
}
} else {
if (genericComponentType instanceof TypeVariable) {
realType = toClass(genericType);
} else {
realType = (Class) genericComponentType;
}
}
}
if (Object.class.equals(realType) || Object[].class.equals(realType)) {
if (LOG.isWarningEnabled()) {
LOG.warning(format("Parameterized types are treated as untyped Objects. See field '%s' on %s", field.getName(), field.getDeclaringClass()));
}
}
if (realType == null) {
throw new MappingException(format("A type could not be found for the field %s.%s", getType(), getField()));
}
}
Aggregations