获取具体地图的类

时间:2018-08-02 08:10:26

标签: java reflection

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

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

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

2 个答案:

答案 0 :(得分:0)

没有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

答案 1 :(得分:0)

Map<Long, String>不是类,但它是一种类型,确切地说,ParameterizedType是可悲的,用于构造它们的Java代码是私有的,但是它们是获得它的2种方法,更动态的方法是实现该接口:

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<Map<String, Long>>{}.getType();-需要在编译时提供类型。
应该有一些提供这两种方法的库,但是我现在还不知道,因为我需要自己编写自己的库以支持从字符串解析。