/** * Returns a type that is functionally equal but not necessarily equal according to {@link * Object#equals(Object) Object.equals()}. */ static Type canonicalize(Type type) { if (type instanceof Class) { Class<?> c = (Class<?>) type; return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c; } else if (type instanceof ParameterizedType) { if (type instanceof ParameterizedTypeImpl) return type; ParameterizedType p = (ParameterizedType) type; return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p.getActualTypeArguments()); } else if (type instanceof GenericArrayType) { if (type instanceof GenericArrayTypeImpl) return type; GenericArrayType g = (GenericArrayType) type; return new GenericArrayTypeImpl(g.getGenericComponentType()); } else if (type instanceof WildcardType) { if (type instanceof WildcardTypeImpl) return type; WildcardType w = (WildcardType) type; return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); } else { return type; // This type is unsupported! } }
public static Class getGenericClass(ParameterizedType parameterizedType, int i) { Object genericClass = parameterizedType.getActualTypeArguments()[i]; if (genericClass instanceof ParameterizedType) { // 处理多级泛型 System.out.println("111111"); return (Class) ((ParameterizedType) genericClass).getRawType(); } else if (genericClass instanceof GenericArrayType) { // 处理数组泛型 return (Class) ((GenericArrayType) genericClass).getGenericComponentType(); } else if (genericClass instanceof TypeVariable) { // 处理泛型擦拭对象 System.out.println("33333333"); return (Class) getClass(((TypeVariable) genericClass).getBounds()[0], 0); } else { System.out.println("444444"); return (Class) genericClass; } }
/** * Returns a type that is functionally equal but not necessarily equal * according to {@link Object#equals(Object) Object.equals()}. The returned * type is {@link java.io.Serializable}. */ public static Type canonicalize(Type type) { if (type instanceof Class) { Class<?> c = (Class<?>) type; return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c; } else if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType) type; return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p.getActualTypeArguments()); } else if (type instanceof GenericArrayType) { GenericArrayType g = (GenericArrayType) type; return new GenericArrayTypeImpl(g.getGenericComponentType()); } else if (type instanceof WildcardType) { WildcardType w = (WildcardType) type; return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); } else { // type is either serializable as-is or unsupported return type; } }
/** * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's * a bit awkward because we have to pull out the inner type in the type literal. */ private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding( Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException { Type typeLiteralType = key.getTypeLiteral().getType(); if (!(typeLiteralType instanceof ParameterizedType)) { throw errors.cannotInjectRawTypeLiteral().toException(); } ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType; Type innerType = parameterizedType.getActualTypeArguments()[0]; // this is unforunate. We don't support building TypeLiterals for type variable like 'T'. If // this proves problematic, we can probably fix TypeLiteral to support type variables if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) { throw errors.cannotInjectTypeLiteralOf(innerType).toException(); } @SuppressWarnings("unchecked") // by definition, innerType == T, so this is safe TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType); InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<TypeLiteral<T>>( Initializables.of(value)); return new InstanceBindingImpl<TypeLiteral<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), value); }
private Class<?> getHttpEntityType(MethodParameter methodParam) { Assert.isAssignable(HttpEntity.class, methodParam.getParameterType()); ParameterizedType type = (ParameterizedType) methodParam.getGenericParameterType(); if (type.getActualTypeArguments().length == 1) { Type typeArgument = type.getActualTypeArguments()[0]; if (typeArgument instanceof Class) { return (Class<?>) typeArgument; } else if (typeArgument instanceof GenericArrayType) { Type componentType = ((GenericArrayType) typeArgument).getGenericComponentType(); if (componentType instanceof Class) { // Surely, there should be a nicer way to do this Object array = Array.newInstance((Class<?>) componentType, 0); return array.getClass(); } } } throw new IllegalArgumentException( "HttpEntity parameter (" + methodParam.getParameterName() + ") is not parameterized"); }
static boolean hasUnresolvableType(Type type) { if (type instanceof Class<?>) { return false; } if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; for (Type typeArgument : parameterizedType.getActualTypeArguments()) { if (hasUnresolvableType(typeArgument)) { return true; } } return false; } if (type instanceof GenericArrayType) { return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType()); } if (type instanceof TypeVariable) { return true; } if (type instanceof WildcardType) { return true; } String className = type == null ? "null" : type.getClass().getName(); throw new IllegalArgumentException("Expected a Class, ParameterizedType, or " + "GenericArrayType, but <" + type + "> is of type " + className); }
/** * Resolves all type variables in {@code type} and all downstream types and returns a * corresponding type with type variables resolved. */ public Type resolveType(Type type) { checkNotNull(type); if (type instanceof TypeVariable) { return typeTable.resolve((TypeVariable<?>) type); } else if (type instanceof ParameterizedType) { return resolveParameterizedType((ParameterizedType) type); } else if (type instanceof GenericArrayType) { return resolveGenericArrayType((GenericArrayType) type); } else if (type instanceof WildcardType) { return resolveWildcardType((WildcardType) type); } else { // if Class<?>, no resolution needed, we are done. return type; } }
public static Class<?> getRawType(Type type) { if (type instanceof Class) { return (Class) type; } if (type instanceof ParameterizedType) { Type rawType = ((ParameterizedType) type).getRawType(); C$Gson$Preconditions.checkArgument(rawType instanceof Class); return (Class) rawType; } else if (type instanceof GenericArrayType) { return Array.newInstance(C$Gson$Types.getRawType(((GenericArrayType) type) .getGenericComponentType()), 0).getClass(); } else { if (type instanceof TypeVariable) { return Object.class; } if (type instanceof WildcardType) { return C$Gson$Types.getRawType(((WildcardType) type).getUpperBounds()[0]); } throw new IllegalArgumentException("Expected a Class, ParameterizedType, or " + "GenericArrayType, but <" + type + "> is of type " + (type == null ? "null" : type.getClass().getName())); } }
/** If the type of the field is parameterized, returns the Class object representing the parameter type at the specified index, * null otherwise. */ public Class getElementType (int index) { Type genericType = field.getGenericType(); if (genericType instanceof ParameterizedType) { Type[] actualTypes = ((ParameterizedType)genericType).getActualTypeArguments(); if (actualTypes.length - 1 >= index) { Type actualType = actualTypes[index]; if (actualType instanceof Class) return (Class)actualType; else if (actualType instanceof ParameterizedType) return (Class)((ParameterizedType)actualType).getRawType(); else if (actualType instanceof GenericArrayType) { Type componentType = ((GenericArrayType)actualType).getGenericComponentType(); if (componentType instanceof Class) return ArrayReflection.newInstance((Class)componentType, 0).getClass(); } } } return null; }
/** * Returns the hashCode of {@code type}. */ public static int hashCode(Type type) { if (type instanceof Class) { // Class specifies hashCode(). return type.hashCode(); } else if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType) type; return Arrays.hashCode(p.getActualTypeArguments()) ^ p.getRawType().hashCode() ^ hashCodeOrZero(p.getOwnerType()); } else if (type instanceof GenericArrayType) { return hashCode(((GenericArrayType) type).getGenericComponentType()); } else if (type instanceof WildcardType) { WildcardType w = (WildcardType) type; return Arrays.hashCode(w.getLowerBounds()) ^ Arrays.hashCode(w.getUpperBounds()); } else { // This isn't a type we support. Probably a type variable return hashCodeOrZero(type); } }
public static Class<?> getGenericClass(Class<?> cls, int i) { try { ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]); Object genericClass = parameterizedType.getActualTypeArguments()[i]; if (genericClass instanceof ParameterizedType) { // 处理多级泛型 return (Class<?>) ((ParameterizedType) genericClass).getRawType(); } else if (genericClass instanceof GenericArrayType) { // 处理数组泛型 return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType(); } else if (genericClass != null) { return (Class<?>) genericClass; } } catch (Throwable e) { } if (cls.getSuperclass() != null) { return getGenericClass(cls.getSuperclass(), i); } else { throw new IllegalArgumentException(cls.getName() + " generic type undefined!"); } }
public static Class getGenericClass(ParameterizedType parameterizedType, int i) { Object genericClass = parameterizedType.getActualTypeArguments()[i]; if (genericClass instanceof ParameterizedType) { // 处理多级泛型 return (Class) ((ParameterizedType) genericClass).getRawType(); } else if (genericClass instanceof GenericArrayType) { // 处理数组泛型 return (Class) ((GenericArrayType) genericClass).getGenericComponentType(); } else if (genericClass instanceof TypeVariable) { // 处理泛型擦拭对象 return (Class) getClass(((TypeVariable) genericClass).getBounds()[0], 0); } else { return (Class) genericClass; } }
public static Type a(Type type) { if (type instanceof Class) { c cVar; Class cls = (Class) type; if (cls.isArray()) { cVar = new c(a(cls.getComponentType())); } else { Object obj = cls; } return cVar; } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; return new d(parameterizedType.getOwnerType(), parameterizedType.getRawType(), parameterizedType.getActualTypeArguments()); } else if (type instanceof GenericArrayType) { return new c(((GenericArrayType) type).getGenericComponentType()); } else { if (!(type instanceof WildcardType)) { return type; } WildcardType wildcardType = (WildcardType) type; return new e(wildcardType.getUpperBounds(), wildcardType.getLowerBounds()); } }
/** * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's * a bit awkward because we have to pull out the inner type in the type literal. */ private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding( Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException { Type typeLiteralType = key.getTypeLiteral().getType(); if (!(typeLiteralType instanceof ParameterizedType)) { throw errors.cannotInjectRawTypeLiteral().toException(); } ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType; Type innerType = parameterizedType.getActualTypeArguments()[0]; // this is unforunate. We don't support building TypeLiterals for type variable like 'T'. If // this proves problematic, we can probably fix TypeLiteral to support type variables if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) { throw errors.cannotInjectTypeLiteralOf(innerType).toException(); } @SuppressWarnings("unchecked") // by definition, innerType == T, so this is safe TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType); InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<>( Initializables.of(value)); return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), value); }
static public Class<?> getClass(Type type) throws ClassNotFoundException { if (type instanceof Class) { return (Class<?>) type; } else if (type instanceof ParameterizedType) { return getClass(((ParameterizedType) type).getRawType()); } else if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type).getGenericComponentType(); Class<?> componentClass = getClass(componentType); if (componentClass != null) { return Array.newInstance(componentClass, 0).getClass(); } } String className = getClassName(type); if (className == null || className.isEmpty()) { return null; } return Class.forName(className); }
/** * Returns a type that is functionally equal but not necessarily equal * according to {@link Object#equals(Object) Object.equals()}. The returned * type is {@link Serializable}. */ public static Type canonicalize(Type type) { if (type instanceof Class) { Class<?> c = (Class<?>) type; return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c; } else if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType) type; return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p.getActualTypeArguments()); } else if (type instanceof GenericArrayType) { GenericArrayType g = (GenericArrayType) type; return new GenericArrayTypeImpl(g.getGenericComponentType()); } else if (type instanceof WildcardType) { WildcardType w = (WildcardType) type; return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); } else { // type is either serializable as-is or unsupported return type; } }
@Override final Object fromNonNullOpenValue(Object openValue) throws InvalidObjectException { final Object[] openArray = (Object[]) openValue; final Type javaType = getJavaType(); final Object[] valueArray; final Type componentType; if (javaType instanceof GenericArrayType) { componentType = ((GenericArrayType) javaType).getGenericComponentType(); } else if (javaType instanceof Class<?> && ((Class<?>) javaType).isArray()) { componentType = ((Class<?>) javaType).getComponentType(); } else { throw new IllegalArgumentException("Not an array: " + javaType); } valueArray = (Object[]) Array.newInstance((Class<?>) componentType, openArray.length); for (int i = 0; i < openArray.length; i++) valueArray[i] = elementMapping.fromOpenValue(openArray[i]); return valueArray; }
/** * Check if this type is assignable from the given Type. * * @deprecated this implementation may be inconsistent with javac for types * with wildcards. */ @Deprecated public boolean isAssignableFrom(Type from) { if (from == null) { return false; } if (type.equals(from)) { return true; } if (type instanceof Class<?>) { return rawType.isAssignableFrom($Gson$Types.getRawType(from)); } else if (type instanceof ParameterizedType) { return isAssignableFrom(from, (ParameterizedType) type, new HashMap<String, Type>()); } else if (type instanceof GenericArrayType) { return rawType.isAssignableFrom($Gson$Types.getRawType(from)) && isAssignableFrom(from, (GenericArrayType) type); } else { throw buildUnexpectedTypeError( type, Class.class, ParameterizedType.class, GenericArrayType.class); } }
/** * Get the underlying class for a type. * <p> * @param type * the type to get class from * @return * the underlying class, or <tt>null</tt> if the type is a variable type */ private static Class<?> getClass(Type type) { if (type instanceof Class) { return (Class<?>) type; } else if (type instanceof ParameterizedType) { return getClass(((ParameterizedType) type).getRawType()); } else if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type) .getGenericComponentType(); Class<?> componentClass = getClass(componentType); if (componentClass != null) { return Array.newInstance(componentClass, 0).getClass(); } else { return null; } } else { return null; } }
/** * Returns true if this type is a subtype of the given {@code type}. "Subtype" is defined * according to * <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.5.1">the rules for * type arguments</a> introduced with Java generics. * * @since 19.0 */ public final boolean isSubtypeOf(Type supertype) { checkNotNull(supertype); if (supertype instanceof WildcardType) { // if 'supertype' is <? super Foo>, 'this' can be: // Foo, SubFoo, <? extends Foo>. // if 'supertype' is <? extends Foo>, nothing is a subtype. return any(((WildcardType) supertype).getLowerBounds()).isSupertypeOf(runtimeType); } // if 'this' is wildcard, it's a suptype of to 'supertype' if any of its "extends" // bounds is a subtype of 'supertype'. if (runtimeType instanceof WildcardType) { // <? super Base> is of no use in checking 'from' being a subtype of 'to'. return any(((WildcardType) runtimeType).getUpperBounds()).isSubtypeOf(supertype); } // if 'this' is type variable, it's a subtype if any of its "extends" // bounds is a subtype of 'supertype'. if (runtimeType instanceof TypeVariable) { return runtimeType.equals(supertype) || any(((TypeVariable<?>) runtimeType).getBounds()).isSubtypeOf(supertype); } if (runtimeType instanceof GenericArrayType) { return of(supertype).isSupertypeOfArray((GenericArrayType) runtimeType); } // Proceed to regular Type subtype check if (supertype instanceof Class) { return this.someRawTypeIsSubclassOf((Class<?>) supertype); } else if (supertype instanceof ParameterizedType) { return this.isSubtypeOfParameterizedType((ParameterizedType) supertype); } else if (supertype instanceof GenericArrayType) { return this.isSubtypeOfArrayType((GenericArrayType) supertype); } else { // to instanceof TypeVariable return false; } }
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) { // We could use the variable's bounds, but that won't work if there are multiple. Having a raw // 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()); }
private static Type getTrueType( Type type, TypeVariable<?>[] typeVariables, Type[] actualTypes) { if (type instanceof TypeVariable<?>) { TypeVariable<?> tv = (TypeVariable<?>) type; String name = tv.getName(); if (actualTypes != null) { for (int i = 0; i < typeVariables.length; i++) { if (name.equals(typeVariables[i].getName())) { return actualTypes[i]; } } } return tv; // }else if (type instanceof Class<?>) { // return type; } else if (type instanceof GenericArrayType) { Type ct = ((GenericArrayType) type).getGenericComponentType(); if (ct instanceof Class<?>) { return Array.newInstance((Class<?>) ct, 0).getClass(); } } return type; }
@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); }
public static Type unwrap(Type type) { if (!(type instanceof GenericArrayType)) { return type; } Type componentType = ((GenericArrayType) type).getGenericComponentType(); if (componentType == Byte.TYPE) { return byte[].class; } if (componentType == Character.TYPE) { return char[].class; } return type; }
public static Type checkPrimitiveArray(GenericArrayType genericArrayType) { Type clz = genericArrayType; Type genericComponentType = genericArrayType.getGenericComponentType(); String prefix = "["; while (genericComponentType instanceof GenericArrayType) { genericComponentType = ((GenericArrayType) genericComponentType) .getGenericComponentType(); prefix += prefix; } if (genericComponentType instanceof Class<?>) { Class<?> ck = (Class<?>) genericComponentType; if (ck.isPrimitive()) { try { if (ck == boolean.class) { clz = Class.forName(prefix + "Z"); } else if (ck == char.class) { clz = Class.forName(prefix + "C"); } else if (ck == byte.class) { clz = Class.forName(prefix + "B"); } else if (ck == short.class) { clz = Class.forName(prefix + "S"); } else if (ck == int.class) { clz = Class.forName(prefix + "I"); } else if (ck == long.class) { clz = Class.forName(prefix + "J"); } else if (ck == float.class) { clz = Class.forName(prefix + "F"); } else if (ck == double.class) { clz = Class.forName(prefix + "D"); } } catch (ClassNotFoundException e) { } } } return clz; }
static Class<?> getRawType(Type type) { checkNotNull(type, "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) { // We could use the variable's bounds, but that won't work if there are multiple. Having a raw // 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()); }
/** Returns true if {@code a} and {@code b} are equal. */ static boolean equals(Type a, Type b) { if (a == b) { return true; // Also handles (a == null && b == null). } else if (a instanceof Class) { return a.equals(b); // Class already specifies equals(). } 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 { return false; // This isn't a type we support! } }
private Class getRawType(Type type) { if (type instanceof Class) { return (Class)type; } else if (type instanceof java.lang.reflect.ParameterizedType) { return (Class)(((java.lang.reflect.ParameterizedType)type).getRawType()); } else if (type instanceof TypeVariable) { return Object.class; } else if (type instanceof WildcardType) { return getRawType(((WildcardType)type).getUpperBounds()[0]); } else if (type instanceof GenericArrayType) { return Array.newInstance(getRawType(((GenericArrayType)type).getGenericComponentType()), 0).getClass(); } else { throw new RuntimeException("Invalid type passed: " + type); } }
private static Type fixType(Type t) { if (!(t instanceof GenericArrayType)) return t; GenericArrayType gat = (GenericArrayType) t; Type ultimate = ultimateComponentType(gat); if (!(ultimate instanceof Class<?>)) return t; Class<?> component = (Class<?>) fixType(gat.getGenericComponentType()); return Array.newInstance(component, 0).getClass(); }
@Override public boolean equals(Object o) { if (o instanceof GenericArrayType) { GenericArrayType that = (GenericArrayType) o; return Objects.equals(genericComponentType, that.getGenericComponentType()); } else return false; }
private static void check2(Type t, String what) { if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; check(pt.getActualTypeArguments(), "type argument", what); } else if (t instanceof TypeVariable) { TypeVariable<?> tv = (TypeVariable<?>) t; check(tv.getBounds(), "bound", what); GenericDeclaration gd = tv.getGenericDeclaration(); if (gd instanceof Type) check((Type) gd, "declaration containing " + what); } else if (t instanceof WildcardType) { WildcardType wt = (WildcardType) t; check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what); check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what); } else if (t instanceof Class<?>) { Class<?> c = (Class<?>) t; check(c.getGenericInterfaces(), "superinterface", c.toString()); check(c.getGenericSuperclass(), "superclass of " + c); check(c.getTypeParameters(), "type parameter", c.toString()); } else if (t instanceof GenericArrayType) { GenericArrayType gat = (GenericArrayType) t; Type comp = gat.getGenericComponentType(); if (comp instanceof Class) { fail("Type " + t + " uses GenericArrayType when plain " + "array would do, in " + what); } else check(comp, "component type of " + what); } else { fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")"); } }
@Override public boolean equals(Object o) { if (o instanceof GenericArrayType) { GenericArrayType that = (GenericArrayType) o; Type thatComponentType = that.getGenericComponentType(); return genericComponentType == null ? thatComponentType == null : genericComponentType.equals(thatComponentType); } else return false; }
public Type getArrayType() { Class<?> clazz = Cast.as(type, Class.class); if (clazz != null) { return clazz.getComponentType(); } GenericArrayType gat = Cast.as(type, GenericArrayType.class); if (gat != null) { return gat.getGenericComponentType(); } return null; }
private boolean isSubtypeOfArrayType(GenericArrayType supertype) { if (runtimeType instanceof Class) { Class<?> fromClass = (Class<?>) runtimeType; if (!fromClass.isArray()) { return false; } return of(fromClass.getComponentType()).isSubtypeOf(supertype.getGenericComponentType()); } else if (runtimeType instanceof GenericArrayType) { GenericArrayType fromArrayType = (GenericArrayType) runtimeType; return of(fromArrayType.getGenericComponentType()) .isSubtypeOf(supertype.getGenericComponentType()); } else { return false; } }
public boolean isArrayButNotByteArray(Type t) { if (t instanceof Class) { Class c = (Class) t; return c.isArray() && c != byte[].class; } if (t instanceof GenericArrayType) { t = ((GenericArrayType) t).getGenericComponentType(); return t != Byte.TYPE; } return false; }
public boolean isArray(Type t) { if (t instanceof Class) { Class c = (Class) t; return c.isArray(); } if (t instanceof GenericArrayType) { return true; } return false; }