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

javac rejects to compile valid java code (private method not seen by compiler)

XMLWordPrintable

    • x86
    • linux_ubuntu

      FULL PRODUCT VERSION :
      java version "1.6.0_20"
      Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux pinguin 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:09:38 UTC 2010 x86_64 GNU/Linux

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      This bug is neither OS specific, nor java 1.6 specific, it can be reproduced on 1.5, 1.6 and 1.7

      A DESCRIPTION OF THE PROBLEM :
      Initially reported against ecj (Eclipse compiler for Java):
      https://bugs.eclipse.org/bugs/show_bug.cgi?id=325157

      It seems that javac has a bug and so rejects to compile valid java code.

      AS IS:
      The code below doesn't compile with javac (error is reported), but compiles with ecj.

      TO BE:
      in the example below javac should not produce any compiler error.

        Evaluation of the problem (taken from comment 1 of the bug above):

      The javac compiler seems to exhibit some interesting behavior:

      case (1) (same case as comment#0, but in one file)
      --------

      public class ConcreteClass
          extends AClass
      {
          private static String getText(String arg) {
              return arg;
          }
          static class StaticClass
                  extends ConcreteClass
          {
              public String getAbbreviatedText(int endIndex){
                  return getText("").substring(0, endIndex);
              }
          }
      }

      class AClass {
          public String getText() {
              return "";
          }
      }

      JDK5,6,7 refuse to compile this.

      => This seems to be a bug in javac. Examples below demonstrate other valid cases which are compilable with both javac and ecj.

      case (2)
      --------


      public class ConcreteClass
      // extends AClass // extends commented out.
      {
          private static String getText(String arg) {
              return arg;
          }
          static class StaticClass
                  extends ConcreteClass
          {
              public String getAbbreviatedText(int endIndex){
                  return getText("").substring(0, endIndex);
              }
          }
      }

      class AClass {
          public String getText() {
              return "";
          }
      }

      JDK5,6,7 compile this OK.

      case (3)
      --------

      public class ConcreteClass
          extends AClass
      {
          private static String getText(String arg) {
              return arg;
          }
          static class StaticClass
      // extends ConcreteClass // extends commented out
          {
              public String getAbbreviatedText(int endIndex){
                  return getText("").substring(0, endIndex);
              }
          }
      }

      class AClass {
          public String getText() {
              return "";
          }
      }

      JDK5,6,7, compile this OK.

      case (4)
      --------

      // AClass is totally out of the picture
      public class ConcreteClass
      {
          private static String getText(String arg) {
              return arg;
          }
          static class StaticClass
                  extends ConcreteClass
          {
              public String getAbbreviatedText(int endIndex){
                  return getText("").substring(0, endIndex);
              }
          }
      }


      JDK5,6,7 compile this OK.

      case (5)
      --------

      // private method changed to public.
      public class ConcreteClass extends AClass {
          public String getText(String arg) { // Note private changed to public
              return arg;
          }
          class StaticClass extends ConcreteClass {
              public String getAbbreviatedText(int endIndex){
                  return getText("").substring(0, endIndex);
              }
          }
      }

      class AClass {
          public String getText() {
              return "";
          }
      }

      compiles OK with JDK5,6,7.

      In summary, the method is accessible and applicable, so I don't see
      why the code should be rejected at all.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Code compiles without compiler errors.
      ACTUAL -
      Compiler error is reported.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      ConcreteClass.java:9: getText() in AClass cannot be applied
      to(java.lang.String)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class ConcreteClass
          extends AClass
      {
          private static String getText(String arg) {
              return arg;
          }
          static class StaticClass
                  extends ConcreteClass
          {
              public String getAbbreviatedText(int endIndex){
                  return getText("").substring(0, endIndex);
              }
          }
      }

      class AClass {
          public String getText() {
              return "";
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      There is no workaround except to rewrite the (valid) Java code.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: