public GenericArrayTypeImpl(Type componentType) { this.componentType = canonicalize(componentType); }
ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments) { // require an owner type if the raw type needs it if (rawType instanceof Class<?>) { Class<?> rawTypeAsClass = (Class<?>) rawType; boolean isStaticOrTopLevelClass = Modifier.isStatic(rawTypeAsClass.getModifiers()) || rawTypeAsClass.getEnclosingClass() == null; if (ownerType == null && !isStaticOrTopLevelClass) throw new IllegalArgumentException(); } this.ownerType = ownerType == null ? null : canonicalize(ownerType); this.rawType = canonicalize(rawType); this.typeArguments = typeArguments.clone(); for (int t = 0; t < this.typeArguments.length; t++) { if (this.typeArguments[t] == null) throw new NullPointerException(); checkNotPrimitive(this.typeArguments[t]); this.typeArguments[t] = canonicalize(this.typeArguments[t]); } }
public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { if (lowerBounds.length > 1) throw new IllegalArgumentException(); if (upperBounds.length != 1) throw new IllegalArgumentException(); if (lowerBounds.length == 1) { if (lowerBounds[0] == null) throw new NullPointerException(); checkNotPrimitive(lowerBounds[0]); if (upperBounds[0] != Object.class) throw new IllegalArgumentException(); this.lowerBound = canonicalize(lowerBounds[0]); this.upperBound = Object.class; } else { if (upperBounds[0] == null) throw new NullPointerException(); checkNotPrimitive(upperBounds[0]); this.lowerBound = null; this.upperBound = canonicalize(upperBounds[0]); } }
/** * Given a parameterized type {@code A<B, C>}, returns B. If the specified type is not a generic * type, returns null. */ public static Type getFirstTypeArgument(Type type) throws Exception { if (!(type instanceof ParameterizedType)) return null; ParameterizedType ptype = (ParameterizedType) type; Type[] actualTypeArguments = ptype.getActualTypeArguments(); if (actualTypeArguments.length == 0) return null; return Types.canonicalize(actualTypeArguments[0]); }
/** * 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! } }
type = Types.canonicalize(type);