繁体   English   中英

在Java通用接口中推断类参数的类型

[英]Inferring type of class arguments in Java generic interface

请注意:我已经整整一整天确保不会重复。 我见过的大多数解决方案都依赖于对clazz.getGenericSuperclass()的调用,然后再调用getActualTypeArguments() ,但这并不能解决我的问题,因为通用超类是java.lang.Object

请继续阅读。


现在解决问题

我想实现一个提供通用接口的库。

public interface MyInterface<K, V> {
    public V get(K key);
    public void set(K key, V value);
}

用户然后可以在一个具体的类中实现它,例如:

import java.util.HashMap;

public class ConcreteClass implements MyInterface<String, Integer> {
    private HashMap< String, Integer > myMap;

    public ConcreteClass() {
    this.myMap = new HashMap< >();
    }

    public Integer get(String key) {
    return myMap.get(key);
    }

    public void set(String key, Integer value) {
    myMap.put(key, value);
    }
}

或者他们可能实现另一个通用类,例如:

import java.util.HashMap;

public class GenericClass<K, V> implements MyInterface<K, V> {
    private HashMap<K, V> myMap;

    public GenericClass() {
    this.myMap = new HashMap<K, V>();
    }

    public V get(K key) {
    return myMap.get(key);
    }

    public void set(K key, V value) {
    myMap.put(key, value);
    }
}

现在,我希望能够推断这些类的任何实例的通用类型。 我正在尝试通过Main.java inferTypes方法实现此inferTypes ,如下所示:

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {
    private static <K, V> void inferTypes(MyInterface<K, V> mi) {
        Class<?> clazz = mi.getClass();

        // Should print whether "ConcreteClass" or "GenericClass"
        System.out.println(clazz.getName());

        Type[] ifaces = clazz.getGenericInterfaces();
        for (Type iface : ifaces) {
            // Should print "MyInterface<String, Integer>"
            System.out.println(iface.toString());
            Type[] types = ((ParameterizedType) iface).getActualTypeArguments();
            for (Type type : types) {
                // Should print "String" and then "Integer"
                System.out.println(type.toString());
            }
        }
    }

    public static void main(String[] args) {
        // Someone instantiates the class
        ConcreteClass cc = new ConcreteClass();
        // And I can successfully infers the type
        Main.inferTypes(cc);

        System.out.println("-------");

        // Someone instantiates the generic class
        GenericClass<String, Integer> gc = new GenericClass<>();
        // And I can't infer the types this time
        Main.inferTypes(gc);
    }
}

它适用于ConcreteClass实例,但不适用于GenericClass实例。

ConcreteClass
MyInterface<java.lang.String, java.lang.Integer>
class java.lang.String
class java.lang.Integer
-------
GenericClass
MyInterface<K, V>
K
V

我无法弄清楚如何为第二种情况获得具体的课程。 任何帮助将非常感激。

对于GenericClass<K, V> ,在类文件中没有确定类型的信息。 这是由于类型擦除。

您能做的最好的就是从Map的内容中推断类型。 如果您需要记录类型可以

public class GenericClass<K, V> implements MyInterface<K, V> {
    private final Map<K, V> myMap;
    private final Class<K> keyClass;
    private final Class<V> valueClass

    public GenericClass(Class<K> keyClass, Class<V> valueClass) {
        this.myMap = new HashMap<K, V>();
    }

    public Class<K> getKeyClass() { return keyClass; }
    public Class<V> getValueClass() { return valueClass; }

即,您需要显式存储类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM