简体   繁体   English

获得通用类型的“真实”类

[英]Get “real” class of generic type

How can I get the "real" class of a generic type? 如何获得泛型类型的“真实”类?

For Example: 例如:

public class MyClass<T> {
    public void method(){
        //something

        System.out.println(T.class) //causes a compile error, I wont the class name

        //something
    }
}

If T = Integer 如果T =整数

Output: 输出:

java.lang.Integer

If T = String 如果T = String

Output: 输出:

java.lang.String

Thanks 谢谢

If you have a instance variable of type T in your class, and it happens to be set, then you could print the class of that variable. 如果您的类中有一个类型为T的实例变量,并且恰好已设置,则可以打印该变量的类。

public class Test<T> {

    T var;

    public static void main(String[] args) {
        Test<Integer> a = new Test<Integer>();
        System.out.println(a.boo());
        a.setVar(new Integer(10));
        System.out.println(a.boo());
    }

    public String boo() {
        if (var == null) {
            return "Don't know yet";
        }
        return var.getClass().getSimpleName();
    }

    public void setVar(T var) {
        this.var = var;
    }

    public T getVar() {
        return var;
    }
}

You can't. 你不能。 The information is stripped from the code at compile time, a process that is known as type erasure. 在编译时从代码中剥离信息,这个过程称为类型擦除。 For more, please look here: Type Erasure 有关更多信息,请查看此处: 类型擦除

edit: sorry my bad, the information is not loaded at run time. 编辑:对不起我的错,信息没有在运行时加载。

As others have explained, you cannot do it in that fashion but this is how it's usually achieved in java. 正如其他人所解释的那样,你不能以这种方式去做,但这就是它通常在java中实现的方式。

public class MyClass<T> {
    public void method(Class<T> clazz) {
        // something

        System.out.println(clazz.getName());

        // something
    }
}

and you use it like this 你像这样使用它

new MyClass<String>().method(String.class);

In the case of your situation, you can't. 在你的情况下,你不能。 However, you might be able to use Super Type Tokens for this type of thing: http://gafter.blogspot.com/2006/12/super-type-tokens.html 但是,您可能可以使用超类型标记来执行此类操作: http//gafter.blogspot.com/2006/12/super-type-tokens.html

An example implementation of these is the TypeReference class of the Jackson json processing library. 这些的示例实现是Jackson json处理库的TypeReference类。

This is advanced stuff and probably more than you wanted to know ;-) 这是高级的东西,可能比你想知道的更多;-)

Note that approaches relying on 'getClass()' on an instance received with a generic type will get the actual type of that object, which is not necessarily the generic type - which would be the type by which the caller knew the instance. 请注意,在使用泛型类型接收的实例上依赖于'getClass()'的方法将获得该对象的实际类型,这不一定是泛型类型 - 这将是调用者知道该实例的类型。

For example, consider the case where the caller handles an object by an interface; 例如,考虑调用者通过接口处理对象的情况; when passing to generic constructs, the generic type will be the interface, not the instance's actual class. 传递给泛型构造时,泛型类型将是接口,而不是实例的实际类。

Consider the following example "Pair" class, which allows two object references to be returned through a POJO: 考虑以下示例“Pair”类,它允许通过POJO返回两个对象引用:

public class Pair<U,V>
{
    public final U first;
    public final V second;
    public static <U,V> Pair<U,V> of (U first, V second)
    {
        return new Pair<U,V> (first, second);
    }
    protected Pair (U first, V second)
    {
        this.first  = first;
        this.second = second;
    }
}

We were considering how to modify the 'Pair.of()' factory function to return a Comparable Pair derived class, if U and V were both Comparable. 我们正在考虑如何修改'Pair.of()'工厂函数以返回Comparable Pair派生类,如果U和V都是Comparable。 However, while we can tell whether 'first' and 'second' are comparable using instanceof, we don't know that 'U' and 'V' are themselves comparable. 然而,虽然我们可以使用instanceof来判断“第一”和“第二”是否具有可比性,但我们不知道“U”和“V”本身是可比较的。

For this to work, the exact type of Pair returned by Pair.of() must depend on the generic types, not the actual argument types. 为此,Pair.of()返回的Pair的确切类型必须依赖于泛型类型, 而不是实际的参数类型。

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

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