开发者

Are there any plans for the JVM to support generics at runtime?

You know, like the CLR does. Is anyone even admitting the lack of runtime gener开发者_如何学运维ic information is a problem, and working to solve it?


The designers of Java opted for this solution to maintain backward compatibility (on the bytecode level). Since then, there is even more Java code out there, thus breaking backward compatibility would have ever worse consequences. So I doubt they would change their minds about this.


Why is this a problem (from the comments to your question)

Well, consider for instance that can't overload a method like this:

void foo(List<String> strings) { ... }
void foo(List<Integer> ints) { ... }

even though it makes sense to actually allow this. (The main reason why this doesn't work today, is because it would, when compiled to bytecode, look like foo(List strings) and foo(List ints).)

Are there any plans for the JVM to support generics at runtime?

According to this page it's at least not in the pipe-line for Java 7:

Reified Generics

Currently, generics are implemented using erasure, which means that the generic type information is not available at runtime, which makes some kind of code hard to write. Generics were implemented this way to support backwards compatibility with older non-generic code. Reified generics would make the generic type information available at runtime, which would break legacy non-generic code. However, Neal Gafter has proposed making types reifiable only if specified, so as to not break backward compatibility.

Links

  • Neal Gafter's blog: Reified Generics for Java
  • Two problems with generics in Java


I believe someone (possibly Mark Reinhold) said at JavaOne this year that they may try to reify generics in Java in the future. This certainly wouldn't be any sooner than Java 9 though, and would be a huge change with a lot of potential issues to be worked out.


The problem can be partially solved without JVM changes: In Scala (running as well on the JVM) you can add so-called Manifests which hold run-time type information for generic parameters. I think this solution could be adapted for Java without too much trouble. It isn't perfect, but probably much easier to implement as "the real thing".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜