/** * Determine whether the underlying type represents a wildcard * without specific bounds (i.e., equal to {@code ? extends Object}). */ private boolean isWildcardWithoutBounds() { if (this.type instanceof WildcardType) { WildcardType wt = (WildcardType) this.type; if (wt.getLowerBounds().length == 0) { Type[] upperBounds = wt.getUpperBounds(); if (upperBounds.length == 0 || (upperBounds.length == 1 && Object.class == upperBounds[0])) { return true; } } } return false; }
@Override void visitWildcardType(WildcardType t) { result.set(subtypeOfComponentType(t.getUpperBounds())); }
/** * Creates an uninitialized CaptureTypeImpl. Before using this type, {@link #init(VarMap)} must be called. * @param wildcard The wildcard this is a capture of * @param variable The type variable where the wildcard is a parameter for. */ public CaptureTypeImpl(WildcardType wildcard, TypeVariable<?> variable) { this.wildcard = wildcard; this.variable = variable; this.lowerBounds = wildcard.getLowerBounds(); }
static Type getParameterUpperBound(int index, ParameterizedType type) { Type[] types = type.getActualTypeArguments(); if (index < 0 || index >= types.length) { throw new IllegalArgumentException( "Index " + index + " not in range [0," + types.length + ") for " + type); } Type paramType = types[index]; if (paramType instanceof WildcardType) { return ((WildcardType) paramType).getUpperBounds()[0]; } return paramType; }
/** * <p>Returns an array containing a single value of {@code null} if * {@link WildcardType#getLowerBounds()} returns an empty array. Otherwise, * it returns the result of {@link WildcardType#getLowerBounds()}.</p> * * @param wildcardType the subject wildcard type, not {@code null} * @return a non-empty array containing the lower bounds of the wildcard * type. */ public static Type[] getImplicitLowerBounds(final WildcardType wildcardType) { Validate.notNull(wildcardType, "wildcardType is null"); final Type[] bounds = wildcardType.getLowerBounds(); return bounds.length == 0 ? new Type[] { null } : bounds; }
/** * @return The first bound, either a type or a reference to a TypeVariable */ public Type firstBound() { Type[] lowerBounds = wildcard.getLowerBounds(); Type[] upperBounds = wildcard.getUpperBounds(); return lowerBounds.length != 0 ? lowerBounds[0] : upperBounds[0]; }
private static Type getWildcardTypeUpperBounds(Type type) { if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; Type[] upperBounds = wildcardType.getUpperBounds(); return upperBounds.length > 0 ? upperBounds[0] : Object.class; } return type; }
@Override public List<Error> get() { return Collections.singletonList(boundsError(wt1, wt1.getLowerBounds(), "lower", wt2, wt2.getLowerBounds())); } });
@Override void visitWildcardType(WildcardType fromWildcardType) { if (!(to instanceof WildcardType)) { return; // okay to say <?> is anything } WildcardType toWildcardType = (WildcardType) to; Type[] fromUpperBounds = fromWildcardType.getUpperBounds(); Type[] toUpperBounds = toWildcardType.getUpperBounds(); Type[] fromLowerBounds = fromWildcardType.getLowerBounds(); Type[] toLowerBounds = toWildcardType.getLowerBounds(); checkArgument( fromUpperBounds.length == toUpperBounds.length && fromLowerBounds.length == toLowerBounds.length, "Incompatible type: %s vs. %s", fromWildcardType, to); for (int i = 0; i < fromUpperBounds.length; i++) { populateTypeMappings(mappings, fromUpperBounds[i], toUpperBounds[i]); } for (int i = 0; i < fromLowerBounds.length; i++) { populateTypeMappings(mappings, fromLowerBounds[i], toLowerBounds[i]); } }
@Override void visitWildcardType(WildcardType t) { result.set(subtypeOfComponentType(t.getUpperBounds())); }
/** * {@inheritDoc} */ public TypeList.Generic getLowerBounds() { return new WildcardLowerBoundTypeList(wildcardType.getLowerBounds(), annotationReader); }
@Override public boolean equals(Object obj) { if (obj instanceof WildcardType) { WildcardType that = (WildcardType) obj; return lowerBounds.equals(Arrays.asList(that.getLowerBounds())) && upperBounds.equals(Arrays.asList(that.getUpperBounds())); } return false; }
@Override public List<Error> get() { return Collections.singletonList(boundsError(wt1, wt1.getUpperBounds(), "upper", wt2, wt2.getUpperBounds())); } });
public List<TypeInformation<?>> getLowerBounds() { return getBounds(type.getLowerBounds()); }
/** * Determine whether the underlying type represents a wildcard * without specific bounds (i.e., equal to {@code ? extends Object}). */ private boolean isWildcardWithoutBounds() { if (this.type instanceof WildcardType) { WildcardType wt = (WildcardType) this.type; if (wt.getLowerBounds().length == 0) { Type[] upperBounds = wt.getUpperBounds(); if (upperBounds.length == 0 || (upperBounds.length == 1 && Object.class == upperBounds[0])) { return true; } } } return false; }
@Override void visitWildcardType(WildcardType t) { result.set(subtypeOfComponentType(t.getUpperBounds())); }
/** * Returns subtype of {@code this} with {@code subclass} as the raw class. For example, if this is * {@code Iterable<String>} and {@code subclass} is {@code List}, {@code List<String>} is * returned. */ public final TypeToken<? extends T> getSubtype(Class<?> subclass) { checkArgument( !(runtimeType instanceof TypeVariable), "Cannot get subtype of type variable <%s>", this); if (runtimeType instanceof WildcardType) { return getSubtypeFromLowerBounds(subclass, ((WildcardType) runtimeType).getLowerBounds()); } // unwrap array type if necessary if (isArray()) { return getArraySubtype(subclass); } // At this point, it's either a raw class or parameterized type. checkArgument( getRawType().isAssignableFrom(subclass), "%s isn't a subclass of %s", subclass, this); Type resolvedTypeArgs = resolveTypeArgsForSubclass(subclass); @SuppressWarnings("unchecked") // guarded by the isAssignableFrom() statement above TypeToken<? extends T> subtype = (TypeToken<? extends T>) of(resolvedTypeArgs); checkArgument( subtype.isSubtypeOf(this), "%s does not appear to be a subtype of %s", subtype, this); return subtype; }
/** * Determine whether the underlying type represents a wildcard without specific bounds (i.e., equal to * {@code ? extends Object}). */ private boolean isWildcardWithoutBounds() { if (this.type instanceof WildcardType) { WildcardType wt = (WildcardType) this.type; if (wt.getLowerBounds().length == 0) { Type[] upperBounds = wt.getUpperBounds(); if (upperBounds.length == 0 || (upperBounds.length == 1 && Object.class == upperBounds[0])) { return true; } } } return false; }
@Override void visitWildcardType(WildcardType t) { visit(t.getUpperBounds()); }
/** * Returns subtype of {@code this} with {@code subclass} as the raw class. For example, if this is * {@code Iterable<String>} and {@code subclass} is {@code List}, {@code List<String>} is * returned. */ public final TypeToken<? extends T> getSubtype(Class<?> subclass) { checkArgument( !(runtimeType instanceof TypeVariable), "Cannot get subtype of type variable <%s>", this); if (runtimeType instanceof WildcardType) { return getSubtypeFromLowerBounds(subclass, ((WildcardType) runtimeType).getLowerBounds()); } // unwrap array type if necessary if (isArray()) { return getArraySubtype(subclass); } // At this point, it's either a raw class or parameterized type. checkArgument( getRawType().isAssignableFrom(subclass), "%s isn't a subclass of %s", subclass, this); Type resolvedTypeArgs = resolveTypeArgsForSubclass(subclass); @SuppressWarnings("unchecked") // guarded by the isAssignableFrom() statement above TypeToken<? extends T> subtype = (TypeToken<? extends T>) of(resolvedTypeArgs); checkArgument( subtype.isSubtypeOf(this), "%s does not appear to be a subtype of %s", subtype, this); return subtype; }