【问题标题】:Get Class of a concrete Map获取具体地图的类
【发布时间】:2018-08-02 08:10:26
【问题描述】:

给定 2 个 Class 对象,我如何获得 Map 的 Class 对象? 例如,假设我有:

Class keyClass = Long.class;
Class valueClass = String.class;

如何获取Map<Long,String>Class对象?

【问题讨论】:

  • 给定一个地图对象,你调用myMap.getClass()。 map 类不包含key 类或value 类的信息。
  • 只有Map.classMap<Long, String> 没有类对象,因为泛型类型参数在运行时被擦除。
  • 另外,在您的示例中,您缺少泛型类型参数。应该是Class<Long> keyClass = Long.class
  • (不过看看 OP 的问题,Map.class 可能是正确答案)

标签: java reflection


【解决方案1】:

没有Map<Long, String> 这样的类。你想要的是Map.class。 (或HashMap.class等)

Map<String, Integer> map1 = new HashMap<>();
Map<Long, String> map2 = new HashMap<>();
System.out.println(map1.getClass().equals(map2.getClass()));

结果是true

【讨论】:

  • 但是 map1.getClass().getGenericInterfaces() != map2.getClass().getGenericInterfaces()
  • @Ran 因为是数组,所以使用Arrays.equals(map1.getClass().getGenericInterfaces(), map2.getClass().getGenericInterfaces())
【解决方案2】:

Map&lt;Long, String&gt; 不是一个类,而是一个类型,确切地说是ParameterizedType,遗憾的是用于构造它们的 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();
    }
}

然后你可以只做new ParameterizedTypeImpl(Map.class, new Type[]{String.class, Long.class}, null) 请注意,让这个类对其他人不可见并创建一些工厂方法是一个好习惯。

其他不太动态的方法是使用类型标记,例如在 gson 中:

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(); }
}

然后new TypeToken&lt;Map&lt;String, Long&gt;&gt;{}.getType(); - 需要在编译时提供类型。
应该有一些库提供这两种方法,但我现在不知道,因为我需要自己制作以支持从字符串解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多