Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8145978

Javac fails to compile valid program with intersection type containing clone()

XMLWordPrintable

    • x86_64
    • generic

      FULL PRODUCT VERSION :
      java version "1.7.0_45"
      Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux titan 2.6.34.10-26906-default #1 SMP 2012-05-28 16:10:01 +0200 x86_64 x86_64 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      The javac compiler rejects this valid Java program:

      public class xx {

          interface PublicCloneable {
              Object clone();
          }

          interface HasPosition {
              int getPosition();
              void setPosition(int p);
          }

          @SuppressWarnings("unchecked")
          <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
              final E clone = (E)elem.clone();
              clone.setPosition(clone.getPosition() + 1);
              return clone;
          }
      }

      Errors reported:

      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


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile this program:

      public class xx {

          interface PublicCloneable {
              Object clone();
          }

          interface HasPosition {
              int getPosition();
              void setPosition(int p);
          }

          @SuppressWarnings("unchecked")
          <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
              final E clone = (E)elem.clone();
              clone.setPosition(clone.getPosition() + 1);
              return clone;
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Program compiles successfully.
      ACTUAL -
      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


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      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


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class xx {

          interface PublicCloneable {
              Object clone();
          }

          interface HasPosition {
              int getPosition();
              void setPosition(int p);
          }

          @SuppressWarnings("unchecked")
          <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
              final E clone = (E)elem.clone();
              clone.setPosition(clone.getPosition() + 1);
              return clone;
          }
      }

      ---------- END SOURCE ----------

            dlsmith Dan Smith
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: