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

Hiding of inner classes not resolved properly

    XMLWordPrintable

Details

    • b23
    • generic
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      If an inner class has the same name as an inner class of the super class, the super class' inner class is hidden.
      And calls of the sub-class' inner class are resolved properly in the JRE.

      This does not work with the nashorn ScriptEngine. Calls to the sub-class' inner class are not resolved properly and the super class' inner class is used.
      This leads to wrong results or errors because of missing functions.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      see source code.
      Called is the following script:
      Packages.BuggyNashorn.B.Inner.print()

      B has an static inner class "Inner", and its super class A also has an static inner class "Inner".
      B.Inner may or may not extend A.Inner.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Call of B.Inner.print()
      ACTUAL -
      Call of A.Inner.print()

      ---------- BEGIN SOURCE ----------
      import javax.script.Bindings;
      import javax.script.ScriptContext;
      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;

      public class BuggyNashorn
      {
        public static void main(String[] args)
      throws Exception
        {
          ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
          Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);

          System.out.println("Description\t\t\tJava\t<==> Nashorn");

          System.out.print("Correct (no hiding needed):\t");
          System.out.println(A.Inner.print() + "\t<==> " + engine.eval("Packages.BuggyNashorn.A.Inner.print();", bindings));
          System.out.print("Error (not hidden):\t\t");
          System.out.println(B.Inner.print() + "\t<==> " + engine.eval("Packages.BuggyNashorn.B.Inner.print();", bindings));
          System.out.print("Workaround (now hidden):\t");
          System.out.println(B.Inner.print() + "\t<==> " + engine.eval("Java.type('BuggyNashorn.B.Inner').print();", bindings));
        }

        public static class A
        {
          public static class Inner
          {
            public static String print() { return "A.Inner"; }
          }
        }

        public static class B extends A
        {
          public static class Inner extends A.Inner
          {
            public static String print() { return "B.Inner"; }
          }
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      see source code.
      The inner class is properly resolved when using Java.type("BuggyNashorn.B.Inner")

      FREQUENCY : always


      Attachments

        Activity

          People

            hannesw Hannes Wallnoefer
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: