简体   繁体   English

为什么Java Type Erasure没有阻止编译此代码

[英]Why didn't Java Type Erasure prevents this code from compiling

I have one class which has the following two methods defined: 我有一个类定义了以下两个方法:

public Map<String, Map<String, String>> method(final Map<String, Map<String, String>> data)

public boolean method(final Map<String, String> data)

Based on Java type erasure for generics, this code should not compile because they are all end up with: 基于Java类型擦除泛型,这段代码不应该编译,因为它们最终都是:

method(Map data)

However, this code was successfully compiled in Java 6, but did not compile in Java 8. 但是,此代码已在Java 6中成功编译,但未在Java 8中编译。

Can someone please let me know why it can be compiled under Java 6? 有人可以让我知道为什么它可以在Java 6下编译?

It compiles under Java 6, but not in Java 7 or Java 8. 它在Java 6下编译,但在Java 7或Java 8中编译。

There was a bug in Java 5 and Java 6 that was fixed in Java 7 (#6182950) . Java 5和Java 6中存在一个在Java 7修复的错误(#6182950)

That bug page refers to the JLS, Section 8.4.8.3 , which states: 该bug页面引用了JLS,第8.4.8.3节 ,其中规定:

It is a compile-time error if a type declaration T has a member method m1 and there exists a method m2 declared in T or a supertype of T such that all of the following are true: 如果类型声明T具有成员方法m1并且存在以T形式声明的方法m2或T的超类型使得以下所有条件都为真,则这是编译时错误:

  • m1 and m2 have the same name. m1和m2具有相同的名称。

  • m2 is accessible from T. m2可从T访问。

  • The signature of m1 is not a subsignature (§8.4.2) of the signature of m2. m1的签名不是m2签名的子签名(§8.4.2)。

  • The signature of m1 or some method m1 overrides (directly or indirectly) has the same erasure as the signature of m2 or some method m2 overrides (directly or indirectly). m1的签名或某些方法m1覆盖(直接或间接)具有与m2的签名相同的擦除或某种方法m2覆盖(直接或间接)。

Neither method has a subsignature of the other, because neither parameter type, Map<String, Map<String, String>> and Map<String, String> is a subtype of the other. 这两种方法都没有另一种方法的子签名,因为参数类型, Map<String, Map<String, String>>Map<String, String>都不是另一种的子类型。 But, they have the same erasure, Map . 但是,他们有相同的删除, Map

It never should have compiled, but that Java bug was fixed for Java 7. 它永远不应该编译,但Java 7修复了Java错误。

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

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