Java-通用类型-类型擦除 [英] Java - Generic Types - Type Erasure

查看:76
本文介绍了Java-通用类型-类型擦除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在oracle网站上发现了以下问答

I found the following question and answer on the oracle website

在擦除类型后,以下类将转换为什么?

public class Pair<K, V> {

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey(); { return key; }
    public V getValue(); { return value; }

    public void setKey(K key)     { this.key = key; }
    public void setValue(V value) { this.value = value; }

    private K key;
    private V value;
}

答案:

public class Pair {

    public Pair(Object key, Object value) {
        this.key = key;
        this.value = value;
    }

    public Object getKey()   { return key; }
    public Object getValue() { return value; }

    public void setKey(Object key)     { this.key = key; }
    public void setValue(Object value) { this.value = value; }

    private Object key;
    private Object value;
}

但是当我使用JD-GUI反编译类文件时,仍然看到所有通用类型与实际源代码中的相似.

But when i decompile the class file using JD-GUI, i still see all the generic types similar what i have in the actual source code.

这是否意味着泛型类型保留在类文件中?我正在使用Java 6.

Does this mean that the generic types are retained in the class file? i'm using Java 6.

使用JD-GUI的类的反编译器版本如下.我正在使用Eclipse Helios来编译类.

The decompiler version of the class using JD-GUI is as follows. I'm using Eclipse Helios to compile the class.

public class Pair<K, V>
{
  private K key;
  private V value;

  public Pair(K key, V value)
  {
    this.key = key;
    this.value = value;
  }
  public K getKey() {
    return this.key; } 
  public V getValue() { return this.value; } 
  public void setKey(K key) {
    this.key = key; } 
  public void setValue(V value) { this.value = value;
  }
}

推荐答案

.class文件中保留了一些通用类型信息-足以让您通过反射来确定Pair类型是通用类型,并且对于那些具有Pair成员,通用参数的类型等等.保留该信息的事实构成了超级类型令牌允许您传递类型标记,这些标记保留有关其通用参数的信息.

Some generic type information is retained in the .class file - enough to allow you to determine, via reflection, that the type Pair is generic, and for classes that have Pair members, what the types of the generic parameters are and so on. The fact that this information is retained forms the basis of techniques like super type tokens that allow you pass around type tokens which retain information about their generic parameters.

但是,除了反思之外,类型擦除意味着Pair类的行为将与Oracle网站"Answer"中的剥离版本大体上相同.特别是,方法的实际字节码将不会引用泛型类型KV-仅对象,并且方法签名将接受并返回Object.

Outside of reflection, however, type erasure means that the Pair class is mostly going to behave as in the stripped version in the "Answer" from the Oracle website. In particular, the actual bytecode for the methods won't have reference to generic types K or V - only Object, and the method signatures will accept and return Object.

在这种情况下,JD-GUI只是很聪明,并且使用存储在.class文件中的信息来还原反编译输出中的通用信息,但请确保将字节码中的类型删除.

In this case, JD-GUI is just being smart and using the information stored in the .class file to restore the generic information in the decompiled output, but rest assure the types are erased in the bytecode.

这篇关于Java-通用类型-类型擦除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆