JDK-8246634 : Consider Lookup::ensureInitialized(Class target, MethodHandle mh)
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2020-06-05
  • Updated: 2023-10-21
Related Reports
Relates :  
Description
See https://mail.openjdk.java.net/pipermail/core-libs-dev/2020-June/066991.html and
https://mail.openjdk.java.net/pipermail/core-libs-dev/2020-June/066994.html

/**
 * To the given method handle, prepend an action, if necessary, to trigger
 * initialization of the given class the first time the method handle is called.
 * If it would be illegal to call ensureInitialized on the given class from this lookup,
 * immediately report an error.  Otherwise, if the class is already fully initialized
 * return the method handle unchanged.  Otherwise, return a new method handle
 * (of the same type) which incorporates the necessary initialization action,
 * as if by a call to ensureInitialized.
 */
MethodHandle ensureInitialized(Class target, MethodHandle mh) {
  checkAccess(target);
  if (isFullyInitialized(target))   return mh;
  // we need to tack a “before action” onto mh to trigger inits:
 MethodHandle before = MH_ensureInit.bindTo(target);
 return collectArguments(mh, 0, before);
}