public class A<T> {
public <K> void m(A<K> target) {
// determine if T equals K
}
}
是否可以检查<T>
和<K>
是否相同?
答案 0 :(得分:6)
是的,这与通用TypeReference的工作方式相同。通常,类型会被删除,但它适用于匿名内部类:
public abstract class A<T> {
private Type type;
public A() {
Type superclass = getClass().getGenericSuperclass();
this.type = ((ParameterizedType) superclass).getActualTypeArguments()[0];
}
public <K> void m(A<K> target) {
System.out.println( type.equals( target.type ) );
}
}
使用它:
A<String> as = new A<String>(){};
A<String> a2s = new A<String>(){};
A<Integer> ai = new A<Integer>(){};
as.m(a2s); // prints true
as.m(ai); // prints false
该类不必是抽象的,但它可以作为使其成为匿名内部类的提示。唯一真正的缺点是你必须把{}
放在最后。
答案 1 :(得分:2)
这并不容易,因为javas类型擦除。在运行时,T和K的类型信息将丢失。
但是如果你能得到这些类型的实例,你可以在编译时检查它们:
public class A<T> {
private T t;
public T getT() { return t; }
public A(T t) {
this.t = t;
}
public <K> m(A<K> target) {
// determine if T equals K
boolean areEqual = t.getClass().equals(target.getT().getClass());
}
}
但是,这意味着您需要访问对象的实例。
答案 2 :(得分:1)
不,你最接近的是:
public class A<T> {
private Class<T> _type;
public A(Class<T> type) {
assert type != null;
_type = type;
}
public <K> void m(A<K> target) {
if (target != null && target._type == this._type) {
...
}
}
您可以在EnumMap
等类型中看到将Class<T>
作为构造函数参数传递的惯用语。
答案 3 :(得分:0)
不是真的,只有当你有T
的实例或传递给A
的类或想要比较它们的方法时。类型擦除...
答案 4 :(得分:0)
这是我写的代码,它将告诉你类的泛型类型。使用它来比较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;
}