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

Missing default constructor initializing a newInstance

XMLWordPrintable

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 9+181)
      Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      If a class assumes an implicit default constructor, creating a newInstance of an object throws a NoSuchMethodException shown below. If a default constructor is provided explicitly, everything works fine.

       Exception in thread "main" java.lang.NoSuchMethodException: B.<init>()
              at java.base/java.lang.Class.getConstructor0(Class.java:3322)
              at java.base/java.lang.Class.getConstructor(Class.java:2108)
              at NewInstanceBug.main(NewInstanceBug.java:29)


      REGRESSION. Last worked in version 8u144

      ADDITIONAL REGRESSION INFORMATION:
      java version "9"
      Java(TM) SE Runtime Environment (build 9+181)
      Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Cut and paste the provided code in a file called NewInstanceBug.java
      Compile it: javac NewInstanceBug.java
      Run it: java NewInstanceBug

      To correct the problem, uncomment the default constructor in class B and recompile and run. This is the simplistic case.

      An interface is also provided. A similar test can be constructed showing this case also fails when assuming an implicit constructor.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Output:

      z2.name(): B
      z2: B


      ACTUAL -
      Exception in thread "main" java.lang.NoSuchMethodException: B.<init>()
              at java.base/java.lang.Class.getConstructor0(Class.java:3322)
              at java.base/java.lang.Class.getConstructor(Class.java:2108)
              at NewInstanceBug.main(NewInstanceBug.java:30)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      interface A {

          String name();

      }

      //class B implements A {
      class B {

      /*
          public B() {
          }
      */

          public String name() {
              return getClass().getName();
          }

          public String toString() {
              return name();
          }

      }

      public class NewInstanceBug {

          public static void main(String[] args) throws Exception {
              //A z1 = Class.forName("B").asSubclass(A.class).getConstructor().newInstance();
              B z2 = Class.forName("B").asSubclass(B.class).getConstructor().newInstance();

              //System.err.println("z1.name(): "+z1.name());
              //System.err.println("z1: "+z1);
              System.err.println("z2.name(): "+z2.name());
              System.err.println("z2: "+z2);
          }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Workaround would be to include a default constructor which is what is trying to be avoided.

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: