Java Code Examples for java.lang.reflect.TypeVariable#getGenericDeclaration()

The following examples show how to use java.lang.reflect.TypeVariable#getGenericDeclaration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldInfo.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) {
    Type type = null;
    GenericDeclaration gd = tv.getGenericDeclaration();
    do {
        type = clazz.getGenericSuperclass();
        if (type == null) {
            return null;
        }
        if (type instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedType) type;
            if (ptype.getRawType() == gd) {
                TypeVariable<?>[] tvs = gd.getTypeParameters();
                Type[] types = ptype.getActualTypeArguments();
                for (int i = 0; i < tvs.length; i++) {
                    if (tvs[i] == tv)
                        return types[i];
                }
                return null;
            }
        }
        clazz = TypeUtils.getClass(type);
    } while (type != null);
    return null;
}
 
Example 2
Source File: SServicesMap.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
public Class<?> getGenericType(Method method) {
	Type genericReturnType = method.getGenericReturnType();
	if (method.getGenericReturnType() instanceof ParameterizedType) {
		ParameterizedType parameterizedTypeImpl = (ParameterizedType)genericReturnType;
		Type first = parameterizedTypeImpl.getActualTypeArguments()[0];
		if (first instanceof WildcardType) {
			return null;
		} else if (first instanceof ParameterizedType) {
			return null;
		} else if (first instanceof TypeVariable) {
			TypeVariable<?> typeVariableImpl = (TypeVariable<?>)first;
			GenericDeclaration genericDeclaration = typeVariableImpl.getGenericDeclaration();
			if (genericDeclaration instanceof Class) {
				return (Class<?>) genericDeclaration;
			}
		} else {
			return (Class<?>) first;
		}
	}
	Type genericReturnType2 = method.getGenericReturnType();
	if (genericReturnType2 instanceof Class) {
		return (Class<?>)genericReturnType2;
	}
	return null;
}
 
Example 3
Source File: TypeVariableImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o instanceof TypeVariable &&
            o.getClass() == TypeVariableImpl.class) {
        TypeVariable<?> that = (TypeVariable<?>) o;

        GenericDeclaration thatDecl = that.getGenericDeclaration();
        String thatName = that.getName();

        return Objects.equals(genericDeclaration, thatDecl) &&
            Objects.equals(name, thatName);

    } else
        return false;
}
 
Example 4
Source File: TypeVariableImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o instanceof TypeVariable &&
            o.getClass() == TypeVariableImpl.class) {
        TypeVariable<?> that = (TypeVariable<?>) o;

        GenericDeclaration thatDecl = that.getGenericDeclaration();
        String thatName = that.getName();

        return Objects.equals(genericDeclaration, thatDecl) &&
            Objects.equals(name, thatName);

    } else
        return false;
}
 
Example 5
Source File: TestPlainArrayNotGeneric.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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() + ")");
    }
}
 
Example 6
Source File: TypeVariableImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o instanceof TypeVariable &&
            o.getClass() == TypeVariableImpl.class) {
        TypeVariable<?> that = (TypeVariable<?>) o;

        GenericDeclaration thatDecl = that.getGenericDeclaration();
        String thatName = that.getName();

        return Objects.equals(genericDeclaration, thatDecl) &&
            Objects.equals(name, thatName);

    } else
        return false;
}
 
Example 7
Source File: RetroTypes.java    From wasp with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if {@code a} and {@code b} are equal.
 */
public 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!
  }
}
 
Example 8
Source File: TestPlainArrayNotGeneric.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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() + ")");
    }
}
 
Example 9
Source File: TestPlainArrayNotGeneric.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
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() + ")");
    }
}
 
Example 10
Source File: Main.java    From Java-Coding-Problems with MIT License 5 votes vote down vote up
private static void printGenericsOfExceptions(Type genericType) {
    
    if (genericType instanceof TypeVariable) {

        TypeVariable typeVariable = (TypeVariable) genericType;
        GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();

        System.out.println("Generic declaration: " + genericDeclaration);
            System.out.println("Bounds: ");
            for (Type type: typeVariable.getBounds()) {
                System.out.println(type);
            }
    }
}
 
Example 11
Source File: TestPlainArrayNotGeneric.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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() + ")");
    }
}
 
Example 12
Source File: $Gson$Types.java    From sagetv with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the declaring class of {@code typeVariable}, or {@code null} if it was not declared by
 * a class.
 */
private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {
  GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
  return genericDeclaration instanceof Class
      ? (Class<?>) genericDeclaration
      : null;
}
 
Example 13
Source File: AnnotatedMember.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected final  <S extends Parameterized> Class<?> resolveType(S context, Type type, Class<?> defaultRawType) {
	Class<?> result=defaultRawType;
	if(type instanceof TypeVariable<?>) {
		TypeVariable<? extends GenericDeclaration> variable=(TypeVariable<? extends GenericDeclaration>)type;
		if(variable.getGenericDeclaration() instanceof Type) {
			Class<?> argument=
				getDeclaringClass().
					getActualParameterArgument(
						variable.getName());
			if(argument!=null) {
				result=argument;
			}
		} else {
			Type[] bounds = variable.getBounds();
			if(bounds.length>0) {
				Type bound = bounds[0];
				if(bound instanceof Class<?>) {
					result=(Class<?>)bound;
				} else if(bound instanceof ParameterizedType) {
					result=(Class<?>)((ParameterizedType)bound).getRawType();
				} else {
					throw new IllegalStateException(String.format("Unexpected bound type '%s' (%s)", bound.getClass().getCanonicalName(),TypeUtils.toString(bound)));
				}
			} 
		}
	}
	return result;
}
 
Example 14
Source File: MetaProperty.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static MetaProperty.VariableScope fromType(Type genericType) {
	MetaProperty.VariableScope result=null;
	if(genericType instanceof TypeVariable<?>) {
		TypeVariable<? extends GenericDeclaration> tv=(TypeVariable<? extends GenericDeclaration>)genericType;
		if(tv.getGenericDeclaration() instanceof Type) {
			result=TYPE;
		} else {
			result=METHOD;
		}
	}
	return result;
}
 
Example 15
Source File: Types.java    From Jolyglot with Apache License 2.0 4 votes vote down vote up
/** 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;
    Type[] aTypeArguments = pa instanceof ParameterizedTypeImpl
        ? ((ParameterizedTypeImpl) pa).typeArguments
        : pa.getActualTypeArguments();
    Type[] bTypeArguments = pb instanceof ParameterizedTypeImpl
        ? ((ParameterizedTypeImpl) pb).typeArguments
        : pb.getActualTypeArguments();
    return equal(pa.getOwnerType(), pb.getOwnerType())
        && pa.getRawType().equals(pb.getRawType())
        && Arrays.equals(aTypeArguments, bTypeArguments);

  } 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 {
    // This isn't a supported type.
    return false;
  }
}
 
Example 16
Source File: b.java    From letv with Apache License 2.0 4 votes vote down vote up
public static boolean a(Type type, Type type2) {
    Object obj = type2;
    Object obj2 = type;
    while (obj2 != obj) {
        if (obj2 instanceof Class) {
            return obj2.equals(obj);
        }
        if (obj2 instanceof ParameterizedType) {
            if (!(obj instanceof ParameterizedType)) {
                return false;
            }
            ParameterizedType parameterizedType = (ParameterizedType) obj2;
            ParameterizedType parameterizedType2 = (ParameterizedType) obj;
            Type ownerType = parameterizedType.getOwnerType();
            Type ownerType2 = parameterizedType2.getOwnerType();
            Object obj3 = (ownerType == ownerType2 || (ownerType != null && ownerType.equals(ownerType2))) ? 1 : null;
            return obj3 != null && parameterizedType.getRawType().equals(parameterizedType2.getRawType()) && Arrays.equals(parameterizedType.getActualTypeArguments(), parameterizedType2.getActualTypeArguments());
        } else if (obj2 instanceof GenericArrayType) {
            if (!(obj instanceof GenericArrayType)) {
                return false;
            }
            GenericArrayType genericArrayType = (GenericArrayType) obj;
            obj2 = ((GenericArrayType) obj2).getGenericComponentType();
            obj = genericArrayType.getGenericComponentType();
        } else if (obj2 instanceof WildcardType) {
            if (!(obj instanceof WildcardType)) {
                return false;
            }
            WildcardType wildcardType = (WildcardType) obj2;
            WildcardType wildcardType2 = (WildcardType) obj;
            return Arrays.equals(wildcardType.getUpperBounds(), wildcardType2.getUpperBounds()) && Arrays.equals(wildcardType.getLowerBounds(), wildcardType2.getLowerBounds());
        } else if (!(obj2 instanceof TypeVariable)) {
            return false;
        } else {
            if (!(obj instanceof TypeVariable)) {
                return false;
            }
            TypeVariable typeVariable = (TypeVariable) obj2;
            TypeVariable typeVariable2 = (TypeVariable) obj;
            return typeVariable.getGenericDeclaration() == typeVariable2.getGenericDeclaration() && typeVariable.getName().equals(typeVariable2.getName());
        }
    }
    return true;
}
 
Example 17
Source File: TypeUtils.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
/** 判断是否传入泛型 */
private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {
    GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
    return genericDeclaration instanceof Class ? (Class<?>) genericDeclaration : null;
}
 
Example 18
Source File: SimpleParameterizedType.java    From MOE with Apache License 2.0 4 votes vote down vote up
/**
 * Returns true if {@code a} and {@code b} are equal.
 *
 * <p>Yoinked from {@link com.google.gson.internal.$Gson$Types#equals(Object)}
 */
static boolean equals(Type a, Type b) {
  if (a == b) {
    // also handles (a == null && b == null)
    return true;

  } else if (a instanceof Class) {
    // Class already specifies equals().
    return a.equals(b);

  } else if (a instanceof ParameterizedType) {
    if (!(b instanceof ParameterizedType)) {
      return false;
    }

    // TODO: save a .clone() call
    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 {
    // This isn't a type we support. Could be a generic array type, wildcard type, etc.
    return false;
  }
}
 
Example 19
Source File: TypeParameterMatcher.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
private static Class<?> find0(final Object object, Class<?> parameterizedSuperclass, String typeParamName) {

        final Class<?> thisClass = object.getClass();
        Class<?> currentClass = thisClass;
        for (;;) {
            if (currentClass.getSuperclass() == parameterizedSuperclass) {
                int typeParamIndex = -1;
                final TypeVariable<?>[] typeParams = currentClass.getSuperclass().getTypeParameters();
                for (int i = 0; i < typeParams.length; i ++) {
                    if (typeParamName.equals(typeParams[i].getName())) {
                        typeParamIndex = i;
                        break;
                    }
                }

                if (typeParamIndex < 0) {
                    throw new IllegalStateException(
                            "unknown type parameter '" + typeParamName + "': " + parameterizedSuperclass);
                }

                final Type genericSuperType = currentClass.getGenericSuperclass();
                if (!(genericSuperType instanceof ParameterizedType)) {
                    return Object.class;
                }

                final Type[] actualTypeParams = ((ParameterizedType) genericSuperType).getActualTypeArguments();

                Type actualTypeParam = actualTypeParams[typeParamIndex];
                if (actualTypeParam instanceof ParameterizedType) {
                    actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
                }
                if (actualTypeParam instanceof Class) {
                    return (Class<?>) actualTypeParam;
                }
                if (actualTypeParam instanceof GenericArrayType) {
                    Type componentType = ((GenericArrayType) actualTypeParam).getGenericComponentType();
                    if (componentType instanceof ParameterizedType) {
                        componentType = ((ParameterizedType) componentType).getRawType();
                    }
                    if (componentType instanceof Class) {
                        return Array.newInstance((Class<?>) componentType, 0).getClass();
                    }
                }
                if (actualTypeParam instanceof TypeVariable) {
                    // Resolved type parameter points to another type parameter.
                    TypeVariable<?> v = (TypeVariable<?>) actualTypeParam;
                    currentClass = thisClass;
                    if (!(v.getGenericDeclaration() instanceof Class)) {
                        return Object.class;
                    }

                    parameterizedSuperclass = (Class<?>) v.getGenericDeclaration();
                    typeParamName = v.getName();
                    if (parameterizedSuperclass.isAssignableFrom(thisClass)) {
                        continue;
                    } else {
                        return Object.class;
                    }
                }

                return fail(thisClass, typeParamName);
            }
            currentClass = currentClass.getSuperclass();
            if (currentClass == null) {
                return fail(thisClass, typeParamName);
            }
        }
    }
 
Example 20
Source File: Types.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the declaring class of {@code typeVariable}, or {@code null} if it was not declared by
 * a class.
 */
private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {
    GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
    return genericDeclaration instanceof Class ? (Class<?>) genericDeclaration : null;
}