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

Reflection newInstance() error message when constructor has no access modifiers could use improvement

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Fixed
    • P4
    • 19
    • 19
    • core-libs
    • None

    Description

      I recently modified an empty class used by a test. Part of this modification was adding a default constructor. When the test called Class.newInstance() on this modified class, I got the following exception:

      java.lang.IllegalAccessException: class nsk.share.ObjectInstancesManager cannot access a member of class nsk.share.jdi.TestClass1 with modifiers ""
      at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:394)
      at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
      at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:492)
      at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
      at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:341)
      at java.base/java.lang.Class.newInstance(Class.java:677)
      at nsk.share.ObjectInstancesManager.createReferences(ObjectInstancesManager.java:183)
      at nsk.share.jdi.HeapwalkingDebuggee.parseCommand(HeapwalkingDebuggee.java:128)
      at nsk.share.jpda.AbstractDebuggeeTest.doTest(AbstractDebuggeeTest.java:316)
      at nsk.share.jpda.AbstractDebuggeeTest.doTest(AbstractDebuggeeTest.java:301)
      at nsk.jdi.ReferenceType.instances.instances002.instances002a.main(instances002a.java:37)

      Although I eventually figured out that the new constructor didn't have the needed access modifier, I mainly was able to figure that out fairly quickly because I had just added the constructor, not because the exception message made it clear. I can imaging having spent much more time figuring it out if I were calling newInstance() from new code and for a newly written class.

      The 'modifiers ""' part of the exception message should be more informative. In fact what would be best is if newInstance() could catch the IllegalAccessException and rethrow it with a message that specifically says that the constructor does not have the needed permissions.

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              cjplummer Chris Plummer
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: