Validation

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1535763 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2013-10-25 15:25:22 +00:00
parent b39f106258
commit 38f8b88528
1 changed files with 28 additions and 13 deletions

View File

@ -946,15 +946,18 @@ public class TypeUtils {
* {@code TreeSet} is {@code ? extends Map<Integer, ? extends * {@code TreeSet} is {@code ? extends Map<Integer, ? extends
* Collection<?>>}.</p> * Collection<?>>}.</p>
* *
* @param cls the class whose type parameters are to be determined * @param cls the class whose type parameters are to be determined, not {@code null}
* @param superType the super type from which {@code cls}'s type * @param superType the super type from which {@code cls}'s type
* arguments are to be determined * arguments are to be determined, not {@code null}
* @return a {@code Map} of the type assignments that could be determined * @return a {@code Map} of the type assignments that could be determined
* for the type variables in each type in the inheritance hierarchy from * for the type variables in each type in the inheritance hierarchy from
* {@code type} to {@code toClass} inclusive. * {@code type} to {@code toClass} inclusive.
*/ */
public static Map<TypeVariable<?>, Type> determineTypeArguments(final Class<?> cls, public static Map<TypeVariable<?>, Type> determineTypeArguments(final Class<?> cls,
final ParameterizedType superType) { final ParameterizedType superType) {
Validate.notNull(cls, "cls is null");
Validate.notNull(superType, "superType is null");
final Class<?> superClass = getRawType(superType); final Class<?> superClass = getRawType(superType);
// compatibility check // compatibility check
@ -1114,11 +1117,12 @@ public class TypeUtils {
* </p> * </p>
* *
* @param bounds an array of types representing the upper bounds of either * @param bounds an array of types representing the upper bounds of either
* {@link WildcardType} or {@link TypeVariable}. * {@link WildcardType} or {@link TypeVariable}, not {@code null}.
* @return an array containing the values from {@code bounds} minus the * @return an array containing the values from {@code bounds} minus the
* redundant types. * redundant types.
*/ */
public static Type[] normalizeUpperBounds(final Type[] bounds) { public static Type[] normalizeUpperBounds(final Type[] bounds) {
Validate.notNull(bounds, "null value specified for bounds array");
// don't bother if there's only one (or none) type // don't bother if there's only one (or none) type
if (bounds.length < 2) { if (bounds.length < 2) {
return bounds; return bounds;
@ -1150,10 +1154,11 @@ public class TypeUtils {
* returns the result of {@link TypeVariable#getBounds()} passed into * returns the result of {@link TypeVariable#getBounds()} passed into
* {@link #normalizeUpperBounds}.</p> * {@link #normalizeUpperBounds}.</p>
* *
* @param typeVariable the subject type variable * @param typeVariable the subject type variable, not {@code null}
* @return a non-empty array containing the bounds of the type variable. * @return a non-empty array containing the bounds of the type variable.
*/ */
public static Type[] getImplicitBounds(final TypeVariable<?> typeVariable) { public static Type[] getImplicitBounds(final TypeVariable<?> typeVariable) {
Validate.notNull(typeVariable, "typeVariable is null");
final Type[] bounds = typeVariable.getBounds(); final Type[] bounds = typeVariable.getBounds();
return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds); return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds);
@ -1165,11 +1170,12 @@ public class TypeUtils {
* it returns the result of {@link WildcardType#getUpperBounds()} * it returns the result of {@link WildcardType#getUpperBounds()}
* passed into {@link #normalizeUpperBounds}.</p> * passed into {@link #normalizeUpperBounds}.</p>
* *
* @param wildcardType the subject wildcard type * @param wildcardType the subject wildcard type, not {@code null}
* @return a non-empty array containing the upper bounds of the wildcard * @return a non-empty array containing the upper bounds of the wildcard
* type. * type.
*/ */
public static Type[] getImplicitUpperBounds(final WildcardType wildcardType) { public static Type[] getImplicitUpperBounds(final WildcardType wildcardType) {
Validate.notNull(wildcardType, "wildcardType is null");
final Type[] bounds = wildcardType.getUpperBounds(); final Type[] bounds = wildcardType.getUpperBounds();
return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds); return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds);
@ -1180,11 +1186,12 @@ public class TypeUtils {
* {@link WildcardType#getLowerBounds()} returns an empty array. Otherwise, * {@link WildcardType#getLowerBounds()} returns an empty array. Otherwise,
* it returns the result of {@link WildcardType#getLowerBounds()}.</p> * it returns the result of {@link WildcardType#getLowerBounds()}.</p>
* *
* @param wildcardType the subject wildcard type * @param wildcardType the subject wildcard type, not {@code null}
* @return a non-empty array containing the lower bounds of the wildcard * @return a non-empty array containing the lower bounds of the wildcard
* type. * type.
*/ */
public static Type[] getImplicitLowerBounds(final WildcardType wildcardType) { public static Type[] getImplicitLowerBounds(final WildcardType wildcardType) {
Validate.notNull(wildcardType, "wildcardType is null");
final Type[] bounds = wildcardType.getLowerBounds(); final Type[] bounds = wildcardType.getLowerBounds();
return bounds.length == 0 ? new Type[] { null } : bounds; return bounds.length == 0 ? new Type[] { null } : bounds;
@ -1199,11 +1206,12 @@ public class TypeUtils {
* in {@code typeVarAssigns}.</p> * in {@code typeVarAssigns}.</p>
* *
* @param typeVarAssigns specifies the potential types to be assigned to the * @param typeVarAssigns specifies the potential types to be assigned to the
* type variables. * type variables, not {@code null}.
* @return whether or not the types can be assigned to their respective type * @return whether or not the types can be assigned to their respective type
* variables. * variables.
*/ */
public static boolean typesSatisfyVariables(final Map<TypeVariable<?>, Type> typeVarAssigns) { public static boolean typesSatisfyVariables(final Map<TypeVariable<?>, Type> typeVarAssigns) {
Validate.notNull(typeVarAssigns, "typeVarAssigns is null");
// all types must be assignable to all the bounds of the their mapped // all types must be assignable to all the bounds of the their mapped
// type variable. // type variable.
for (final Map.Entry<TypeVariable<?>, Type> entry : typeVarAssigns.entrySet()) { for (final Map.Entry<TypeVariable<?>, Type> entry : typeVarAssigns.entrySet()) {
@ -1460,6 +1468,8 @@ public class TypeUtils {
*/ */
public static final ParameterizedType parameterize(final Class<?> raw, public static final ParameterizedType parameterize(final Class<?> raw,
final Map<TypeVariable<?>, Type> typeArgMappings) { final Map<TypeVariable<?>, Type> typeArgMappings) {
Validate.notNull(raw, "raw class is null");
Validate.notNull(typeArgMappings, "typeArgMappings is null");
return parameterizeWithOwner(null, raw, extractTypeArgumentsFrom(typeArgMappings, raw.getTypeParameters())); return parameterizeWithOwner(null, raw, extractTypeArgumentsFrom(typeArgMappings, raw.getTypeParameters()));
} }
@ -1475,7 +1485,7 @@ public class TypeUtils {
*/ */
public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> raw, public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> raw,
final Type... typeArguments) { final Type... typeArguments) {
Validate.notNull(raw, "raw class"); Validate.notNull(raw, "raw class is null");
final Type useOwner; final Type useOwner;
if (raw.getEnclosingClass() == null) { if (raw.getEnclosingClass() == null) {
Validate.isTrue(owner == null, "no owner allowed for top-level %s", raw); Validate.isTrue(owner == null, "no owner allowed for top-level %s", raw);
@ -1487,8 +1497,10 @@ public class TypeUtils {
"%s is invalid owner type for parameterized %s", owner, raw); "%s is invalid owner type for parameterized %s", owner, raw);
useOwner = owner; useOwner = owner;
} }
Validate.isTrue(raw.getTypeParameters().length == Validate.noNullElements(typeArguments, Validate.noNullElements(typeArguments, "null type argument at index %s");
"null type argument at index %s").length); Validate.isTrue(raw.getTypeParameters().length == typeArguments.length,
"invalid number of type parameters specified: expected %s, got %s", raw.getTypeParameters().length,
typeArguments.length);
return new ParameterizedTypeImpl(raw, useOwner, typeArguments); return new ParameterizedTypeImpl(raw, useOwner, typeArguments);
} }
@ -1504,6 +1516,8 @@ public class TypeUtils {
*/ */
public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> raw, public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> raw,
final Map<TypeVariable<?>, Type> typeArgMappings) { final Map<TypeVariable<?>, Type> typeArgMappings) {
Validate.notNull(raw, "raw class is null");
Validate.notNull(typeArgMappings, "typeArgMappings is null");
return parameterizeWithOwner(owner, raw, extractTypeArgumentsFrom(typeArgMappings, raw.getTypeParameters())); return parameterizeWithOwner(owner, raw, extractTypeArgumentsFrom(typeArgMappings, raw.getTypeParameters()));
} }
@ -1541,7 +1555,7 @@ public class TypeUtils {
* @since 3.2 * @since 3.2
*/ */
public static GenericArrayType genericArrayType(final Type componentType) { public static GenericArrayType genericArrayType(final Type componentType) {
return new GenericArrayTypeImpl(componentType); return new GenericArrayTypeImpl(Validate.notNull(componentType, "componentType is null"));
} }
/** /**
@ -1635,7 +1649,7 @@ public class TypeUtils {
/** /**
* Present a given type as a Java-esque String. * Present a given type as a Java-esque String.
* *
* @param type the type to create a String representation for * @param type the type to create a String representation for, not {@code null}
* @return String * @return String
* @since 3.2 * @since 3.2
*/ */
@ -1662,11 +1676,12 @@ public class TypeUtils {
/** /**
* Format a {@link TypeVariable} including its {@link GenericDeclaration}. * Format a {@link TypeVariable} including its {@link GenericDeclaration}.
* *
* @param var the type variable to create a String representation for * @param var the type variable to create a String representation for, not {@code null}
* @return String * @return String
* @since 3.2 * @since 3.2
*/ */
public static String toLongString(TypeVariable<?> var) { public static String toLongString(TypeVariable<?> var) {
Validate.notNull(var, "var is null");
final StringBuilder buf = new StringBuilder(); final StringBuilder buf = new StringBuilder();
final GenericDeclaration d = ((TypeVariable<?>) var).getGenericDeclaration(); final GenericDeclaration d = ((TypeVariable<?>) var).getGenericDeclaration();
if (d instanceof Class<?>) { if (d instanceof Class<?>) {