< prev index next >

src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java

Print this page




  32 import java.lang.reflect.Field;
  33 
  34 import static java.lang.invoke.MethodHandleNatives.Constants.*;
  35 import static java.lang.invoke.MethodHandleStatics.TRACE_METHOD_LINKAGE;
  36 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  37 
  38 /**
  39  * The JVM interface for the method handles package is all here.
  40  * This is an interface internal and private to an implementation of JSR 292.
  41  * <em>This class is not part of the JSR 292 standard.</em>
  42  * @author jrose
  43  */
  44 class MethodHandleNatives {
  45 
  46     private MethodHandleNatives() { } // static only
  47 
  48     /// MemberName support
  49 
  50     static native void init(MemberName self, Object ref);
  51     static native void expand(MemberName self);
  52     static native MemberName resolve(MemberName self, Class<?> caller) throws LinkageError, ClassNotFoundException;
  53     static native int getMembers(Class<?> defc, String matchName, String matchSig,
  54             int matchFlags, Class<?> caller, int skip, MemberName[] results);
  55 
  56     /// Field layout queries parallel to jdk.internal.misc.Unsafe:
  57     static native long objectFieldOffset(MemberName self);  // e.g., returns vmindex
  58     static native long staticFieldOffset(MemberName self);  // e.g., returns vmindex
  59     static native Object staticFieldBase(MemberName self);  // e.g., returns clazz
  60     static native Object getMemberVMInfo(MemberName self);  // returns {vmindex,vmtarget}
  61 
  62     /// CallSite support
  63 
  64     /** Tell the JVM that we need to change the target of a CallSite. */
  65     static native void setCallSiteTargetNormal(CallSite site, MethodHandle target);
  66     static native void setCallSiteTargetVolatile(CallSite site, MethodHandle target);
  67 
  68     /** Represents a context to track nmethod dependencies on CallSite instance target. */
  69     static class CallSiteContext implements Runnable {
  70         //@Injected JVM_nmethodBucket* vmdependencies;
  71 
  72         static CallSiteContext make(CallSite cs) {


 490     }
 491     static NoSuchMethodError newNoSuchMethodErrorOnVarHandle(String name, MethodType mtype) {
 492         return new NoSuchMethodError("VarHandle." + name + mtype);
 493     }
 494 
 495     /**
 496      * The JVM is resolving a CONSTANT_MethodHandle CP entry.  And it wants our help.
 497      * It will make an up-call to this method.  (Do not change the name or signature.)
 498      * The type argument is a Class for field requests and a MethodType for non-fields.
 499      * <p>
 500      * Recent versions of the JVM may also pass a resolved MemberName for the type.
 501      * In that case, the name is ignored and may be null.
 502      */
 503     static MethodHandle linkMethodHandleConstant(Class<?> callerClass, int refKind,
 504                                                  Class<?> defc, String name, Object type) {
 505         try {
 506             Lookup lookup = IMPL_LOOKUP.in(callerClass);
 507             assert(refKindIsValid(refKind));
 508             return lookup.linkMethodHandleConstant((byte) refKind, defc, name, type);
 509         } catch (IllegalAccessException ex) {


 510             Throwable cause = ex.getCause();
 511             if (cause instanceof AbstractMethodError) {
 512                 throw (AbstractMethodError) cause;
 513             } else {
 514                 Error err = new IllegalAccessError(ex.getMessage());
 515                 throw initCauseFrom(err, ex);
 516             }
 517         } catch (NoSuchMethodException ex) {
 518             Error err = new NoSuchMethodError(ex.getMessage());
 519             throw initCauseFrom(err, ex);
 520         } catch (NoSuchFieldException ex) {
 521             Error err = new NoSuchFieldError(ex.getMessage());
 522             throw initCauseFrom(err, ex);



 523         } catch (ReflectiveOperationException ex) {
 524             Error err = new IncompatibleClassChangeError();
 525             throw initCauseFrom(err, ex);
 526         }
 527     }
 528 
 529     /**
 530      * Use best possible cause for err.initCause(), substituting the
 531      * cause for err itself if the cause has the same (or better) type.
 532      */
 533     private static Error initCauseFrom(Error err, Exception ex) {
 534         Throwable th = ex.getCause();
 535         if (err.getClass().isInstance(th))
 536            return (Error) th;
 537         err.initCause(th == null ? ex : th);
 538         return err;
 539     }
 540 
 541     /**
 542      * Is this method a caller-sensitive method?




  32 import java.lang.reflect.Field;
  33 
  34 import static java.lang.invoke.MethodHandleNatives.Constants.*;
  35 import static java.lang.invoke.MethodHandleStatics.TRACE_METHOD_LINKAGE;
  36 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  37 
  38 /**
  39  * The JVM interface for the method handles package is all here.
  40  * This is an interface internal and private to an implementation of JSR 292.
  41  * <em>This class is not part of the JSR 292 standard.</em>
  42  * @author jrose
  43  */
  44 class MethodHandleNatives {
  45 
  46     private MethodHandleNatives() { } // static only
  47 
  48     /// MemberName support
  49 
  50     static native void init(MemberName self, Object ref);
  51     static native void expand(MemberName self);
  52     static native MemberName resolve(MemberName self, Class<?> caller) throws LinkageError;
  53     static native int getMembers(Class<?> defc, String matchName, String matchSig,
  54             int matchFlags, Class<?> caller, int skip, MemberName[] results);
  55 
  56     /// Field layout queries parallel to jdk.internal.misc.Unsafe:
  57     static native long objectFieldOffset(MemberName self);  // e.g., returns vmindex
  58     static native long staticFieldOffset(MemberName self);  // e.g., returns vmindex
  59     static native Object staticFieldBase(MemberName self);  // e.g., returns clazz
  60     static native Object getMemberVMInfo(MemberName self);  // returns {vmindex,vmtarget}
  61 
  62     /// CallSite support
  63 
  64     /** Tell the JVM that we need to change the target of a CallSite. */
  65     static native void setCallSiteTargetNormal(CallSite site, MethodHandle target);
  66     static native void setCallSiteTargetVolatile(CallSite site, MethodHandle target);
  67 
  68     /** Represents a context to track nmethod dependencies on CallSite instance target. */
  69     static class CallSiteContext implements Runnable {
  70         //@Injected JVM_nmethodBucket* vmdependencies;
  71 
  72         static CallSiteContext make(CallSite cs) {


 490     }
 491     static NoSuchMethodError newNoSuchMethodErrorOnVarHandle(String name, MethodType mtype) {
 492         return new NoSuchMethodError("VarHandle." + name + mtype);
 493     }
 494 
 495     /**
 496      * The JVM is resolving a CONSTANT_MethodHandle CP entry.  And it wants our help.
 497      * It will make an up-call to this method.  (Do not change the name or signature.)
 498      * The type argument is a Class for field requests and a MethodType for non-fields.
 499      * <p>
 500      * Recent versions of the JVM may also pass a resolved MemberName for the type.
 501      * In that case, the name is ignored and may be null.
 502      */
 503     static MethodHandle linkMethodHandleConstant(Class<?> callerClass, int refKind,
 504                                                  Class<?> defc, String name, Object type) {
 505         try {
 506             Lookup lookup = IMPL_LOOKUP.in(callerClass);
 507             assert(refKindIsValid(refKind));
 508             return lookup.linkMethodHandleConstant((byte) refKind, defc, name, type);
 509         } catch (IllegalAccessException ex) {
 510             // MemberName.makeAccessException() wraps most of LinkageErrors reported by Factory.resolve() in IAE
 511             // when resolution fails. Try to recover original error and rethrow it.
 512             Throwable cause = ex.getCause();
 513             if (cause instanceof LinkageError) {
 514                 throw (LinkageError) cause;
 515             } else {
 516                 Error err = new IllegalAccessError(ex.getMessage());
 517                 throw initCauseFrom(err, ex);
 518             }
 519         } catch (NoSuchMethodException ex) {
 520             Error err = new NoSuchMethodError(ex.getMessage());
 521             throw initCauseFrom(err, ex);
 522         } catch (NoSuchFieldException ex) {
 523             Error err = new NoSuchFieldError(ex.getMessage());
 524             throw initCauseFrom(err, ex);
 525         } catch (ClassNotFoundException ex) {
 526             Error err = new NoClassDefFoundError(ex.getMessage());
 527             throw initCauseFrom(err, ex);
 528         } catch (ReflectiveOperationException ex) {
 529             Error err = new IncompatibleClassChangeError();
 530             throw initCauseFrom(err, ex);
 531         }
 532     }
 533 
 534     /**
 535      * Use best possible cause for err.initCause(), substituting the
 536      * cause for err itself if the cause has the same (or better) type.
 537      */
 538     private static Error initCauseFrom(Error err, Exception ex) {
 539         Throwable th = ex.getCause();
 540         if (err.getClass().isInstance(th))
 541            return (Error) th;
 542         err.initCause(th == null ? ex : th);
 543         return err;
 544     }
 545 
 546     /**
 547      * Is this method a caller-sensitive method?


< prev index next >