Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取一个具体地图的类_Java_Reflection - Fatal编程技术网

Java 获取一个具体地图的类

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

给定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());
结果是
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(); }
}