does Java type erasure erase my generic type?

前端 未结 5 1564
执念已碎
执念已碎 2020-12-25 07:53

I\'ve thought java erasure wipes generic types out in compile time however when i test it by myself i realized there are some information about generic types in Bytecode.

5条回答
  •  旧时难觅i
    2020-12-25 08:56

    Some Generic type information is stored in Signature attributes . Refer JLS 4.8 and 4.6 and JVM spec 4.3.4. Read here:

    Probably the most common complaint about generics in Java is that they are not reified - there is not a way to know at runtime that a List is any different from a List. I've gotten so used to this that I was quite surprised to run across Neil Gafter's work on Super Type Tokens. It turns out that while the JVM will not track the actual type arguments for instances of a generic class, it does track the actual type arguments for subclasses of generic classes. In other words, while a new ArrayList() is really just a new ArrayList() at runtime, if a class extends ArrayList, then the JVM knows that String is the actual type argument for List's type parameter.

    and Neal Gafter's blog.

提交回复
热议问题