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

Implementation of JEP 416: Reimplement Core Reflection with Method Handle

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 18
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      Code that inspects the internal generated reflection classes (i.e., subclasses of `MagicAccessorImpl`) will no longer work and must be updated.

      Method-handle invocation may consume more resources than the old core reflection implementation. Such invocation involves calling multiple Java methods to ensure that the declaring class of a member is initialized prior to access, and thus may require more stack space for the necessary execution frames. This may result in a `StackOverflowError` or, if a `StackOverflowError` is thrown while initializing a class, then a `NoClassDefFoundError`.
      Show
      Code that inspects the internal generated reflection classes (i.e., subclasses of `MagicAccessorImpl`) will no longer work and must be updated. Method-handle invocation may consume more resources than the old core reflection implementation. Such invocation involves calling multiple Java methods to ensure that the declaring class of a member is initialized prior to access, and thus may require more stack space for the necessary execution frames. This may result in a `StackOverflowError` or, if a `StackOverflowError` is thrown while initializing a class, then a `NoClassDefFoundError`.
    • System or security property
    • JDK

    Description

      Summary

      JEP 416: Reimplement Core Reflection with Method Handles

      Problem

      There are three different internal mechanisms for reflective operations:

      • VM native methods,
      • Dynamically generated bytecode stubs for Method::invoke and Constructor::newInstance, along with Unsafe field access for Field::get and set, and
      • Method handles.

      When we update java.lang.reflect and java.lang.invoke to support new language features, such as those envisioned in Project Valhalla, we must modify all three code paths, which is costly. In addition, the current implementation relies on special treatment by the VM of the generated bytecode, which is wrapped in subclasses of jdk.internal.reflect.MagicAccessorImpl:

      • Accessibility is relaxed so that these classes can access inaccessible fields and methods of other classes,
      • Verification is disabled to work around JLS ยง6.6.2 in order to support reflection on Object::clone, and
      • A non-well-behaved class loader is used to work around some security and compatibility issues.

      Solution

      Reimplement java.lang.reflect.Method, Constructor, and Field with java.lang.invoke method handles. Making method handles the underlying mechanism for reflection will reduce the maintenance and development cost of both the java.lang.reflect and java.lang.invoke APIs.

      Specification

      Code that depends upon highly implementation-specific and undocumented aspects of the existing implementation may be impacted. To mitigate this compatibility risk, as a workaround you can enable the old implementation via -Djdk.reflect.useDirectMethodHandle=false.

      The old core reflection implementation will be removed in a later release. The -Djdk.reflect.useDirectMethodHandle=false workaround will stop working at that point.

      Attachments

        Issue Links

          Activity

            People

              mchung Mandy Chung
              mchung Mandy Chung
              Alan Bateman
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: