Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

javac

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?

like image 316
Archie Avatar asked Oct 08 '15 18:10

Archie


1 Answers

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.

like image 72
ZhongYu Avatar answered Oct 23 '22 13:10

ZhongYu