2009-05-05 22:40:09 -07:00
|
|
|
/*
|
2012-07-24 10:47:44 -07:00
|
|
|
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
|
2009-05-05 22:40:09 -07:00
|
|
|
* 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
|
2010-05-25 15:58:33 -07:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2009-05-05 22:40:09 -07:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 15:58:33 -07:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2009-05-05 22:40:09 -07:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* 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.
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
|
2011-03-23 23:02:31 -07:00
|
|
|
package java.lang.invoke;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2011-03-23 23:02:31 -07:00
|
|
|
import java.lang.invoke.MethodHandles.Lookup;
|
2009-05-05 22:40:09 -07:00
|
|
|
import java.lang.reflect.AccessibleObject;
|
|
|
|
import java.lang.reflect.Field;
|
2011-03-23 23:02:31 -07:00
|
|
|
import static java.lang.invoke.MethodHandleNatives.Constants.*;
|
2012-07-24 10:47:44 -07:00
|
|
|
import static java.lang.invoke.MethodHandleStatics.*;
|
2011-03-23 23:02:31 -07:00
|
|
|
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The JVM interface for the method handles package is all here.
|
2010-04-30 23:48:23 -07:00
|
|
|
* This is an interface internal and private to an implemetantion of JSR 292.
|
|
|
|
* <em>This class is not part of the JSR 292 standard.</em>
|
2009-05-05 22:40:09 -07:00
|
|
|
* @author jrose
|
|
|
|
*/
|
|
|
|
class MethodHandleNatives {
|
|
|
|
|
|
|
|
private MethodHandleNatives() { } // static only
|
|
|
|
|
2012-07-24 10:47:44 -07:00
|
|
|
/// MemberName support
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
static native void init(MemberName self, Object ref);
|
|
|
|
static native void expand(MemberName self);
|
2012-07-24 10:47:44 -07:00
|
|
|
static native MemberName resolve(MemberName self, Class<?> caller) throws LinkageError;
|
2009-05-05 22:40:09 -07:00
|
|
|
static native int getMembers(Class<?> defc, String matchName, String matchSig,
|
|
|
|
int matchFlags, Class<?> caller, int skip, MemberName[] results);
|
|
|
|
|
2012-07-24 10:47:44 -07:00
|
|
|
/// Field layout queries parallel to sun.misc.Unsafe:
|
|
|
|
static native long objectFieldOffset(MemberName self); // e.g., returns vmindex
|
|
|
|
static native long staticFieldOffset(MemberName self); // e.g., returns vmindex
|
|
|
|
static native Object staticFieldBase(MemberName self); // e.g., returns clazz
|
|
|
|
static native Object getMemberVMInfo(MemberName self); // returns {vmindex,vmtarget}
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2012-07-24 10:47:44 -07:00
|
|
|
/// MethodHandle support
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
2011-09-07 21:05:24 -07:00
|
|
|
static final boolean COUNT_GWT;
|
|
|
|
|
2011-11-02 02:03:30 -07:00
|
|
|
/// CallSite support
|
|
|
|
|
|
|
|
/** Tell the JVM that we need to change the target of a CallSite. */
|
|
|
|
static native void setCallSiteTargetNormal(CallSite site, MethodHandle target);
|
|
|
|
static native void setCallSiteTargetVolatile(CallSite site, MethodHandle target);
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
private static native void registerNatives();
|
|
|
|
static {
|
2011-06-14 22:47:09 -07:00
|
|
|
registerNatives();
|
2011-09-07 21:05:24 -07:00
|
|
|
COUNT_GWT = getConstant(Constants.GC_COUNT_GWT) != 0;
|
2012-07-24 10:47:44 -07:00
|
|
|
|
|
|
|
// The JVM calls MethodHandleNatives.<clinit>. Cascade the <clinit> calls as needed:
|
|
|
|
MethodHandleImpl.initStatics();
|
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
// 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
|
2012-07-24 10:47:44 -07:00
|
|
|
GC_COUNT_GWT = 4,
|
|
|
|
GC_LAMBDA_SUPPORT = 5;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
// 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
|
2012-07-24 10:47:44 -07:00
|
|
|
MN_REFERENCE_KIND_SHIFT = 24, // refKind
|
|
|
|
MN_REFERENCE_KIND_MASK = 0x0F000000 >> MN_REFERENCE_KIND_SHIFT,
|
|
|
|
// The SEARCH_* bits are not for MN.flags but for the matchFlags argument of MHN.getMembers:
|
|
|
|
MN_SEARCH_SUPERCLASSES = 0x00100000,
|
|
|
|
MN_SEARCH_INTERFACES = 0x00200000;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic types as encoded in the JVM. These code values are not
|
|
|
|
* intended for use outside this class. They are used as part of
|
|
|
|
* a private interface between the JVM and this class.
|
|
|
|
*/
|
|
|
|
static final int
|
|
|
|
T_BOOLEAN = 4,
|
|
|
|
T_CHAR = 5,
|
|
|
|
T_FLOAT = 6,
|
|
|
|
T_DOUBLE = 7,
|
|
|
|
T_BYTE = 8,
|
|
|
|
T_SHORT = 9,
|
|
|
|
T_INT = 10,
|
|
|
|
T_LONG = 11,
|
|
|
|
T_OBJECT = 12,
|
|
|
|
//T_ARRAY = 13
|
2011-05-12 19:27:49 -07:00
|
|
|
T_VOID = 14,
|
2009-05-05 22:40:09 -07:00
|
|
|
//T_ADDRESS = 15
|
2011-05-12 19:27:49 -07:00
|
|
|
T_ILLEGAL = 99;
|
2010-06-08 23:08:56 -07:00
|
|
|
|
2012-07-24 10:47:44 -07:00
|
|
|
/**
|
|
|
|
* Constant pool entry types.
|
|
|
|
*/
|
|
|
|
static final byte
|
|
|
|
CONSTANT_Utf8 = 1,
|
|
|
|
CONSTANT_Integer = 3,
|
|
|
|
CONSTANT_Float = 4,
|
|
|
|
CONSTANT_Long = 5,
|
|
|
|
CONSTANT_Double = 6,
|
|
|
|
CONSTANT_Class = 7,
|
|
|
|
CONSTANT_String = 8,
|
|
|
|
CONSTANT_Fieldref = 9,
|
|
|
|
CONSTANT_Methodref = 10,
|
|
|
|
CONSTANT_InterfaceMethodref = 11,
|
|
|
|
CONSTANT_NameAndType = 12,
|
|
|
|
CONSTANT_MethodHandle = 15, // JSR 292
|
|
|
|
CONSTANT_MethodType = 16, // JSR 292
|
|
|
|
CONSTANT_InvokeDynamic = 18,
|
|
|
|
CONSTANT_LIMIT = 19; // Limit to tags found in classfiles
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access modifier flags.
|
|
|
|
*/
|
|
|
|
static final char
|
|
|
|
ACC_PUBLIC = 0x0001,
|
|
|
|
ACC_PRIVATE = 0x0002,
|
|
|
|
ACC_PROTECTED = 0x0004,
|
|
|
|
ACC_STATIC = 0x0008,
|
|
|
|
ACC_FINAL = 0x0010,
|
|
|
|
ACC_SYNCHRONIZED = 0x0020,
|
|
|
|
ACC_VOLATILE = 0x0040,
|
|
|
|
ACC_TRANSIENT = 0x0080,
|
|
|
|
ACC_NATIVE = 0x0100,
|
|
|
|
ACC_INTERFACE = 0x0200,
|
|
|
|
ACC_ABSTRACT = 0x0400,
|
|
|
|
ACC_STRICT = 0x0800,
|
|
|
|
ACC_SYNTHETIC = 0x1000,
|
|
|
|
ACC_ANNOTATION = 0x2000,
|
|
|
|
ACC_ENUM = 0x4000,
|
|
|
|
// aliases:
|
|
|
|
ACC_SUPER = ACC_SYNCHRONIZED,
|
|
|
|
ACC_BRIDGE = ACC_VOLATILE,
|
|
|
|
ACC_VARARGS = ACC_TRANSIENT;
|
|
|
|
|
2010-06-08 23:08:56 -07:00
|
|
|
/**
|
|
|
|
* Constant pool reference-kind codes, as used by CONSTANT_MethodHandle CP entries.
|
|
|
|
*/
|
2012-07-24 10:47:44 -07:00
|
|
|
static final byte
|
|
|
|
REF_NONE = 0, // null value
|
2010-06-08 23:08:56 -07:00
|
|
|
REF_getField = 1,
|
|
|
|
REF_getStatic = 2,
|
|
|
|
REF_putField = 3,
|
|
|
|
REF_putStatic = 4,
|
|
|
|
REF_invokeVirtual = 5,
|
|
|
|
REF_invokeStatic = 6,
|
|
|
|
REF_invokeSpecial = 7,
|
|
|
|
REF_newInvokeSpecial = 8,
|
2012-07-24 10:47:44 -07:00
|
|
|
REF_invokeInterface = 9,
|
|
|
|
REF_LIMIT = 10;
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2012-07-24 10:47:44 -07:00
|
|
|
static boolean refKindIsValid(int refKind) {
|
|
|
|
return (refKind > REF_NONE && refKind < REF_LIMIT);
|
|
|
|
}
|
|
|
|
static boolean refKindIsField(byte refKind) {
|
|
|
|
assert(refKindIsValid(refKind));
|
|
|
|
return (refKind <= REF_putStatic);
|
|
|
|
}
|
|
|
|
static boolean refKindIsGetter(byte refKind) {
|
|
|
|
assert(refKindIsValid(refKind));
|
|
|
|
return (refKind <= REF_getStatic);
|
|
|
|
}
|
|
|
|
static boolean refKindIsSetter(byte refKind) {
|
|
|
|
return refKindIsField(refKind) && !refKindIsGetter(refKind);
|
|
|
|
}
|
|
|
|
static boolean refKindIsMethod(byte refKind) {
|
|
|
|
return !refKindIsField(refKind) && (refKind != REF_newInvokeSpecial);
|
|
|
|
}
|
|
|
|
static boolean refKindHasReceiver(byte refKind) {
|
|
|
|
assert(refKindIsValid(refKind));
|
|
|
|
return (refKind & 1) != 0;
|
|
|
|
}
|
|
|
|
static boolean refKindIsStatic(byte refKind) {
|
|
|
|
return !refKindHasReceiver(refKind) && (refKind != REF_newInvokeSpecial);
|
|
|
|
}
|
|
|
|
static boolean refKindDoesDispatch(byte refKind) {
|
|
|
|
assert(refKindIsValid(refKind));
|
|
|
|
return (refKind == REF_invokeVirtual ||
|
|
|
|
refKind == REF_invokeInterface);
|
|
|
|
}
|
|
|
|
static {
|
|
|
|
final int HR_MASK = ((1 << REF_getField) |
|
|
|
|
(1 << REF_putField) |
|
|
|
|
(1 << REF_invokeVirtual) |
|
|
|
|
(1 << REF_invokeSpecial) |
|
|
|
|
(1 << REF_invokeInterface)
|
|
|
|
);
|
|
|
|
for (byte refKind = REF_NONE+1; refKind < REF_LIMIT; refKind++) {
|
|
|
|
assert(refKindHasReceiver(refKind) == (((1<<refKind) & HR_MASK) != 0)) : refKind;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static String refKindName(byte refKind) {
|
|
|
|
assert(refKindIsValid(refKind));
|
|
|
|
return REFERENCE_KIND_NAME[refKind];
|
|
|
|
}
|
|
|
|
private static String[] REFERENCE_KIND_NAME = {
|
|
|
|
null,
|
|
|
|
"getField",
|
|
|
|
"getStatic",
|
|
|
|
"putField",
|
|
|
|
"putStatic",
|
|
|
|
"invokeVirtual",
|
|
|
|
"invokeStatic",
|
|
|
|
"invokeSpecial",
|
|
|
|
"newInvokeSpecial",
|
|
|
|
"invokeInterface"
|
|
|
|
};
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
private static native int getNamedCon(int which, Object[] name);
|
|
|
|
static boolean verifyConstants() {
|
|
|
|
Object[] box = { null };
|
|
|
|
for (int i = 0; ; i++) {
|
|
|
|
box[0] = null;
|
|
|
|
int vmval = getNamedCon(i, box);
|
|
|
|
if (box[0] == null) break;
|
|
|
|
String name = (String) box[0];
|
|
|
|
try {
|
|
|
|
Field con = Constants.class.getDeclaredField(name);
|
|
|
|
int jval = con.getInt(null);
|
2011-05-12 19:27:49 -07:00
|
|
|
if (jval == vmval) continue;
|
|
|
|
String err = (name+": JVM has "+vmval+" while Java has "+jval);
|
|
|
|
if (name.equals("CONV_OP_LIMIT")) {
|
|
|
|
System.err.println("warning: "+err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw new InternalError(err);
|
2012-07-24 10:47:44 -07:00
|
|
|
} catch (NoSuchFieldException | IllegalAccessException ex) {
|
|
|
|
String err = (name+": JVM has "+vmval+" which Java does not define");
|
|
|
|
// ignore exotic ops the JVM cares about; we just wont issue them
|
|
|
|
//System.err.println("warning: "+err);
|
|
|
|
continue;
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static {
|
2011-05-12 19:27:49 -07:00
|
|
|
assert(verifyConstants());
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
2010-04-30 23:48:23 -07:00
|
|
|
|
|
|
|
// Up-calls from the JVM.
|
|
|
|
// These must NOT be public.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The JVM is linking an invokedynamic instruction. Create a reified call site for it.
|
|
|
|
*/
|
2012-07-24 10:47:44 -07:00
|
|
|
static MemberName linkCallSite(Object callerObj,
|
|
|
|
Object bootstrapMethodObj,
|
|
|
|
Object nameObj, Object typeObj,
|
|
|
|
Object staticArguments,
|
|
|
|
Object[] appendixResult) {
|
|
|
|
MethodHandle bootstrapMethod = (MethodHandle)bootstrapMethodObj;
|
|
|
|
Class<?> caller = (Class<?>)callerObj;
|
|
|
|
String name = nameObj.toString().intern();
|
|
|
|
MethodType type = (MethodType)typeObj;
|
|
|
|
appendixResult[0] = CallSite.makeSite(bootstrapMethod,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
staticArguments,
|
|
|
|
caller);
|
|
|
|
return Invokers.linkToCallSiteMethod(type);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The JVM wants a pointer to a MethodType. Oblige it by finding or creating one.
|
|
|
|
*/
|
|
|
|
static MethodType findMethodHandleType(Class<?> rtype, Class<?>[] ptypes) {
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodType.makeImpl(rtype, ptypes, true);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
2010-10-30 21:02:30 -07:00
|
|
|
/**
|
2012-07-24 10:47:44 -07:00
|
|
|
* The JVM wants to link a call site that requires a dynamic type check.
|
|
|
|
* Name is a type-checking invoker, invokeExact or invoke.
|
|
|
|
* Return a JVM method (MemberName) to handle the invoking.
|
|
|
|
* The method assumes the following arguments on the stack:
|
|
|
|
* 0: the method handle being invoked
|
|
|
|
* 1-N: the arguments to the method handle invocation
|
|
|
|
* N+1: an implicitly added type argument (the given MethodType)
|
2010-10-30 21:02:30 -07:00
|
|
|
*/
|
2012-07-24 10:47:44 -07:00
|
|
|
static MemberName linkMethod(Class<?> callerClass, int refKind,
|
|
|
|
Class<?> defc, String name, Object type,
|
|
|
|
Object[] appendixResult) {
|
|
|
|
if (!TRACE_METHOD_LINKAGE)
|
|
|
|
return linkMethodImpl(callerClass, refKind, defc, name, type, appendixResult);
|
|
|
|
return linkMethodTracing(callerClass, refKind, defc, name, type, appendixResult);
|
2011-03-18 00:03:24 -07:00
|
|
|
}
|
2012-07-24 10:47:44 -07:00
|
|
|
static MemberName linkMethodImpl(Class<?> callerClass, int refKind,
|
|
|
|
Class<?> defc, String name, Object type,
|
|
|
|
Object[] appendixResult) {
|
2012-08-17 13:42:25 -07:00
|
|
|
try {
|
|
|
|
if (defc == MethodHandle.class && refKind == REF_invokeVirtual) {
|
|
|
|
switch (name) {
|
|
|
|
case "invoke":
|
|
|
|
return Invokers.genericInvokerMethod(fixMethodType(callerClass, type), appendixResult);
|
|
|
|
case "invokeExact":
|
|
|
|
return Invokers.exactInvokerMethod(fixMethodType(callerClass, type), appendixResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
if (ex instanceof LinkageError)
|
|
|
|
throw (LinkageError) ex;
|
|
|
|
else
|
|
|
|
throw new LinkageError(ex.getMessage(), ex);
|
2011-05-12 19:27:49 -07:00
|
|
|
}
|
2012-08-17 13:42:25 -07:00
|
|
|
throw new LinkageError("no such method "+defc.getName()+"."+name+type);
|
|
|
|
}
|
|
|
|
private static MethodType fixMethodType(Class<?> callerClass, Object type) {
|
|
|
|
if (type instanceof MethodType)
|
|
|
|
return (MethodType) type;
|
|
|
|
else
|
|
|
|
return MethodType.fromMethodDescriptorString((String)type, callerClass.getClassLoader());
|
2012-07-24 10:47:44 -07:00
|
|
|
}
|
|
|
|
// Tracing logic:
|
|
|
|
static MemberName linkMethodTracing(Class<?> callerClass, int refKind,
|
|
|
|
Class<?> defc, String name, Object type,
|
|
|
|
Object[] appendixResult) {
|
|
|
|
System.out.println("linkMethod "+defc.getName()+"."+
|
|
|
|
name+type+"/"+Integer.toHexString(refKind));
|
|
|
|
try {
|
|
|
|
MemberName res = linkMethodImpl(callerClass, refKind, defc, name, type, appendixResult);
|
|
|
|
System.out.println("linkMethod => "+res+" + "+appendixResult[0]);
|
|
|
|
return res;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
System.out.println("linkMethod => throw "+ex);
|
|
|
|
throw ex;
|
2010-10-30 21:02:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-17 13:42:25 -07:00
|
|
|
|
2010-06-08 23:08:56 -07:00
|
|
|
/**
|
|
|
|
* 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.)
|
2012-07-24 10:47:44 -07:00
|
|
|
* The type argument is a Class for field requests and a MethodType for non-fields.
|
|
|
|
* <p>
|
|
|
|
* Recent versions of the JVM may also pass a resolved MemberName for the type.
|
|
|
|
* In that case, the name is ignored and may be null.
|
2010-06-08 23:08:56 -07:00
|
|
|
*/
|
|
|
|
static MethodHandle linkMethodHandleConstant(Class<?> callerClass, int refKind,
|
|
|
|
Class<?> defc, String name, Object type) {
|
2010-09-08 18:40:23 -07:00
|
|
|
try {
|
|
|
|
Lookup lookup = IMPL_LOOKUP.in(callerClass);
|
2012-07-24 10:47:44 -07:00
|
|
|
assert(refKindIsValid(refKind));
|
|
|
|
return lookup.linkMethodHandleConstant((byte) refKind, defc, name, type);
|
2011-02-11 01:26:32 -08:00
|
|
|
} catch (ReflectiveOperationException ex) {
|
2010-09-08 18:40:23 -07:00
|
|
|
Error err = new IncompatibleClassChangeError();
|
|
|
|
err.initCause(ex);
|
|
|
|
throw err;
|
2010-06-08 23:08:56 -07:00
|
|
|
}
|
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|