2009-05-05 22:40:09 -07:00
|
|
|
/*
|
2011-03-18 00:03:24 -07:00
|
|
|
* Copyright (c) 2008, 2011, 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 sun.invoke.util.VerifyType;
|
|
|
|
import sun.invoke.util.Wrapper;
|
|
|
|
import static java.lang.invoke.MethodHandleStatics.*;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The flavor of method handle which emulates an invoke instruction
|
|
|
|
* on a predetermined argument. The JVM dispatches to the correct method
|
|
|
|
* when the handle is created, not when it is invoked.
|
|
|
|
* @author jrose
|
|
|
|
*/
|
2011-03-18 00:03:24 -07:00
|
|
|
class BoundMethodHandle extends MethodHandle {
|
2009-05-05 22:40:09 -07:00
|
|
|
//MethodHandle vmtarget; // next BMH or final DMH or methodOop
|
|
|
|
private final Object argument; // argument to insert
|
|
|
|
private final int vmargslot; // position at which it is inserted
|
|
|
|
|
|
|
|
// Constructors in this class *must* be package scoped or private.
|
|
|
|
|
|
|
|
/** Bind a direct MH to its receiver (or first ref. argument).
|
|
|
|
* The JVM will pre-dispatch the MH if it is not already static.
|
|
|
|
*/
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ BoundMethodHandle(DirectMethodHandle mh, Object argument) {
|
|
|
|
super(mh.type().dropParameterTypes(0, 1));
|
2009-05-05 22:40:09 -07:00
|
|
|
// check the type now, once for all:
|
|
|
|
this.argument = checkReferenceArgument(argument, mh, 0);
|
|
|
|
this.vmargslot = this.type().parameterSlotCount();
|
2011-03-18 00:03:24 -07:00
|
|
|
initTarget(mh, 0);
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/** Insert an argument into an arbitrary method handle.
|
|
|
|
* If argnum is zero, inserts the first argument, etc.
|
|
|
|
* The argument type must be a reference.
|
|
|
|
*/
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ BoundMethodHandle(MethodHandle mh, Object argument, int argnum) {
|
2010-01-07 16:16:45 -08:00
|
|
|
this(mh.type().dropParameterTypes(argnum, argnum+1),
|
|
|
|
mh, argument, argnum);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Insert an argument into an arbitrary method handle.
|
|
|
|
* If argnum is zero, inserts the first argument, etc.
|
|
|
|
*/
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ BoundMethodHandle(MethodType type, MethodHandle mh, Object argument, int argnum) {
|
|
|
|
super(type);
|
2010-01-07 16:16:45 -08:00
|
|
|
if (mh.type().parameterType(argnum).isPrimitive())
|
2009-05-05 22:40:09 -07:00
|
|
|
this.argument = bindPrimitiveArgument(argument, mh, argnum);
|
|
|
|
else {
|
|
|
|
this.argument = checkReferenceArgument(argument, mh, argnum);
|
|
|
|
}
|
2010-01-07 16:16:45 -08:00
|
|
|
this.vmargslot = type.parameterSlotDepth(argnum);
|
|
|
|
initTarget(mh, argnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initTarget(MethodHandle mh, int argnum) {
|
2011-03-18 00:03:24 -07:00
|
|
|
//this.vmtarget = mh; // maybe updated by JVM
|
|
|
|
MethodHandleNatives.init(this, mh, argnum);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** For the AdapterMethodHandle subclass.
|
|
|
|
*/
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ BoundMethodHandle(MethodType type, Object argument, int vmargslot) {
|
|
|
|
super(type);
|
2009-05-05 22:40:09 -07:00
|
|
|
this.argument = argument;
|
|
|
|
this.vmargslot = vmargslot;
|
2010-10-30 21:02:30 -07:00
|
|
|
assert(this instanceof AdapterMethodHandle);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
/** Initialize the current object as a self-bound method handle, binding it
|
2010-01-07 16:16:45 -08:00
|
|
|
* as the first argument of the method handle {@code entryPoint}.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The invocation type of the resulting method handle will be the
|
2010-01-07 16:16:45 -08:00
|
|
|
* same as {@code entryPoint}, except that the first argument
|
2009-05-05 22:40:09 -07:00
|
|
|
* type will be dropped.
|
|
|
|
*/
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ BoundMethodHandle(MethodHandle entryPoint) {
|
|
|
|
super(entryPoint.type().dropParameterTypes(0, 1));
|
2010-01-07 16:16:45 -08:00
|
|
|
this.argument = this; // kludge; get rid of
|
|
|
|
this.vmargslot = this.type().parameterSlotDepth(0);
|
|
|
|
initTarget(entryPoint, 0);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Make sure the given {@code argument} can be used as {@code argnum}-th
|
|
|
|
* parameter of the given method handle {@code mh}, which must be a reference.
|
|
|
|
* <p>
|
|
|
|
* If this fails, throw a suitable {@code WrongMethodTypeException},
|
|
|
|
* which will prevent the creation of an illegally typed bound
|
|
|
|
* method handle.
|
|
|
|
*/
|
|
|
|
final static Object checkReferenceArgument(Object argument, MethodHandle mh, int argnum) {
|
|
|
|
Class<?> ptype = mh.type().parameterType(argnum);
|
|
|
|
if (ptype.isPrimitive()) {
|
|
|
|
// fail
|
|
|
|
} else if (argument == null) {
|
|
|
|
return null;
|
|
|
|
} else if (VerifyType.isNullReferenceConversion(argument.getClass(), ptype)) {
|
|
|
|
return argument;
|
|
|
|
}
|
|
|
|
throw badBoundArgumentException(argument, mh, argnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Make sure the given {@code argument} can be used as {@code argnum}-th
|
|
|
|
* parameter of the given method handle {@code mh}, which must be a primitive.
|
|
|
|
* <p>
|
|
|
|
* If this fails, throw a suitable {@code WrongMethodTypeException},
|
|
|
|
* which will prevent the creation of an illegally typed bound
|
|
|
|
* method handle.
|
|
|
|
*/
|
|
|
|
final static Object bindPrimitiveArgument(Object argument, MethodHandle mh, int argnum) {
|
|
|
|
Class<?> ptype = mh.type().parameterType(argnum);
|
|
|
|
Wrapper wrap = Wrapper.forPrimitiveType(ptype);
|
|
|
|
Object zero = wrap.zero();
|
|
|
|
if (zero == null) {
|
|
|
|
// fail
|
|
|
|
} else if (argument == null) {
|
|
|
|
if (ptype != int.class && wrap.isSubwordOrInt())
|
|
|
|
return Integer.valueOf(0);
|
|
|
|
else
|
|
|
|
return zero;
|
|
|
|
} else if (VerifyType.isNullReferenceConversion(argument.getClass(), zero.getClass())) {
|
|
|
|
if (ptype != int.class && wrap.isSubwordOrInt())
|
|
|
|
return Wrapper.INT.wrap(argument);
|
|
|
|
else
|
|
|
|
return argument;
|
|
|
|
}
|
|
|
|
throw badBoundArgumentException(argument, mh, argnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
final static RuntimeException badBoundArgumentException(Object argument, MethodHandle mh, int argnum) {
|
|
|
|
String atype = (argument == null) ? "null" : argument.getClass().toString();
|
2011-05-26 17:37:36 -07:00
|
|
|
return new ClassCastException("cannot bind "+atype+" argument to parameter #"+argnum+" of "+mh.type());
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-05-17 19:48:19 -07:00
|
|
|
String debugString() {
|
2011-03-18 00:03:24 -07:00
|
|
|
return addTypeString(baseName(), this);
|
2010-10-30 21:08:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Component of toString() before the type string. */
|
|
|
|
protected String baseName() {
|
2010-01-07 16:16:45 -08:00
|
|
|
MethodHandle mh = this;
|
|
|
|
while (mh instanceof BoundMethodHandle) {
|
|
|
|
Object info = MethodHandleNatives.getTargetInfo(mh);
|
|
|
|
if (info instanceof MethodHandle) {
|
|
|
|
mh = (MethodHandle) info;
|
|
|
|
} else {
|
|
|
|
String name = null;
|
|
|
|
if (info instanceof MemberName)
|
|
|
|
name = ((MemberName)info).getName();
|
|
|
|
if (name != null)
|
|
|
|
return name;
|
|
|
|
else
|
2010-10-30 21:08:23 -07:00
|
|
|
return noParens(super.toString()); // "invoke", probably
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
|
|
|
assert(mh != this);
|
|
|
|
}
|
2010-10-30 21:08:23 -07:00
|
|
|
return noParens(mh.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String noParens(String str) {
|
|
|
|
int paren = str.indexOf('(');
|
|
|
|
if (paren >= 0) str = str.substring(0, paren);
|
|
|
|
return str;
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
}
|