Java 获取一个具体地图的类
给定2个类对象,如何获取地图的类对象? 例如,假设我有:Java 获取一个具体地图的类,java,reflection,Java,Reflection,给定2个类对象,如何获取地图的类对象? 例如,假设我有: Class keyClass = Long.class; Class valueClass = String.class; 如何获取Map的类对象?没有这样的Map类。您需要的是Map.class。(或HashMap.class等) Map map1=newhashmap(); Map map2=新的HashMap(); System.out.println(map1.getClass().equals(map2.getClass())
Class keyClass = Long.class;
Class valueClass = String.class;
如何获取
Map
的类对象?没有这样的Map
类。您需要的是Map.class
。(或HashMap.class
等)
Map map1=newhashmap();
Map map2=新的HashMap();
System.out.println(map1.getClass().equals(map2.getClass());
结果是true
Map
不是一个类,但它是一个类型,参数化类型
准确地说,遗憾的是,构建它们的java代码是私有的,但它们有两种方法,更动态的方法是实现该接口:
final class ParameterizedTypeImpl implements ParameterizedType {
private final Type[] actualTypeArguments;
private final Class rawType;
@Nullable private final Type ownerType;
ParameterizedTypeImpl(Class rawType, Type[] actualTypeArguments, @Nullable Type ownerType) {
this.actualTypeArguments = actualTypeArguments.clone();
this.rawType = rawType;
if ((ownerType != null) || (rawType.getDeclaringClass() == null)) {
this.ownerType = ownerType;
}
else {
Class declaringClass = rawType.getDeclaringClass();
if (Modifier.isStatic(rawType.getModifiers())) {
this.ownerType = declaringClass;
}
else {
TypeVariable[] typeParameters = declaringClass.getTypeParameters();
if (typeParameters.length == 0) {
this.ownerType = declaringClass;
}
else {
this.ownerType = new ParameterizedTypeImpl(declaringClass, typeParameters, null);
}
}
}
}
@Override
public Type[] getActualTypeArguments() { return this.actualTypeArguments.clone(); }
@Override
public Class getRawType() { return this.rawType; }
@Nullable @Override
public Type getOwnerType() { return this.ownerType; }
@Override public boolean equals(Object o) {
if (o instanceof ParameterizedType) {
ParameterizedType that = (ParameterizedType) o;
if (this == that) return true;
Type thatOwner = that.getOwnerType();
Type thatRawType = that.getRawType();
return Objects.equals(this.ownerType, thatOwner) && Objects.equals(this.rawType, thatRawType) && Arrays.equals(this.actualTypeArguments, that.getActualTypeArguments());
}
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(this.actualTypeArguments) ^ Objects.hashCode(this.ownerType) ^ Objects.hashCode(this.rawType);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(256);
if (this.ownerType != null) {
sb.append(this.ownerType.getTypeName());
sb.append("$");
if (this.ownerType instanceof ParameterizedTypeImpl) {
sb.append(this.rawType.getName().replace(((ParameterizedTypeImpl) this.ownerType).rawType.getName() + "$", ""));
}
else {
sb.append(this.rawType.getSimpleName());
}
}
else {
sb.append(this.rawType.getName());
}
StringJoiner joiner = new StringJoiner(", ", "<", ">");
joiner.setEmptyValue("");
for (Type type : this.actualTypeArguments) {
joiner.add(type.getTypeName());
}
sb.append(joiner.toString());
return sb.toString();
}
}
然后newtypetoken{}.getType()代码>-需要在编译时提供类型。
应该有一些库提供这两种方法,但我现在还不知道任何库,因为我需要创建自己的库来支持从字符串进行解析。给定一个map对象,可以调用myMap.getClass()
。map的类不包含有关键的类
或值的类
的信息。只有map.class
。Map
没有类对象,因为泛型类型参数在运行时被删除。此外,在exmaple中,您缺少泛型类型参数。它应该是Class-keyClass=Long.Class
(但是看看OP的问题,Map.Class
可能是正确的答案),而不是map1.getClass().getGenericInterfaces()!=map2.getClass().getGenericInterfaces()@Ran因为它们是数组,所以使用arrays.equals(map1.getClass().getGenericInterfaces(),map2.getClass().getGenericInterfaces())
。
final class ParameterizedTypeImpl implements ParameterizedType {
private final Type[] actualTypeArguments;
private final Class rawType;
@Nullable private final Type ownerType;
ParameterizedTypeImpl(Class rawType, Type[] actualTypeArguments, @Nullable Type ownerType) {
this.actualTypeArguments = actualTypeArguments.clone();
this.rawType = rawType;
if ((ownerType != null) || (rawType.getDeclaringClass() == null)) {
this.ownerType = ownerType;
}
else {
Class declaringClass = rawType.getDeclaringClass();
if (Modifier.isStatic(rawType.getModifiers())) {
this.ownerType = declaringClass;
}
else {
TypeVariable[] typeParameters = declaringClass.getTypeParameters();
if (typeParameters.length == 0) {
this.ownerType = declaringClass;
}
else {
this.ownerType = new ParameterizedTypeImpl(declaringClass, typeParameters, null);
}
}
}
}
@Override
public Type[] getActualTypeArguments() { return this.actualTypeArguments.clone(); }
@Override
public Class getRawType() { return this.rawType; }
@Nullable @Override
public Type getOwnerType() { return this.ownerType; }
@Override public boolean equals(Object o) {
if (o instanceof ParameterizedType) {
ParameterizedType that = (ParameterizedType) o;
if (this == that) return true;
Type thatOwner = that.getOwnerType();
Type thatRawType = that.getRawType();
return Objects.equals(this.ownerType, thatOwner) && Objects.equals(this.rawType, thatRawType) && Arrays.equals(this.actualTypeArguments, that.getActualTypeArguments());
}
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(this.actualTypeArguments) ^ Objects.hashCode(this.ownerType) ^ Objects.hashCode(this.rawType);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(256);
if (this.ownerType != null) {
sb.append(this.ownerType.getTypeName());
sb.append("$");
if (this.ownerType instanceof ParameterizedTypeImpl) {
sb.append(this.rawType.getName().replace(((ParameterizedTypeImpl) this.ownerType).rawType.getName() + "$", ""));
}
else {
sb.append(this.rawType.getSimpleName());
}
}
else {
sb.append(this.rawType.getName());
}
StringJoiner joiner = new StringJoiner(", ", "<", ">");
joiner.setEmptyValue("");
for (Type type : this.actualTypeArguments) {
joiner.add(type.getTypeName());
}
sb.append(joiner.toString());
return sb.toString();
}
}
public class TypeToken<T> {
final Type type;
protected TypeToken() {
this.type = this.getClass().getGenericSuperclass();
}
public final Type getType() { return this.type; }
@Override public final int hashCode() { return this.type.hashCode(); }
@Override public final boolean equals(Object o) { return (o instanceof TypeToken<?>) && this.type.equals(((TypeToken<?>) o).type); }
@Override public final String toString() { return this.type.toString(); }
}