/* * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.lang.invoke; import java.lang.invoke.MethodHandles.Lookup; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import static java.lang.invoke.MethodHandleNatives.Constants.*; import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; /** * The JVM interface for the method handles package is all here. * This is an interface internal and private to an implemetantion of JSR 292. * This class is not part of the JSR 292 standard. * @author jrose */ class MethodHandleNatives { private MethodHandleNatives() { } // static only /// MethodName support static native void init(MemberName self, Object ref); static native void expand(MemberName self); static native void resolve(MemberName self, Class caller); static native int getMembers(Class defc, String matchName, String matchSig, int matchFlags, Class caller, int skip, MemberName[] results); /// MethodHandle support /** Initialize the method handle to adapt the call. */ static native void init(AdapterMethodHandle self, MethodHandle target, int argnum); /** Initialize the method handle to call the correct method, directly. */ static native void init(BoundMethodHandle self, Object target, int argnum); /** Initialize the method handle to call as if by an invoke* instruction. */ static native void init(DirectMethodHandle self, Object ref, boolean doDispatch, Class caller); /** Initialize a method type, once per form. */ static native void init(MethodType self); /** Tell the JVM about a class's bootstrap method. */ static native void registerBootstrap(Class caller, MethodHandle bootstrapMethod); /** Ask the JVM about a class's bootstrap method. */ static native MethodHandle getBootstrap(Class caller); /** Tell the JVM that we need to change the target of an invokedynamic. */ static native void setCallSiteTarget(CallSite site, MethodHandle target); /** Fetch the vmtarget field. * It will be sanitized as necessary to avoid exposing non-Java references. * This routine is for debugging and reflection. */ static native Object getTarget(MethodHandle self, int format); /** Fetch the name of the handled method, if available. * This routine is for debugging and reflection. */ static MemberName getMethodName(MethodHandle self) { return (MemberName) getTarget(self, ETF_METHOD_NAME); } /** Fetch the reflective version of the handled method, if available. */ static AccessibleObject getTargetMethod(MethodHandle self) { return (AccessibleObject) getTarget(self, ETF_REFLECT_METHOD); } /** Fetch the target of this method handle. * If it directly targets a method, return a MemberName for the method. * If it is chained to another method handle, return that handle. */ static Object getTargetInfo(MethodHandle self) { return getTarget(self, ETF_HANDLE_OR_METHOD_NAME); } static Object[] makeTarget(Class defc, String name, String sig, int mods, Class refc) { return new Object[] { defc, name, sig, mods, refc }; } /** Fetch MH-related JVM parameter. * which=0 retrieves MethodHandlePushLimit * which=1 retrieves stack slot push size (in address units) */ static native int getConstant(int which); /** Java copy of MethodHandlePushLimit in range 2..255. */ static final int JVM_PUSH_LIMIT; /** JVM stack motion (in words) after one slot is pushed, usually -1. */ static final int JVM_STACK_MOVE_UNIT; /** Which conv-ops are implemented by the JVM? */ static final int CONV_OP_IMPLEMENTED_MASK; private static native void registerNatives(); static { int JVM_PUSH_LIMIT_; int JVM_STACK_MOVE_UNIT_; int CONV_OP_IMPLEMENTED_MASK_; try { registerNatives(); JVM_PUSH_LIMIT_ = getConstant(Constants.GC_JVM_PUSH_LIMIT); JVM_STACK_MOVE_UNIT_ = getConstant(Constants.GC_JVM_STACK_MOVE_UNIT); CONV_OP_IMPLEMENTED_MASK_ = getConstant(Constants.GC_CONV_OP_IMPLEMENTED_MASK); //sun.reflect.Reflection.registerMethodsToFilter(MethodHandleImpl.class, "init"); } catch (UnsatisfiedLinkError ee) { // ignore; if we use init() methods later we'll see linkage errors JVM_PUSH_LIMIT_ = 3; // arbitrary JVM_STACK_MOVE_UNIT_ = -1; // arbitrary CONV_OP_IMPLEMENTED_MASK_ = 0; JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_; JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_; throw ee; // just die; hopeless to try to run with an older JVM } JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_; JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_; if (CONV_OP_IMPLEMENTED_MASK_ == 0) CONV_OP_IMPLEMENTED_MASK_ = DEFAULT_CONV_OP_IMPLEMENTED_MASK; CONV_OP_IMPLEMENTED_MASK = CONV_OP_IMPLEMENTED_MASK_; } // All compile-time constants go here. // There is an opportunity to check them against the JVM's idea of them. static class Constants { Constants() { } // static only // MethodHandleImpl static final int // for getConstant GC_JVM_PUSH_LIMIT = 0, GC_JVM_STACK_MOVE_UNIT = 1, GC_CONV_OP_IMPLEMENTED_MASK = 2; static final int ETF_HANDLE_OR_METHOD_NAME = 0, // all available data (immediate MH or method) ETF_DIRECT_HANDLE = 1, // ultimate method handle (will be a DMH, may be self) ETF_METHOD_NAME = 2, // ultimate method as MemberName ETF_REFLECT_METHOD = 3; // ultimate method as java.lang.reflect object (sans refClass) // MemberName // The JVM uses values of -2 and above for vtable indexes. // Field values are simple positive offsets. // Ref: src/share/vm/oops/methodOop.hpp // This value is negative enough to avoid such numbers, // but not too negative. static final int MN_IS_METHOD = 0x00010000, // method (not constructor) MN_IS_CONSTRUCTOR = 0x00020000, // constructor MN_IS_FIELD = 0x00040000, // field MN_IS_TYPE = 0x00080000, // nested type MN_SEARCH_SUPERCLASSES = 0x00100000, // for MHN.getMembers MN_SEARCH_INTERFACES = 0x00200000, // for MHN.getMembers VM_INDEX_UNINITIALIZED = -99; // BoundMethodHandle /** Constants for decoding the vmargslot field, which contains 2 values. */ static final int ARG_SLOT_PUSH_SHIFT = 16, ARG_SLOT_MASK = (1< rtype, Class[] ptypes) { return MethodType.makeImpl(rtype, ptypes, true); } /** * The JVM wants to use a MethodType with invokeGeneric. Give the runtime fair warning. */ static void notifyGenericMethodType(MethodType type) { type.form().notifyGenericMethodType(); } /** * The JVM wants to raise an exception. Here's the path. */ static void raiseException(int code, Object actual, Object required) { String message; // disregard the identity of the actual object, if it is not a class: if (!(actual instanceof Class) && !(actual instanceof MethodType)) actual = actual.getClass(); if (actual != null) message = "required "+required+" but encountered "+actual; else message = "required "+required; switch (code) { case 192: // checkcast throw new ClassCastException(message); default: throw new InternalError("unexpected code "+code+": "+message); } } /** * The JVM is resolving a CONSTANT_MethodHandle CP entry. And it wants our help. * It will make an up-call to this method. (Do not change the name or signature.) */ static MethodHandle linkMethodHandleConstant(Class callerClass, int refKind, Class defc, String name, Object type) { try { Lookup lookup = IMPL_LOOKUP.in(callerClass); switch (refKind) { case REF_getField: return lookup.findGetter( defc, name, (Class) type ); case REF_getStatic: return lookup.findStaticGetter( defc, name, (Class) type ); case REF_putField: return lookup.findSetter( defc, name, (Class) type ); case REF_putStatic: return lookup.findStaticSetter( defc, name, (Class) type ); case REF_invokeVirtual: return lookup.findVirtual( defc, name, (MethodType) type ); case REF_invokeStatic: return lookup.findStatic( defc, name, (MethodType) type ); case REF_invokeSpecial: return lookup.findSpecial( defc, name, (MethodType) type, callerClass ); case REF_newInvokeSpecial: return lookup.findConstructor( defc, (MethodType) type ); case REF_invokeInterface: return lookup.findVirtual( defc, name, (MethodType) type ); } throw new IllegalArgumentException("bad MethodHandle constant "+name+" : "+type); } catch (ReflectiveOperationException ex) { Error err = new IncompatibleClassChangeError(); err.initCause(ex); throw err; } } }