Вот код, который я написал, который расскажет вам об общих типах класса. Используйте это для сравнения универсальных типов A с target.getClass ():
/**
* Gets a list of type Class that contains the type arguments that a child class has used to extend a generic base
* class.
*
* For example, if a class called ChildClass has this signature:
*
* <code>
* public class ChildClass extends ParentClass<Integer, String>
* </code>
*
* then the list returned would have two entries: Integer and String.
*
* @param baseClass The generic base class being extended.
* @param childClass The child class that is doing the extending.
* @return A list of type Class containing the raw classes for the type arguments.
*/
public <T> List<Class<?>> getTypeArguments(Class<T> baseClass, Class<? extends T> childClass) {
Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
Type type = childClass;
// start walking up the inheritance hierarchy until we hit baseClass
while (getClass(type) != null && !getClass(type).equals(baseClass)) {
if (type instanceof Class) {
// there is no useful information for us in raw types, so just keep going.
type = ((Class<?>) type).getGenericSuperclass();
} else {
ParameterizedType parameterizedType = (ParameterizedType) type;
Class<?> rawType = (Class<?>) parameterizedType.getRawType();
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
TypeVariable<?>[] typeParameters = rawType.getTypeParameters();
for (int i = 0; i < actualTypeArguments.length; i++) {
resolvedTypes.put(typeParameters[i], actualTypeArguments[i]);
}
if (!rawType.equals(baseClass)) {
type = rawType.getGenericSuperclass();
}
}
}
// finally, for each actual type argument provided to baseClass, determine (if possible) the raw class for that
// type argument
Type[] actualTypeArguments;
if (type instanceof Class) {
actualTypeArguments = ((Class<?>) type).getTypeParameters();
} else {
actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
}
// convert types to their raw classes
List<Class<?>> typeArgumentsAsClasses = new ArrayList<Class<?>>();
for (Type baseType : actualTypeArguments) {
while (resolvedTypes.containsKey(baseType)) {
baseType = resolvedTypes.get(baseType);
}
typeArgumentsAsClasses.add(getClass(baseType));
}
return typeArgumentsAsClasses;
}
/**
* Gets the Class for a Type. If the Type is a variable type, null is returned.
*
* @param type The Type to get the Class for
* @return Returns the Class, unless Type is a variable type, then null is returned.
*/
public Class<?> getClass(Type type) {
Class<?> returnClass = null;
if (type instanceof Class) {
returnClass = (Class<?>) type;
} else if (type instanceof ParameterizedType) {
returnClass = getClass(((ParameterizedType) type).getRawType());
} else if (type instanceof GenericArrayType) {
Class<?> componentClass = getClass(((GenericArrayType) type).getGenericComponentType());
if (componentClass != null) {
returnClass = Array.newInstance(componentClass, 0).getClass();
}
}
return returnClass;
}