继承的方法Object.clone()无法隐藏公共抽象方法 [英] The inherited method Object.clone() cannot hide the public abstract method
问题描述
所以,我有一些疯狂的代码,这使编译器在我的脸上吐了几个小时,出现以下错误:
So, I have this bit of wild, crazy code, that is making the compiler spit in my face for some hours the following error:
The inherited method Object.clone() cannot hide the public abstract method in IOrderable<T>
罪魁祸首是以下类(错误出现在泛型的T中):
The culprit classes are the following (the error appears right in the T of the generic):
public class MyInterval<T extends Xpto & Successorable<T>> implements Cloneable {
public MyInterval<T> clone(){
MyInterval<T> it = null;
try {
it = (MyInterval<T>) super.clone();
it.max = it.max.clone();
it.min = (T) it.min.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return it;
}
}
public interface Xpto {}
public interface Successorable<Y> extends IOrderable<Y> {
Y suc();
}
interface IOrderable<J> extends Rankable<J>, Cloneable {
boolean greaterEq(J e);
J clone();
}
public interface Rankable<P> {
int rank(P e);
}
是的,它们似乎有点随机。它们只是为了测试我正在执行的类似于编译器/ java_byte_code_instrumentation的项目中的某些奇怪的事物而存在。
Yes, they seem kinda random. They solely exist to test some weird things in a compiler/java_byte_code_instrumentation-like project I am doing. How can I make this work while keeping the logic intact?
谢谢
推荐答案
我遇到了相同的编译错误,据我所知,这是交集类型(乘以绑定的泛型类型)的规范中的一个极端情况,并且没有解决方案。
I encountered the same compilation error, and as far as I can tell, this is an edge case in the specification for intersection types (multiply-bound generic types), and there is no solution possible.
一个小测试用例:
public interface InterfaceA {
public Object clone();
}
public interface InterfaceB {}
public class ClassA<T extends InterfaceA> {} // ok
public class ClassB<T extends InterfaceA & InterfaceB> {} //not ok
交集类型不能包含任何包含其方法的接口签名与基类上的非公共方法匹配(在这种情况下,隐式为Object)。
You can't have an intersection type containing any interfaces containing any method whose signature matches a non-public method on the base class (in this case, implicitly Object). This is not specific to Object or clone, however:
public abstract class Base {
protected abstract void m();
}
public interface Interface {
public void m();
}
public class Class<T extends Base & Interface> {} // not ok
这是相关的Oracle错误,标记为不是缺陷。
这是相关的Eclipse错误,其中修改了编译器以产生与Javac相匹配的相同错误。这似乎是Indigo 3.7的更改,它解释了为什么有些人可以复制而其他人不能复制。
Here's the relevant Eclipse bug, where the compiler was modified to produce the same error to match javac. This appears to have been a change for Indigo 3.7, which explains why some people could reproduce and others couldn't.
这篇关于继承的方法Object.clone()无法隐藏公共抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!