This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit 5b157bbc94a74b71e681cbcb688b1e4462c6981e Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Sun Jun 15 13:32:05 2025 -0400 Reduce vertical whitespace --- .../apache/commons/lang3/reflect/TypeUtils.java | 107 --------------------- 1 file changed, 107 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java index 8da8e5c5d..ba8404f51 100644 --- a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java +++ b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java @@ -423,26 +423,20 @@ private static boolean containsVariableTypeSameParametrizedTypeBound(final TypeV public static Map<TypeVariable<?>, Type> determineTypeArguments(final Class<?> cls, final ParameterizedType superParameterizedType) { Objects.requireNonNull(cls, "cls"); Objects.requireNonNull(superParameterizedType, "superParameterizedType"); - final Class<?> superClass = getRawType(superParameterizedType); - // compatibility check if (!isAssignable(cls, superClass)) { return null; } - if (cls.equals(superClass)) { return getTypeArguments(superParameterizedType, superClass, null); } - // get the next class in the inheritance hierarchy final Type midType = getClosestParentType(cls, superClass); - // can only be a class or a parameterized type if (midType instanceof Class<?>) { return determineTypeArguments((Class<?>) midType, superParameterizedType); } - final ParameterizedType midParameterizedType = (ParameterizedType) midType; final Class<?> midClass = getRawType(midParameterizedType); // get the type variables of the mid class that map to the type @@ -450,7 +444,6 @@ public static Map<TypeVariable<?>, Type> determineTypeArguments(final Class<?> c final Map<TypeVariable<?>, Type> typeVarAssigns = determineTypeArguments(midClass, superParameterizedType); // map the arguments of the mid type to the class type variables mapTypeVariablesToArguments(cls, midParameterizedType, typeVarAssigns); - return typeVarAssigns; } @@ -622,11 +615,9 @@ private static Type getClosestParentType(final Class<?> cls, final Class<?> supe final Type[] interfaceTypes = cls.getGenericInterfaces(); // will hold the best generic interface match found Type genericInterface = null; - // find the interface closest to the super class for (final Type midType : interfaceTypes) { final Class<?> midClass; - if (midType instanceof ParameterizedType) { midClass = getRawType((ParameterizedType) midType); } else if (midType instanceof Class<?>) { @@ -634,20 +625,17 @@ private static Type getClosestParentType(final Class<?> cls, final Class<?> supe } else { throw new IllegalStateException("Unexpected generic" + " interface type found: " + midType); } - // check if this interface is further up the inheritance chain // than the previously found match if (isAssignable(midClass, superClass) && isAssignable(genericInterface, (Type) midClass)) { genericInterface = midType; } } - // found a match? if (genericInterface != null) { return genericInterface; } } - // none of the interfaces were descendants of the target class, so the // super class has to be one, instead return cls.getGenericSuperclass(); @@ -734,60 +722,47 @@ public static Class<?> getRawType(final Type type, final Type assigningType) { // it is raw, no problem return (Class<?>) type; } - if (type instanceof ParameterizedType) { // simple enough to get the raw type of a ParameterizedType return getRawType((ParameterizedType) type); } - if (type instanceof TypeVariable<?>) { if (assigningType == null) { return null; } - // get the entity declaring this type variable final Object genericDeclaration = ((TypeVariable<?>) type).getGenericDeclaration(); - // can't get the raw type of a method- or constructor-declared type // variable if (!(genericDeclaration instanceof Class<?>)) { return null; } - // get the type arguments for the declaring class/interface based // on the enclosing type final Map<TypeVariable<?>, Type> typeVarAssigns = getTypeArguments(assigningType, (Class<?>) genericDeclaration); - // enclosingType has to be a subclass (or subinterface) of the // declaring type if (typeVarAssigns == null) { return null; } - // get the argument assigned to this type variable final Type typeArgument = typeVarAssigns.get(type); - if (typeArgument == null) { return null; } - // get the argument for this type variable return getRawType(typeArgument, assigningType); } - if (type instanceof GenericArrayType) { // get raw component type final Class<?> rawComponentType = getRawType(((GenericArrayType) type).getGenericComponentType(), assigningType); - // create array type from raw component type and return its class return rawComponentType != null ? Array.newInstance(rawComponentType, 0).getClass() : null; } - // (hand-waving) this is not the method you're looking for if (type instanceof WildcardType) { return null; } - throw new IllegalArgumentException("unknown type: " + type); } @@ -804,7 +779,6 @@ private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, final C if (!isAssignable(cls, toClass)) { return null; } - // can't work with primitives if (cls.isPrimitive()) { // both classes are primitives? @@ -813,19 +787,15 @@ private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, final C // harvested with these two types. return new HashMap<>(); } - // work with wrapper the wrapper class instead of the primitive cls = ClassUtils.primitiveToWrapper(cls); } - // create a copy of the incoming map, or an empty one if it's null final HashMap<TypeVariable<?>, Type> typeVarAssigns = subtypeVarAssigns == null ? new HashMap<>() : new HashMap<>(subtypeVarAssigns); - // has target class been reached? if (toClass.equals(cls)) { return typeVarAssigns; } - // walk the inheritance hierarchy until the target class is reached return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns); } @@ -852,15 +822,12 @@ public static Map<TypeVariable<?>, Type> getTypeArguments(final ParameterizedTyp private static Map<TypeVariable<?>, Type> getTypeArguments(final ParameterizedType parameterizedType, final Class<?> toClass, final Map<TypeVariable<?>, Type> subtypeVarAssigns) { final Class<?> cls = getRawType(parameterizedType); - // make sure they're assignable if (!isAssignable(cls, toClass)) { return null; } - final Type ownerType = parameterizedType.getOwnerType(); final Map<TypeVariable<?>, Type> typeVarAssigns; - if (ownerType instanceof ParameterizedType) { // get the owner type arguments first final ParameterizedType parameterizedOwnerType = (ParameterizedType) ownerType; @@ -869,23 +836,19 @@ private static Map<TypeVariable<?>, Type> getTypeArguments(final ParameterizedTy // no owner, prep the type variable assignments map typeVarAssigns = subtypeVarAssigns == null ? new HashMap<>() : new HashMap<>(subtypeVarAssigns); } - // get the subject parameterized type's arguments final Type[] typeArgs = parameterizedType.getActualTypeArguments(); // and get the corresponding type variables from the raw class final TypeVariable<?>[] typeParams = cls.getTypeParameters(); - // map the arguments to their respective type variables for (int i = 0; i < typeParams.length; i++) { final Type typeArg = typeArgs[i]; typeVarAssigns.put(typeParams[i], typeVarAssigns.getOrDefault(typeArg, typeArg)); } - if (toClass.equals(cls)) { // target class has been reached. Done. return typeVarAssigns; } - // walk the inheritance hierarchy until the target class is reached return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns); } @@ -931,16 +894,13 @@ private static Map<TypeVariable<?>, Type> getTypeArguments(final Type type, fina if (type instanceof Class<?>) { return getTypeArguments((Class<?>) type, toClass, subtypeVarAssigns); } - if (type instanceof ParameterizedType) { return getTypeArguments((ParameterizedType) type, toClass, subtypeVarAssigns); } - if (type instanceof GenericArrayType) { return getTypeArguments(((GenericArrayType) type).getGenericComponentType(), toClass.isArray() ? toClass.getComponentType() : toClass, subtypeVarAssigns); } - // since wildcard types are not assignable to classes, should this just // return null? if (type instanceof WildcardType) { @@ -950,10 +910,8 @@ private static Map<TypeVariable<?>, Type> getTypeArguments(final Type type, fina return getTypeArguments(bound, toClass, subtypeVarAssigns); } } - return null; } - if (type instanceof TypeVariable<?>) { for (final Type bound : getImplicitBounds((TypeVariable<?>) type)) { // find the first bound that is assignable to the target class @@ -961,7 +919,6 @@ private static Map<TypeVariable<?>, Type> getTypeArguments(final Type type, fina return getTypeArguments(bound, toClass, subtypeVarAssigns); } } - return null; } throw new IllegalStateException("found an unhandled type: " + type); @@ -989,28 +946,23 @@ private static boolean isAssignable(final Type type, final Class<?> toClass) { // consistency with ClassUtils.isAssignable() behavior return toClass == null || !toClass.isPrimitive(); } - // only a null type can be assigned to null type which // would have cause the previous to return true if (toClass == null) { return false; } - // all types are assignable to themselves if (toClass.equals(type)) { return true; } - if (type instanceof Class<?>) { // just comparing two classes return ClassUtils.isAssignable((Class<?>) type, toClass); } - if (type instanceof ParameterizedType) { // only have to compare the raw type to the class return isAssignable(getRawType((ParameterizedType) type), toClass); } - // * if (type instanceof TypeVariable<?>) { // if any of the bounds are assignable to the class, then the @@ -1020,23 +972,19 @@ private static boolean isAssignable(final Type type, final Class<?> toClass) { return true; } } - return false; } - // the only classes to which a generic array type can be assigned // are class Object and array classes if (type instanceof GenericArrayType) { return toClass.equals(Object.class) || toClass.isArray() && isAssignable(((GenericArrayType) type).getGenericComponentType(), toClass.getComponentType()); } - // wildcard types are not assignable to a class (though one would think // "? super Object" would be assignable to Object) if (type instanceof WildcardType) { return false; } - throw new IllegalStateException("found an unhandled type: " + type); } @@ -1052,32 +1000,25 @@ private static boolean isAssignable(final Type type, final GenericArrayType toGe if (type == null) { return true; } - // only a null type can be assigned to null type which // would have cause the previous to return true if (toGenericArrayType == null) { return false; } - // all types are assignable to themselves if (toGenericArrayType.equals(type)) { return true; } - final Type toComponentType = toGenericArrayType.getGenericComponentType(); - if (type instanceof Class<?>) { final Class<?> cls = (Class<?>) type; - // compare the component types return cls.isArray() && isAssignable(cls.getComponentType(), toComponentType, typeVarAssigns); } - if (type instanceof GenericArrayType) { // compare the component types return isAssignable(((GenericArrayType) type).getGenericComponentType(), toComponentType, typeVarAssigns); } - if (type instanceof WildcardType) { // so long as one of the upper bounds is assignable, it's good for (final Type bound : getImplicitUpperBounds((WildcardType) type)) { @@ -1085,10 +1026,8 @@ private static boolean isAssignable(final Type type, final GenericArrayType toGe return true; } } - return false; } - if (type instanceof TypeVariable<?>) { // probably should remove the following logic and just return false. // type variables cannot specify arrays as bounds. @@ -1097,17 +1036,14 @@ private static boolean isAssignable(final Type type, final GenericArrayType toGe return true; } } - return false; } - if (type instanceof ParameterizedType) { // the raw type of a parameterized type is never an array or // generic array, otherwise the declaration would look like this: // Collection[]< ? extends String > collection; return false; } - throw new IllegalStateException("found an unhandled type: " + type); } @@ -1123,53 +1059,43 @@ private static boolean isAssignable(final Type type, final ParameterizedType toP if (type == null) { return true; } - // only a null type can be assigned to null type which // would have cause the previous to return true if (toParameterizedType == null) { return false; } - // cannot cast an array type to a parameterized type. if (type instanceof GenericArrayType) { return false; } - // all types are assignable to themselves if (toParameterizedType.equals(type)) { return true; } - // get the target type's raw type final Class<?> toClass = getRawType(toParameterizedType); // get the subject type's type arguments including owner type arguments // and supertype arguments up to and including the target class. final Map<TypeVariable<?>, Type> fromTypeVarAssigns = getTypeArguments(type, toClass, null); - // null means the two types are not compatible if (fromTypeVarAssigns == null) { return false; } - // compatible types, but there's no type arguments. this is equivalent // to comparing Map< ?, ? > to Map, and raw types are always assignable // to parameterized types. if (fromTypeVarAssigns.isEmpty()) { return true; } - // get the target type's type arguments including owner type arguments final Map<TypeVariable<?>, Type> toTypeVarAssigns = getTypeArguments(toParameterizedType, toClass, typeVarAssigns); - // now to check each type argument for (final TypeVariable<?> var : toTypeVarAssigns.keySet()) { final Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns); final Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns); - if (toTypeArg == null && fromTypeArg instanceof Class) { continue; } - // parameters must either be absent from the subject type, within // the bounds of the wildcard type, or be an exact match to the // parameters of the target type. @@ -1205,23 +1131,18 @@ private static boolean isAssignable(final Type type, final Type toType, final Ma if (toType == null || toType instanceof Class<?>) { return isAssignable(type, (Class<?>) toType); } - if (toType instanceof ParameterizedType) { return isAssignable(type, (ParameterizedType) toType, typeVarAssigns); } - if (toType instanceof GenericArrayType) { return isAssignable(type, (GenericArrayType) toType, typeVarAssigns); } - if (toType instanceof WildcardType) { return isAssignable(type, (WildcardType) toType, typeVarAssigns); } - if (toType instanceof TypeVariable<?>) { return isAssignable(type, (TypeVariable<?>) toType, typeVarAssigns); } - throw new IllegalStateException("found an unhandled type: " + toType); } @@ -1237,35 +1158,29 @@ private static boolean isAssignable(final Type type, final TypeVariable<?> toTyp if (type == null) { return true; } - // only a null type can be assigned to null type which // would have cause the previous to return true if (toTypeVariable == null) { return false; } - // all types are assignable to themselves if (toTypeVariable.equals(type)) { return true; } - if (type instanceof TypeVariable<?>) { // a type variable is assignable to another type variable, if // and only if the former is the latter, extends the latter, or // is otherwise a descendant of the latter. final Type[] bounds = getImplicitBounds((TypeVariable<?>) type); - for (final Type bound : bounds) { if (isAssignable(bound, toTypeVariable, typeVarAssigns)) { return true; } } } - if (type instanceof Class<?> || type instanceof ParameterizedType || type instanceof GenericArrayType || type instanceof WildcardType) { return false; } - throw new IllegalStateException("found an unhandled type: " + type); } @@ -1281,31 +1196,25 @@ private static boolean isAssignable(final Type type, final WildcardType toWildca if (type == null) { return true; } - // only a null type can be assigned to null type which // would have cause the previous to return true if (toWildcardType == null) { return false; } - // all types are assignable to themselves if (toWildcardType.equals(type)) { return true; } - final Type[] toUpperBounds = getImplicitUpperBounds(toWildcardType); final Type[] toLowerBounds = getImplicitLowerBounds(toWildcardType); - if (type instanceof WildcardType) { final WildcardType wildcardType = (WildcardType) type; final Type[] upperBounds = getImplicitUpperBounds(wildcardType); final Type[] lowerBounds = getImplicitLowerBounds(wildcardType); - for (Type toBound : toUpperBounds) { // if there are assignments for unresolved type variables, // now's the time to substitute them. toBound = substituteTypeVariables(toBound, typeVarAssigns); - // each upper bound of the subject type has to be assignable to // each // upper bound of the target type @@ -1315,12 +1224,10 @@ private static boolean isAssignable(final Type type, final WildcardType toWildca } } } - for (Type toBound : toLowerBounds) { // if there are assignments for unresolved type variables, // now's the time to substitute them. toBound = substituteTypeVariables(toBound, typeVarAssigns); - // each lower bound of the target type has to be assignable to // each // lower bound of the subject type @@ -1332,7 +1239,6 @@ private static boolean isAssignable(final Type type, final WildcardType toWildca } return true; } - for (final Type toBound : toUpperBounds) { // if there are assignments for unresolved type variables, // now's the time to substitute them. @@ -1340,7 +1246,6 @@ private static boolean isAssignable(final Type type, final WildcardType toWildca return false; } } - for (final Type toBound : toLowerBounds) { // if there are assignments for unresolved type variables, // now's the time to substitute them. @@ -1380,7 +1285,6 @@ public static boolean isInstance(final Object value, final Type type) { if (type == null) { return false; } - return value == null ? !(type instanceof Class<?>) || !((Class<?>) type).isPrimitive() : isAssignable(value.getClass(), type, null); } @@ -1396,29 +1300,23 @@ private static <T> void mapTypeVariablesToArguments(final Class<T> cls, final Pa final Map<TypeVariable<?>, Type> typeVarAssigns) { // capture the type variables from the owner type that have assignments final Type ownerType = parameterizedType.getOwnerType(); - if (ownerType instanceof ParameterizedType) { // recursion to make sure the owner's owner type gets processed mapTypeVariablesToArguments(cls, (ParameterizedType) ownerType, typeVarAssigns); } - // parameterizedType is a generic interface/class (or it's in the owner // hierarchy of said interface/class) implemented/extended by the class // cls. Find out which type variables of cls are type arguments of // parameterizedType: final Type[] typeArgs = parameterizedType.getActualTypeArguments(); - // of the cls's type variables that are arguments of parameterizedType, // find out which ones can be determined from the super type's arguments final TypeVariable<?>[] typeVars = getRawType(parameterizedType).getTypeParameters(); - // use List view of type parameters of cls so the contains() method can be used: final List<TypeVariable<Class<T>>> typeVarList = Arrays.asList(cls.getTypeParameters()); - for (int i = 0; i < typeArgs.length; i++) { final TypeVariable<?> typeVar = typeVars[i]; final Type typeArg = typeArgs[i]; - // argument of parameterizedType is a type variable of cls if (typeVarList.contains(typeArg) // type variable of parameterizedType has an assignment in @@ -1460,24 +1358,19 @@ public static Type[] normalizeUpperBounds(final Type[] bounds) { if (bounds.length < 2) { return bounds; } - final Set<Type> types = new HashSet<>(bounds.length); - for (final Type type1 : bounds) { boolean subtypeFound = false; - for (final Type type2 : bounds) { if (type1 != type2 && isAssignable(type2, type1, null)) { subtypeFound = true; break; } } - if (!subtypeFound) { types.add(type1); } } - return types.toArray(ArrayUtils.EMPTY_TYPE_ARRAY); }