unable to use an intersection type when notional class requires access modification

半世苍凉 提交于 2019-12-12 08:49:31

问题


Interfaces:

interface PublicCloneable {
    Object clone();
}

interface HasPosition {
    // doesn't matter
}

Attempt to use intersection type:

@SuppressWarnings("unchecked")
<E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
    final E clone = (E)elem.clone();
    // rest omitted
}

Attempt to compile with javac 1.8.0_60:

$ javac xx.java
xx.java:13: error: clone() in Object cannot implement clone() in PublicCloneable
    <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
     ^
  attempting to assign weaker access privileges; was public
xx.java:14: error: clone() has protected access in Object
        final E clone = (E)elem.clone();
                               ^
2 errors

Why this intersection type is invalid for javac?


回答1:


This looks like a javac bug.

http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.4

The members of a type variable X with bound T & I1 & ... & In are the members of the intersection type (§4.9) T & I1 & ... & In

http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.9

Every intersection type T1 & ... & Tn induces a notional class or interface for the purpose of identifying the members of the intersection type ...

If Ck is Object, a notional interface is induced ... has direct superinterfaces T1', ..., Tn'

Therefore, for PublicCloneable & HasPosition, a notional interface is introduced, extending both of them, which should be OK.




回答2:


As the first error is trying to tell you, your code cannot work, because you're constraining to types with two incompatible signatures for the clone() method.



来源:https://stackoverflow.com/questions/33023332/unable-to-use-an-intersection-type-when-notional-class-requires-access-modificat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!