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

Anonymous class with inaccessible type in constructor is rejected

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 1.4.0
    • 1.3.0
    • tools
    • beta
    • generic
    • generic

    Description

      When an anonymous class is declared, a constructor is created for it by
      the compiler. The compiler currently uses the signature of the superclass
      constructor as the signature of the anonymous class constructor.

      Difficulties arise when the superclass constructor signature refers to a class
      that is inaccessible in the package in which the anonymous class is defined.
      Consider the code below:

      P1/priv.java:

      package P1;

      class priv {

        public void foo() {};
      }


      p1/pub.java:

      package P1;

      public class pub extends priv {
      }


      P1/pubExposePriv.java:

      package P1;

      public class pubExposePriv extends priv {


        public priv baz() { return new priv();}
        public void bar(priv p){}
        public pubExposePriv(priv p) {}
      }

      P2/usePub.java

      package P2;

      class usePub extends P1.pub {

        void bar () {

        P1.pubExposePriv pd = new P1.pubExposePriv(new P1.pub());

        P1.pubExposePriv pe = new P1.pubExposePriv(new P1.pub()){};
        P1.pubExposePriv pf = new P1.pubExposePriv(null){};
        P1.pub p = new P1.pub();
        p.foo();
        this.foo();
        foo();
        }

        public static void main(String[] args) {

          (new usePub()).bar();
        }
      }

      javac rejects the following code with the message:

      usePub.java:9: P1.priv is not public in P1; cannot be accessed from outside package
        P1.pubExposePriv pe = new P1.pubExposePriv(new P1.pub()){};
                                                                ^
      usePub.java:10: P1.priv is not public in P1; cannot be accessed from outside package
        P1.pubExposePriv pf = new P1.pubExposePriv(null){};
                                                        ^
      2 errors

      This creates the absurd situation where the superclass constructor can be
      called, but the anonymous subclass is illegal. This is an artifact of the
      particular source-to-source translation used.

      The fact that an inaccessible type is mentioned in the synthesized
      constructor signature should cause no difficulty at the VM level. The
      program should be accepted.


      gilad.bracha@eng 2000-01-31

      Attachments

        Activity

          People

            gafter Neal Gafter
            gbrachasunw Gilad Bracha (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: