提交 9199c174 编写于 作者: J jrose

6839872: remove implementation inheritance from JSR 292 APIs

Summary: move everything into a single package; remove all multi-package machinery
Reviewed-by: twisti, forax
上级 d672a43f
/* /*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,15 +23,11 @@ ...@@ -23,15 +23,11 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import sun.dyn.util.VerifyType; import sun.dyn.util.VerifyType;
import sun.dyn.util.Wrapper; import sun.dyn.util.Wrapper;
import java.dyn.*; import static java.dyn.MethodHandleStatics.*;
import java.util.List;
import sun.dyn.MethodHandleNatives.Constants;
import static sun.dyn.MethodHandleImpl.IMPL_LOOKUP;
import static sun.dyn.MemberName.newIllegalArgumentException;
/** /**
* The flavor of method handle which emulates an invoke instruction * The flavor of method handle which emulates an invoke instruction
...@@ -39,37 +35,29 @@ import static sun.dyn.MemberName.newIllegalArgumentException; ...@@ -39,37 +35,29 @@ import static sun.dyn.MemberName.newIllegalArgumentException;
* when the handle is created, not when it is invoked. * when the handle is created, not when it is invoked.
* @author jrose * @author jrose
*/ */
public class BoundMethodHandle extends MethodHandle { class BoundMethodHandle extends MethodHandle {
//MethodHandle vmtarget; // next BMH or final DMH or methodOop //MethodHandle vmtarget; // next BMH or final DMH or methodOop
private final Object argument; // argument to insert private final Object argument; // argument to insert
private final int vmargslot; // position at which it is inserted private final int vmargslot; // position at which it is inserted
private static final Access IMPL_TOKEN = Access.getToken();
private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory(IMPL_TOKEN);
// Constructors in this class *must* be package scoped or private. // Constructors in this class *must* be package scoped or private.
/** Bind a direct MH to its receiver (or first ref. argument). /** Bind a direct MH to its receiver (or first ref. argument).
* The JVM will pre-dispatch the MH if it is not already static. * The JVM will pre-dispatch the MH if it is not already static.
*/ */
BoundMethodHandle(DirectMethodHandle mh, Object argument) { /*non-public*/ BoundMethodHandle(DirectMethodHandle mh, Object argument) {
super(Access.TOKEN, mh.type().dropParameterTypes(0, 1)); super(mh.type().dropParameterTypes(0, 1));
// check the type now, once for all: // check the type now, once for all:
this.argument = checkReferenceArgument(argument, mh, 0); this.argument = checkReferenceArgument(argument, mh, 0);
this.vmargslot = this.type().parameterSlotCount(); this.vmargslot = this.type().parameterSlotCount();
if (MethodHandleNatives.JVM_SUPPORT) { initTarget(mh, 0);
this.vmtarget = null; // maybe updated by JVM
MethodHandleNatives.init(this, mh, 0);
} else {
this.vmtarget = mh;
}
} }
/** Insert an argument into an arbitrary method handle. /** Insert an argument into an arbitrary method handle.
* If argnum is zero, inserts the first argument, etc. * If argnum is zero, inserts the first argument, etc.
* The argument type must be a reference. * The argument type must be a reference.
*/ */
BoundMethodHandle(MethodHandle mh, Object argument, int argnum) { /*non-public*/ BoundMethodHandle(MethodHandle mh, Object argument, int argnum) {
this(mh.type().dropParameterTypes(argnum, argnum+1), this(mh.type().dropParameterTypes(argnum, argnum+1),
mh, argument, argnum); mh, argument, argnum);
} }
...@@ -77,8 +65,8 @@ public class BoundMethodHandle extends MethodHandle { ...@@ -77,8 +65,8 @@ public class BoundMethodHandle extends MethodHandle {
/** Insert an argument into an arbitrary method handle. /** Insert an argument into an arbitrary method handle.
* If argnum is zero, inserts the first argument, etc. * If argnum is zero, inserts the first argument, etc.
*/ */
BoundMethodHandle(MethodType type, MethodHandle mh, Object argument, int argnum) { /*non-public*/ BoundMethodHandle(MethodType type, MethodHandle mh, Object argument, int argnum) {
super(Access.TOKEN, type); super(type);
if (mh.type().parameterType(argnum).isPrimitive()) if (mh.type().parameterType(argnum).isPrimitive())
this.argument = bindPrimitiveArgument(argument, mh, argnum); this.argument = bindPrimitiveArgument(argument, mh, argnum);
else { else {
...@@ -89,18 +77,14 @@ public class BoundMethodHandle extends MethodHandle { ...@@ -89,18 +77,14 @@ public class BoundMethodHandle extends MethodHandle {
} }
private void initTarget(MethodHandle mh, int argnum) { private void initTarget(MethodHandle mh, int argnum) {
if (MethodHandleNatives.JVM_SUPPORT) { //this.vmtarget = mh; // maybe updated by JVM
this.vmtarget = null; // maybe updated by JVM MethodHandleNatives.init(this, mh, argnum);
MethodHandleNatives.init(this, mh, argnum);
} else {
this.vmtarget = mh;
}
} }
/** For the AdapterMethodHandle subclass. /** For the AdapterMethodHandle subclass.
*/ */
BoundMethodHandle(MethodType type, Object argument, int vmargslot) { /*non-public*/ BoundMethodHandle(MethodType type, Object argument, int vmargslot) {
super(Access.TOKEN, type); super(type);
this.argument = argument; this.argument = argument;
this.vmargslot = vmargslot; this.vmargslot = vmargslot;
assert(this instanceof AdapterMethodHandle); assert(this instanceof AdapterMethodHandle);
...@@ -112,8 +96,8 @@ public class BoundMethodHandle extends MethodHandle { ...@@ -112,8 +96,8 @@ public class BoundMethodHandle extends MethodHandle {
* same as {@code entryPoint}, except that the first argument * same as {@code entryPoint}, except that the first argument
* type will be dropped. * type will be dropped.
*/ */
protected BoundMethodHandle(Access token, MethodHandle entryPoint) { /*non-public*/ BoundMethodHandle(MethodHandle entryPoint) {
super(token, entryPoint.type().dropParameterTypes(0, 1)); super(entryPoint.type().dropParameterTypes(0, 1));
this.argument = this; // kludge; get rid of this.argument = this; // kludge; get rid of
this.vmargslot = this.type().parameterSlotDepth(0); this.vmargslot = this.type().parameterSlotDepth(0);
initTarget(entryPoint, 0); initTarget(entryPoint, 0);
...@@ -172,7 +156,7 @@ public class BoundMethodHandle extends MethodHandle { ...@@ -172,7 +156,7 @@ public class BoundMethodHandle extends MethodHandle {
@Override @Override
public String toString() { public String toString() {
return MethodHandleImpl.addTypeString(baseName(), this); return addTypeString(baseName(), this);
} }
/** Component of toString() before the type string. */ /** Component of toString() before the type string. */
......
...@@ -25,10 +25,10 @@ ...@@ -25,10 +25,10 @@
package java.dyn; package java.dyn;
import sun.dyn.*;
import sun.dyn.empty.Empty; import sun.dyn.empty.Empty;
import sun.misc.Unsafe; import sun.misc.Unsafe;
import java.util.Collection; import static java.dyn.MethodHandleStatics.*;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* A {@code CallSite} is a holder for a variable {@link MethodHandle}, * A {@code CallSite} is a holder for a variable {@link MethodHandle},
...@@ -85,7 +85,6 @@ private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String nam ...@@ -85,7 +85,6 @@ private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String nam
*/ */
abstract abstract
public class CallSite { public class CallSite {
private static final Access IMPL_TOKEN = Access.getToken();
static { MethodHandleImpl.initStatics(); } static { MethodHandleImpl.initStatics(); }
// Fields used only by the JVM. Do not use or change. // Fields used only by the JVM. Do not use or change.
...@@ -111,7 +110,7 @@ public class CallSite { ...@@ -111,7 +110,7 @@ public class CallSite {
*/ */
/*package-private*/ /*package-private*/
CallSite(MethodType type) { CallSite(MethodType type) {
target = MethodHandles.invokers(type).uninitializedCallSite(); target = type.invokers().uninitializedCallSite();
} }
/** /**
...@@ -218,7 +217,7 @@ public class CallSite { ...@@ -218,7 +217,7 @@ public class CallSite {
public abstract MethodHandle dynamicInvoker(); public abstract MethodHandle dynamicInvoker();
/*non-public*/ MethodHandle makeDynamicInvoker() { /*non-public*/ MethodHandle makeDynamicInvoker() {
MethodHandle getTarget = MethodHandleImpl.bindReceiver(IMPL_TOKEN, GET_TARGET, this); MethodHandle getTarget = MethodHandleImpl.bindReceiver(GET_TARGET, this);
MethodHandle invoker = MethodHandles.exactInvoker(this.type()); MethodHandle invoker = MethodHandles.exactInvoker(this.type());
return MethodHandles.foldArguments(invoker, getTarget); return MethodHandles.foldArguments(invoker, getTarget);
} }
...@@ -226,7 +225,7 @@ public class CallSite { ...@@ -226,7 +225,7 @@ public class CallSite {
private static final MethodHandle GET_TARGET; private static final MethodHandle GET_TARGET;
static { static {
try { try {
GET_TARGET = MethodHandles.Lookup.IMPL_LOOKUP. GET_TARGET = IMPL_LOOKUP.
findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class)); findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class));
} catch (ReflectiveOperationException ignore) { } catch (ReflectiveOperationException ignore) {
throw new InternalError(); throw new InternalError();
...@@ -252,7 +251,6 @@ public class CallSite { ...@@ -252,7 +251,6 @@ public class CallSite {
/*package-private*/ /*package-private*/
void setTargetNormal(MethodHandle newTarget) { void setTargetNormal(MethodHandle newTarget) {
target = newTarget; target = newTarget;
//CallSiteImpl.setCallSiteTarget(IMPL_TOKEN, this, newTarget);
} }
/*package-private*/ /*package-private*/
MethodHandle getTargetVolatile() { MethodHandle getTargetVolatile() {
...@@ -261,6 +259,105 @@ public class CallSite { ...@@ -261,6 +259,105 @@ public class CallSite {
/*package-private*/ /*package-private*/
void setTargetVolatile(MethodHandle newTarget) { void setTargetVolatile(MethodHandle newTarget) {
unsafe.putObjectVolatile(this, TARGET_OFFSET, newTarget); unsafe.putObjectVolatile(this, TARGET_OFFSET, newTarget);
//CallSiteImpl.setCallSiteTarget(IMPL_TOKEN, this, newTarget); }
// this implements the upcall from the JVM, MethodHandleNatives.makeDynamicCallSite:
static CallSite makeSite(MethodHandle bootstrapMethod,
// Callee information:
String name, MethodType type,
// Extra arguments for BSM, if any:
Object info,
// Caller information:
MemberName callerMethod, int callerBCI) {
Class<?> callerClass = callerMethod.getDeclaringClass();
Object caller;
if (bootstrapMethod.type().parameterType(0) == Class.class && TRANSITIONAL_BEFORE_PFD)
caller = callerClass; // remove for PFD
else
caller = IMPL_LOOKUP.in(callerClass);
if (bootstrapMethod == null && TRANSITIONAL_BEFORE_PFD) {
// If there is no bootstrap method, throw IncompatibleClassChangeError.
// This is a valid generic error type for resolution (JLS 12.3.3).
throw new IncompatibleClassChangeError
("Class "+callerClass.getName()+" has not declared a bootstrap method for invokedynamic");
}
CallSite site;
try {
Object binding;
info = maybeReBox(info);
if (info == null) {
binding = bootstrapMethod.invokeGeneric(caller, name, type);
} else if (!info.getClass().isArray()) {
binding = bootstrapMethod.invokeGeneric(caller, name, type, info);
} else {
Object[] argv = (Object[]) info;
maybeReBoxElements(argv);
if (3 + argv.length > 255)
throw new InvokeDynamicBootstrapError("too many bootstrap method arguments");
MethodType bsmType = bootstrapMethod.type();
if (bsmType.parameterCount() == 4 && bsmType.parameterType(3) == Object[].class)
binding = bootstrapMethod.invokeGeneric(caller, name, type, argv);
else
binding = MethodHandles.spreadInvoker(bsmType, 3)
.invokeGeneric(bootstrapMethod, caller, name, type, argv);
}
//System.out.println("BSM for "+name+type+" => "+binding);
if (binding instanceof CallSite) {
site = (CallSite) binding;
} else if (binding instanceof MethodHandle && TRANSITIONAL_BEFORE_PFD) {
// Transitional!
MethodHandle target = (MethodHandle) binding;
site = new ConstantCallSite(target);
} else {
throw new ClassCastException("bootstrap method failed to produce a CallSite");
}
if (TRANSITIONAL_BEFORE_PFD)
PRIVATE_INITIALIZE_CALL_SITE.invokeExact(site, name, type,
callerMethod, callerBCI);
assert(site.getTarget() != null);
assert(site.getTarget().type().equals(type));
} catch (Throwable ex) {
InvokeDynamicBootstrapError bex;
if (ex instanceof InvokeDynamicBootstrapError)
bex = (InvokeDynamicBootstrapError) ex;
else
bex = new InvokeDynamicBootstrapError("call site initialization exception", ex);
throw bex;
}
return site;
}
private static final boolean TRANSITIONAL_BEFORE_PFD = true; // FIXME: remove for PFD
// booby trap to force removal after package rename:
static { if (TRANSITIONAL_BEFORE_PFD) assert(CallSite.class.getName().startsWith("java.dyn.")); }
private static Object maybeReBox(Object x) {
if (x instanceof Integer) {
int xi = (int) x;
if (xi == (byte) xi)
x = xi; // must rebox; see JLS 5.1.7
}
return x;
}
private static void maybeReBoxElements(Object[] xa) {
for (int i = 0; i < xa.length; i++) {
xa[i] = maybeReBox(xa[i]);
}
}
// This method is private in CallSite because it touches private fields in CallSite.
// These private fields (vmmethod, vmindex) are specific to the JVM.
private static final MethodHandle PRIVATE_INITIALIZE_CALL_SITE;
static {
try {
PRIVATE_INITIALIZE_CALL_SITE =
!TRANSITIONAL_BEFORE_PFD ? null :
IMPL_LOOKUP.findVirtual(CallSite.class, "initializeFromJVM",
MethodType.methodType(void.class,
String.class, MethodType.class,
MemberName.class, int.class));
} catch (ReflectiveOperationException ex) {
throw uncaughtException(ex);
}
} }
} }
...@@ -27,8 +27,6 @@ package java.dyn; ...@@ -27,8 +27,6 @@ package java.dyn;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.lang.reflect.UndeclaredThrowableException;
/** /**
* Lazily associate a computed value with (potentially) every type. * Lazily associate a computed value with (potentially) every type.
......
/* /*
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,10 +23,9 @@ ...@@ -23,10 +23,9 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*; import static java.dyn.MethodHandleNatives.Constants.*;
import static sun.dyn.MethodHandleNatives.Constants.*;
/** /**
* The flavor of method handle which emulates invokespecial or invokestatic. * The flavor of method handle which emulates invokespecial or invokestatic.
...@@ -39,7 +38,7 @@ class DirectMethodHandle extends MethodHandle { ...@@ -39,7 +38,7 @@ class DirectMethodHandle extends MethodHandle {
// Constructors in this class *must* be package scoped or private. // Constructors in this class *must* be package scoped or private.
DirectMethodHandle(MethodType mtype, MemberName m, boolean doDispatch, Class<?> lookupClass) { DirectMethodHandle(MethodType mtype, MemberName m, boolean doDispatch, Class<?> lookupClass) {
super(Access.TOKEN, mtype); super(mtype);
assert(m.isMethod() || !doDispatch && m.isConstructor()); assert(m.isMethod() || !doDispatch && m.isConstructor());
if (!m.isResolved()) if (!m.isResolved())
......
/* /*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,11 +23,11 @@ ...@@ -23,11 +23,11 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*;
import java.lang.reflect.*; import java.lang.reflect.*;
import static sun.dyn.MemberName.newIllegalArgumentException; import static java.dyn.MethodHandleStatics.*;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* These adapters apply arbitrary conversions to arguments * These adapters apply arbitrary conversions to arguments
...@@ -123,7 +123,7 @@ class FilterGeneric { ...@@ -123,7 +123,7 @@ class FilterGeneric {
MethodType entryType = entryType(kind, pos, filterType, targetType); MethodType entryType = entryType(kind, pos, filterType, targetType);
if (entryType.generic() != entryType) if (entryType.generic() != entryType)
throw newIllegalArgumentException("must be generic: "+entryType); throw newIllegalArgumentException("must be generic: "+entryType);
MethodTypeImpl form = MethodTypeImpl.of(entryType); MethodTypeForm form = entryType.form();
FilterGeneric filterGen = form.filterGeneric; FilterGeneric filterGen = form.filterGeneric;
if (filterGen == null) if (filterGen == null)
form.filterGeneric = filterGen = new FilterGeneric(entryType); form.filterGeneric = filterGen = new FilterGeneric(entryType);
...@@ -186,7 +186,7 @@ class FilterGeneric { ...@@ -186,7 +186,7 @@ class FilterGeneric {
// see if it has the required invoke method // see if it has the required invoke method
MethodHandle entryPoint = null; MethodHandle entryPoint = null;
try { try {
entryPoint = MethodHandleImpl.IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls); entryPoint = IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
} }
if (entryPoint == null) continue; if (entryPoint == null) continue;
...@@ -231,7 +231,7 @@ class FilterGeneric { ...@@ -231,7 +231,7 @@ class FilterGeneric {
@Override @Override
public String toString() { public String toString() {
return MethodHandleImpl.addTypeString(target, this); return addTypeString(target, this);
} }
protected boolean isPrototype() { return target == null; } protected boolean isPrototype() { return target == null; }
...@@ -246,7 +246,7 @@ class FilterGeneric { ...@@ -246,7 +246,7 @@ class FilterGeneric {
protected Adapter(MethodHandle entryPoint, protected Adapter(MethodHandle entryPoint,
MethodHandle filter, MethodHandle target) { MethodHandle filter, MethodHandle target) {
super(Access.TOKEN, entryPoint); super(entryPoint);
this.filter = filter; this.filter = filter;
this.target = target; this.target = target;
} }
...@@ -256,7 +256,7 @@ class FilterGeneric { ...@@ -256,7 +256,7 @@ class FilterGeneric {
MethodHandle filter, MethodHandle target); MethodHandle filter, MethodHandle target);
// { return new ThisType(entryPoint, filter, target); } // { return new ThisType(entryPoint, filter, target); }
static private final String CLASS_PREFIX; // "sun.dyn.FilterGeneric$" static private final String CLASS_PREFIX; // "java.dyn.FilterGeneric$"
static { static {
String aname = Adapter.class.getName(); String aname = Adapter.class.getName();
String sname = Adapter.class.getSimpleName(); String sname = Adapter.class.getSimpleName();
......
/* /*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,10 +23,10 @@ ...@@ -23,10 +23,10 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*; import static java.dyn.MethodHandleStatics.*;
import static sun.dyn.MemberName.uncaughtException; import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* Unary function composition, useful for many small plumbing jobs. * Unary function composition, useful for many small plumbing jobs.
...@@ -36,7 +36,7 @@ import static sun.dyn.MemberName.uncaughtException; ...@@ -36,7 +36,7 @@ import static sun.dyn.MemberName.uncaughtException;
* final method type is the responsibility of a JVM-level adapter. * final method type is the responsibility of a JVM-level adapter.
* @author jrose * @author jrose
*/ */
public class FilterOneArgument extends BoundMethodHandle { class FilterOneArgument extends BoundMethodHandle {
protected final MethodHandle filter; // Object -> Object protected final MethodHandle filter; // Object -> Object
protected final MethodHandle target; // Object -> Object protected final MethodHandle target; // Object -> Object
...@@ -54,15 +54,15 @@ public class FilterOneArgument extends BoundMethodHandle { ...@@ -54,15 +54,15 @@ public class FilterOneArgument extends BoundMethodHandle {
static { static {
try { try {
INVOKE = INVOKE =
MethodHandleImpl.IMPL_LOOKUP.findVirtual(FilterOneArgument.class, "invoke", IMPL_LOOKUP.findVirtual(FilterOneArgument.class, "invoke",
MethodType.genericMethodType(1)); MethodType.genericMethodType(1));
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
throw uncaughtException(ex); throw uncaughtException(ex);
} }
} }
protected FilterOneArgument(MethodHandle filter, MethodHandle target) { protected FilterOneArgument(MethodHandle filter, MethodHandle target) {
super(Access.TOKEN, INVOKE); super(INVOKE);
this.filter = filter; this.filter = filter;
this.target = target; this.target = target;
} }
......
...@@ -23,12 +23,13 @@ ...@@ -23,12 +23,13 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*; import sun.dyn.util.ValueConversions;
import sun.dyn.util.Wrapper;
import java.lang.reflect.*; import java.lang.reflect.*;
import sun.dyn.util.*; import static java.dyn.MethodHandleStatics.*;
import static sun.dyn.MethodTypeImpl.invokers; import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* Adapters which mediate between incoming calls which are generic * Adapters which mediate between incoming calls which are generic
...@@ -82,8 +83,8 @@ class FromGeneric { ...@@ -82,8 +83,8 @@ class FromGeneric {
} }
// outgoing primitive arguments will be wrapped; unwrap them // outgoing primitive arguments will be wrapped; unwrap them
MethodType primsAsObj = MethodTypeImpl.of(targetType).primArgsAsBoxes(); MethodType primsAsObj = targetType.form().primArgsAsBoxes();
MethodType objArgsRawRet = MethodTypeImpl.of(primsAsObj).primsAsInts(); MethodType objArgsRawRet = primsAsObj.form().primsAsInts();
if (objArgsRawRet != targetType) if (objArgsRawRet != targetType)
ad = findAdapter(internalType0 = objArgsRawRet); ad = findAdapter(internalType0 = objArgsRawRet);
if (ad == null) { if (ad == null) {
...@@ -129,16 +130,16 @@ class FromGeneric { ...@@ -129,16 +130,16 @@ class FromGeneric {
MethodType targetType, MethodType internalType) { MethodType targetType, MethodType internalType) {
// All the adapters we have here have reference-untyped internal calls. // All the adapters we have here have reference-untyped internal calls.
assert(internalType == internalType.erase()); assert(internalType == internalType.erase());
MethodHandle invoker = invokers(targetType).exactInvoker(); MethodHandle invoker = targetType.invokers().exactInvoker();
// cast all narrow reference types, unbox all primitive arguments: // cast all narrow reference types, unbox all primitive arguments:
MethodType fixArgsType = internalType.changeReturnType(targetType.returnType()); MethodType fixArgsType = internalType.changeReturnType(targetType.returnType());
MethodHandle fixArgs = AdapterMethodHandle.convertArguments(Access.TOKEN, MethodHandle fixArgs = MethodHandleImpl.convertArguments(
invoker, Invokers.invokerType(fixArgsType), invoker, Invokers.invokerType(fixArgsType),
invoker.type(), null); invoker.type(), null);
if (fixArgs == null) if (fixArgs == null)
throw new InternalError("bad fixArgs"); throw new InternalError("bad fixArgs");
// reinterpret the calling sequence as raw: // reinterpret the calling sequence as raw:
MethodHandle retyper = AdapterMethodHandle.makeRetypeRaw(Access.TOKEN, MethodHandle retyper = AdapterMethodHandle.makeRetypeRaw(
Invokers.invokerType(internalType), fixArgs); Invokers.invokerType(internalType), fixArgs);
if (retyper == null) if (retyper == null)
throw new InternalError("bad retyper"); throw new InternalError("bad retyper");
...@@ -171,7 +172,7 @@ class FromGeneric { ...@@ -171,7 +172,7 @@ class FromGeneric {
/** Return the adapter information for this type's erasure. */ /** Return the adapter information for this type's erasure. */
static FromGeneric of(MethodType type) { static FromGeneric of(MethodType type) {
MethodTypeImpl form = MethodTypeImpl.of(type); MethodTypeForm form = type.form();
FromGeneric fromGen = form.fromGeneric; FromGeneric fromGen = form.fromGeneric;
if (fromGen == null) if (fromGen == null)
form.fromGeneric = fromGen = new FromGeneric(form.erasedType()); form.fromGeneric = fromGen = new FromGeneric(form.erasedType());
...@@ -185,7 +186,7 @@ class FromGeneric { ...@@ -185,7 +186,7 @@ class FromGeneric {
/* Create an adapter that handles spreading calls for the given type. */ /* Create an adapter that handles spreading calls for the given type. */
static Adapter findAdapter(MethodType internalType) { static Adapter findAdapter(MethodType internalType) {
MethodType entryType = internalType.generic(); MethodType entryType = internalType.generic();
MethodTypeImpl form = MethodTypeImpl.of(internalType); MethodTypeForm form = internalType.form();
Class<?> rtype = internalType.returnType(); Class<?> rtype = internalType.returnType();
int argc = form.parameterCount(); int argc = form.parameterCount();
int lac = form.longPrimitiveParameterCount(); int lac = form.longPrimitiveParameterCount();
...@@ -203,7 +204,7 @@ class FromGeneric { ...@@ -203,7 +204,7 @@ class FromGeneric {
// see if it has the required invoke method // see if it has the required invoke method
MethodHandle entryPoint = null; MethodHandle entryPoint = null;
try { try {
entryPoint = MethodHandleImpl.IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls); entryPoint = IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
} }
if (entryPoint == null) continue; if (entryPoint == null) continue;
...@@ -257,7 +258,7 @@ class FromGeneric { ...@@ -257,7 +258,7 @@ class FromGeneric {
@Override @Override
public String toString() { public String toString() {
return MethodHandleImpl.addTypeString(target, this); return addTypeString(target, this);
} }
protected boolean isPrototype() { return target == null; } protected boolean isPrototype() { return target == null; }
...@@ -272,7 +273,7 @@ class FromGeneric { ...@@ -272,7 +273,7 @@ class FromGeneric {
protected Adapter(MethodHandle entryPoint, protected Adapter(MethodHandle entryPoint,
MethodHandle invoker, MethodHandle convert, MethodHandle target) { MethodHandle invoker, MethodHandle convert, MethodHandle target) {
super(Access.TOKEN, entryPoint); super(entryPoint);
this.invoker = invoker; this.invoker = invoker;
this.convert = convert; this.convert = convert;
this.target = target; this.target = target;
...@@ -290,7 +291,7 @@ class FromGeneric { ...@@ -290,7 +291,7 @@ class FromGeneric {
protected Object convert_F(float result) throws Throwable { return convert.invokeExact(result); } protected Object convert_F(float result) throws Throwable { return convert.invokeExact(result); }
protected Object convert_D(double result) throws Throwable { return convert.invokeExact(result); } protected Object convert_D(double result) throws Throwable { return convert.invokeExact(result); }
static private final String CLASS_PREFIX; // "sun.dyn.FromGeneric$" static private final String CLASS_PREFIX; // "java.dyn.FromGeneric$"
static { static {
String aname = Adapter.class.getName(); String aname = Adapter.class.getName();
String sname = Adapter.class.getSimpleName(); String sname = Adapter.class.getSimpleName();
......
/* /*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
......
...@@ -23,15 +23,13 @@ ...@@ -23,15 +23,13 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*;
import java.lang.reflect.*;
import sun.dyn.util.*; import sun.dyn.util.*;
import static sun.dyn.MethodTypeImpl.invokers; import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* Adapters which manage MethodHanndle.invokeGeneric calls. * Adapters which manage MethodHandle.invokeGeneric calls.
* The JVM calls one of these when the exact type match fails. * The JVM calls one of these when the exact type match fails.
* @author jrose * @author jrose
*/ */
...@@ -44,7 +42,8 @@ class InvokeGeneric { ...@@ -44,7 +42,8 @@ class InvokeGeneric {
/** Compute and cache information for this adapter, so that it can /** Compute and cache information for this adapter, so that it can
* call out to targets of the erasure-family of the given erased type. * call out to targets of the erasure-family of the given erased type.
*/ */
private InvokeGeneric(MethodType erasedCallerType) throws ReflectiveOperationException { /*non-public*/ InvokeGeneric(MethodType erasedCallerType) throws ReflectiveOperationException {
assert(erasedCallerType.equals(erasedCallerType.erase()));
this.erasedCallerType = erasedCallerType; this.erasedCallerType = erasedCallerType;
this.initialInvoker = makeInitialInvoker(); this.initialInvoker = makeInitialInvoker();
assert initialInvoker.type().equals(erasedCallerType assert initialInvoker.type().equals(erasedCallerType
...@@ -53,22 +52,13 @@ class InvokeGeneric { ...@@ -53,22 +52,13 @@ class InvokeGeneric {
} }
private static MethodHandles.Lookup lookup() { private static MethodHandles.Lookup lookup() {
return MethodHandleImpl.IMPL_LOOKUP; return IMPL_LOOKUP;
} }
/** Return the adapter information for this type's erasure. */ /** Return the adapter information for this type's erasure. */
static MethodHandle genericInvokerOf(MethodType type) { /*non-public*/ static MethodHandle genericInvokerOf(MethodType erasedCallerType) throws ReflectiveOperationException {
MethodTypeImpl form = MethodTypeImpl.of(type); InvokeGeneric gen = new InvokeGeneric(erasedCallerType);
MethodHandle genericInvoker = form.genericInvoker; return gen.initialInvoker;
if (genericInvoker == null) {
try {
InvokeGeneric gen = new InvokeGeneric(form.erasedType());
form.genericInvoker = genericInvoker = gen.initialInvoker;
} catch (ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
}
return genericInvoker;
} }
private MethodHandle makeInitialInvoker() throws ReflectiveOperationException { private MethodHandle makeInitialInvoker() throws ReflectiveOperationException {
...@@ -88,7 +78,7 @@ class InvokeGeneric { ...@@ -88,7 +78,7 @@ class InvokeGeneric {
private MethodHandle makePostDispatchInvoker() { private MethodHandle makePostDispatchInvoker() {
// Take (MH'; MT, MH; A...) and run MH'(MT, MH; A...). // Take (MH'; MT, MH; A...) and run MH'(MT, MH; A...).
MethodType invokerType = erasedCallerType.insertParameterTypes(0, EXTRA_ARGS); MethodType invokerType = erasedCallerType.insertParameterTypes(0, EXTRA_ARGS);
return invokers(invokerType).exactInvoker(); return invokerType.invokers().exactInvoker();
} }
private MethodHandle dropDispatchArguments(MethodHandle targetInvoker) { private MethodHandle dropDispatchArguments(MethodHandle targetInvoker) {
assert(targetInvoker.type().parameterType(0) == MethodHandle.class); assert(targetInvoker.type().parameterType(0) == MethodHandle.class);
...@@ -112,7 +102,7 @@ class InvokeGeneric { ...@@ -112,7 +102,7 @@ class InvokeGeneric {
if (USE_AS_TYPE_PATH || target.isVarargsCollector()) { if (USE_AS_TYPE_PATH || target.isVarargsCollector()) {
MethodHandle newTarget = target.asType(callerType); MethodHandle newTarget = target.asType(callerType);
targetType = callerType; targetType = callerType;
Invokers invokers = MethodTypeImpl.invokers(Access.TOKEN, targetType); Invokers invokers = targetType.invokers();
MethodHandle invoker = invokers.erasedInvokerWithDrops; MethodHandle invoker = invokers.erasedInvokerWithDrops;
if (invoker == null) { if (invoker == null) {
invokers.erasedInvokerWithDrops = invoker = invokers.erasedInvokerWithDrops = invoker =
......
...@@ -23,16 +23,16 @@ ...@@ -23,16 +23,16 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*;
import sun.dyn.empty.Empty; import sun.dyn.empty.Empty;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* Construction and caching of often-used invokers. * Construction and caching of often-used invokers.
* @author jrose * @author jrose
*/ */
public class Invokers { class Invokers {
// exact type (sans leading taget MH) for the outgoing call // exact type (sans leading taget MH) for the outgoing call
private final MethodType targetType; private final MethodType targetType;
...@@ -60,15 +60,15 @@ public class Invokers { ...@@ -60,15 +60,15 @@ public class Invokers {
this.spreadInvokers = new MethodHandle[targetType.parameterCount()+1]; this.spreadInvokers = new MethodHandle[targetType.parameterCount()+1];
} }
public static MethodType invokerType(MethodType targetType) { /*non-public*/ static MethodType invokerType(MethodType targetType) {
return targetType.insertParameterTypes(0, MethodHandle.class); return targetType.insertParameterTypes(0, MethodHandle.class);
} }
public MethodHandle exactInvoker() { /*non-public*/ MethodHandle exactInvoker() {
MethodHandle invoker = exactInvoker; MethodHandle invoker = exactInvoker;
if (invoker != null) return invoker; if (invoker != null) return invoker;
try { try {
invoker = MethodHandleImpl.IMPL_LOOKUP.findVirtual(MethodHandle.class, "invokeExact", targetType); invoker = IMPL_LOOKUP.findVirtual(MethodHandle.class, "invokeExact", targetType);
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
throw new InternalError("JVM cannot find invoker for "+targetType); throw new InternalError("JVM cannot find invoker for "+targetType);
} }
...@@ -77,7 +77,7 @@ public class Invokers { ...@@ -77,7 +77,7 @@ public class Invokers {
return invoker; return invoker;
} }
public MethodHandle genericInvoker() { /*non-public*/ MethodHandle genericInvoker() {
MethodHandle invoker1 = exactInvoker(); MethodHandle invoker1 = exactInvoker();
MethodHandle invoker = genericInvoker; MethodHandle invoker = genericInvoker;
if (invoker != null) return invoker; if (invoker != null) return invoker;
...@@ -87,7 +87,7 @@ public class Invokers { ...@@ -87,7 +87,7 @@ public class Invokers {
return invoker; return invoker;
} }
public MethodHandle erasedInvoker() { /*non-public*/ MethodHandle erasedInvoker() {
MethodHandle invoker1 = exactInvoker(); MethodHandle invoker1 = exactInvoker();
MethodHandle invoker = erasedInvoker; MethodHandle invoker = erasedInvoker;
if (invoker != null) return invoker; if (invoker != null) return invoker;
...@@ -100,7 +100,7 @@ public class Invokers { ...@@ -100,7 +100,7 @@ public class Invokers {
return invoker; return invoker;
} }
public MethodHandle spreadInvoker(int objectArgCount) { /*non-public*/ MethodHandle spreadInvoker(int objectArgCount) {
MethodHandle vaInvoker = spreadInvokers[objectArgCount]; MethodHandle vaInvoker = spreadInvokers[objectArgCount];
if (vaInvoker != null) return vaInvoker; if (vaInvoker != null) return vaInvoker;
MethodHandle gInvoker = genericInvoker(); MethodHandle gInvoker = genericInvoker();
...@@ -111,12 +111,12 @@ public class Invokers { ...@@ -111,12 +111,12 @@ public class Invokers {
private static MethodHandle THROW_UCS = null; private static MethodHandle THROW_UCS = null;
public MethodHandle uninitializedCallSite() { /*non-public*/ MethodHandle uninitializedCallSite() {
MethodHandle invoker = uninitializedCallSite; MethodHandle invoker = uninitializedCallSite;
if (invoker != null) return invoker; if (invoker != null) return invoker;
if (targetType.parameterCount() > 0) { if (targetType.parameterCount() > 0) {
MethodType type0 = targetType.dropParameterTypes(0, targetType.parameterCount()); MethodType type0 = targetType.dropParameterTypes(0, targetType.parameterCount());
Invokers invokers0 = MethodTypeImpl.invokers(type0); Invokers invokers0 = type0.invokers();
invoker = MethodHandles.dropArguments(invokers0.uninitializedCallSite(), invoker = MethodHandles.dropArguments(invokers0.uninitializedCallSite(),
0, targetType.parameterList()); 0, targetType.parameterList());
assert(invoker.type().equals(targetType)); assert(invoker.type().equals(targetType));
...@@ -125,14 +125,14 @@ public class Invokers { ...@@ -125,14 +125,14 @@ public class Invokers {
} }
if (THROW_UCS == null) { if (THROW_UCS == null) {
try { try {
THROW_UCS = MethodHandleImpl.IMPL_LOOKUP THROW_UCS = IMPL_LOOKUP
.findStatic(CallSite.class, "uninitializedCallSite", .findStatic(CallSite.class, "uninitializedCallSite",
MethodType.methodType(Empty.class)); MethodType.methodType(Empty.class));
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }
invoker = AdapterMethodHandle.makeRetypeRaw(Access.TOKEN, targetType, THROW_UCS); invoker = AdapterMethodHandle.makeRetypeRaw(targetType, THROW_UCS);
assert(invoker.type().equals(targetType)); assert(invoker.type().equals(targetType));
uninitializedCallSite = invoker; uninitializedCallSite = invoker;
return invoker; return invoker;
......
...@@ -25,13 +25,9 @@ ...@@ -25,13 +25,9 @@
package java.dyn; package java.dyn;
import java.dyn.MethodHandles.Lookup;
import java.util.WeakHashMap;
import sun.dyn.Access;
import sun.dyn.MethodHandleImpl;
import sun.dyn.util.VerifyAccess; import sun.dyn.util.VerifyAccess;
import java.dyn.MethodHandles.Lookup;
import sun.reflect.Reflection; import sun.reflect.Reflection;
import static sun.dyn.MemberName.newIllegalArgumentException;
/** /**
* <em>CLASS WILL BE REMOVED FOR PFD:</em> * <em>CLASS WILL BE REMOVED FOR PFD:</em>
...@@ -41,8 +37,6 @@ import static sun.dyn.MemberName.newIllegalArgumentException; ...@@ -41,8 +37,6 @@ import static sun.dyn.MemberName.newIllegalArgumentException;
* @deprecated This class will be removed in the Public Final Draft. * @deprecated This class will be removed in the Public Final Draft.
*/ */
public class Linkage { public class Linkage {
private static final Access IMPL_TOKEN = Access.getToken();
private Linkage() {} // do not instantiate private Linkage() {} // do not instantiate
/** /**
...@@ -56,7 +50,7 @@ public class Linkage { ...@@ -56,7 +50,7 @@ public class Linkage {
Class callc = Reflection.getCallerClass(2); Class callc = Reflection.getCallerClass(2);
if (callc != null && !VerifyAccess.isSamePackage(callerClass, callc)) if (callc != null && !VerifyAccess.isSamePackage(callerClass, callc))
throw new IllegalArgumentException("cannot set bootstrap method on "+callerClass); throw new IllegalArgumentException("cannot set bootstrap method on "+callerClass);
MethodHandleImpl.registerBootstrap(IMPL_TOKEN, callerClass, bootstrapMethod); MethodHandleImpl.registerBootstrap(callerClass, bootstrapMethod);
} }
/** /**
...@@ -84,14 +78,14 @@ public class Linkage { ...@@ -84,14 +78,14 @@ public class Linkage {
private static private static
void registerBootstrapMethodLookup(Class<?> callerClass, Class<?> runtime, String name) { void registerBootstrapMethodLookup(Class<?> callerClass, Class<?> runtime, String name) {
Lookup lookup = new Lookup(IMPL_TOKEN, callerClass); Lookup lookup = new Lookup(callerClass);
MethodHandle bootstrapMethod; MethodHandle bootstrapMethod;
try { try {
bootstrapMethod = lookup.findStatic(runtime, name, BOOTSTRAP_METHOD_TYPE); bootstrapMethod = lookup.findStatic(runtime, name, BOOTSTRAP_METHOD_TYPE);
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
throw new IllegalArgumentException("no such bootstrap method in "+runtime+": "+name, ex); throw new IllegalArgumentException("no such bootstrap method in "+runtime+": "+name, ex);
} }
MethodHandleImpl.registerBootstrap(IMPL_TOKEN, callerClass, bootstrapMethod); MethodHandleImpl.registerBootstrap(callerClass, bootstrapMethod);
} }
private static final MethodType BOOTSTRAP_METHOD_TYPE private static final MethodType BOOTSTRAP_METHOD_TYPE
......
...@@ -23,10 +23,9 @@ ...@@ -23,10 +23,9 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import sun.dyn.util.BytecodeDescriptor; import sun.dyn.util.BytecodeDescriptor;
import java.dyn.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
...@@ -37,7 +36,8 @@ import java.util.Arrays; ...@@ -37,7 +36,8 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import static sun.dyn.MethodHandleNatives.Constants.*; import static java.dyn.MethodHandleNatives.Constants.*;
import static java.dyn.MethodHandleStatics.*;
/** /**
* A {@code MemberName} is a compact symbolic datum which fully characterizes * A {@code MemberName} is a compact symbolic datum which fully characterizes
...@@ -66,7 +66,7 @@ import static sun.dyn.MethodHandleNatives.Constants.*; ...@@ -66,7 +66,7 @@ import static sun.dyn.MethodHandleNatives.Constants.*;
* and those seven fields omit much of the information in Method. * and those seven fields omit much of the information in Method.
* @author jrose * @author jrose
*/ */
public final class MemberName implements Member, Cloneable { /*non-public*/ final class MemberName implements Member, Cloneable {
private Class<?> clazz; // class in which the method is defined private Class<?> clazz; // class in which the method is defined
private String name; // may be null if not yet materialized private String name; // may be null if not yet materialized
private Object type; // may be null if not yet materialized private Object type; // may be null if not yet materialized
...@@ -435,7 +435,7 @@ public final class MemberName implements Member, Cloneable { ...@@ -435,7 +435,7 @@ public final class MemberName implements Member, Cloneable {
/** Query whether this member name is resolved to a non-static, non-final method. /** Query whether this member name is resolved to a non-static, non-final method.
*/ */
public boolean hasReceiverTypeDispatch() { public boolean hasReceiverTypeDispatch() {
return (isMethod() && getVMIndex(Access.TOKEN) >= 0); return (isMethod() && getVMIndex() >= 0);
} }
/** Produce a string form of this member name. /** Produce a string form of this member name.
...@@ -490,59 +490,38 @@ public final class MemberName implements Member, Cloneable { ...@@ -490,59 +490,38 @@ public final class MemberName implements Member, Cloneable {
// Queries to the JVM: // Queries to the JVM:
/** Document? */ /** Document? */
public int getVMIndex(Access token) { /*non-public*/ int getVMIndex() {
Access.check(token);
if (!isResolved()) if (!isResolved())
throw newIllegalStateException("not resolved"); throw newIllegalStateException("not resolved", this);
return vmindex; return vmindex;
} }
// public Object getVMTarget(Access token) { // /*non-public*/ Object getVMTarget() {
// Access.check(token);
// if (!isResolved()) // if (!isResolved())
// throw newIllegalStateException("not resolved"); // throw newIllegalStateException("not resolved", this);
// return vmtarget; // return vmtarget;
// } // }
private RuntimeException newIllegalStateException(String message) {
return new IllegalStateException(message+": "+this);
}
// handy shared exception makers (they simplify the common case code) public IllegalAccessException makeAccessException(String message, Object from) {
public static RuntimeException newIllegalArgumentException(String message) { message = message + ": "+ toString();
return new IllegalArgumentException(message);
}
public static IllegalAccessException newNoAccessException(MemberName name, Object from) {
return newNoAccessException("cannot access", name, from);
}
public static IllegalAccessException newNoAccessException(String message,
MemberName name, Object from) {
message += ": " + name;
if (from != null) message += ", from " + from; if (from != null) message += ", from " + from;
return new IllegalAccessException(message); return new IllegalAccessException(message);
} }
public static ReflectiveOperationException newNoAccessException(MemberName name) { public ReflectiveOperationException makeAccessException(String message) {
if (name.isResolved()) message = message + ": "+ toString();
return new IllegalAccessException(name.toString()); if (isResolved())
else if (name.isConstructor()) return new IllegalAccessException(message);
return new NoSuchMethodException(name.toString()); else if (isConstructor())
else if (name.isMethod()) return new NoSuchMethodException(message);
return new NoSuchMethodException(name.toString()); else if (isMethod())
return new NoSuchMethodException(message);
else else
return new NoSuchFieldException(name.toString()); return new NoSuchFieldException(message);
}
public static Error uncaughtException(Exception ex) {
Error err = new InternalError("uncaught exception");
err.initCause(ex);
return err;
} }
/** Actually making a query requires an access check. */ /** Actually making a query requires an access check. */
public static Factory getFactory(Access token) { /*non-public*/ static Factory getFactory() {
Access.check(token);
return Factory.INSTANCE; return Factory.INSTANCE;
} }
public static Factory getFactory() {
return getFactory(Access.getToken());
}
/** A factory type for resolving member names with the help of the VM. /** A factory type for resolving member names with the help of the VM.
* TBD: Define access-safe public constructors for this factory. * TBD: Define access-safe public constructors for this factory.
*/ */
...@@ -662,7 +641,7 @@ public final class MemberName implements Member, Cloneable { ...@@ -662,7 +641,7 @@ public final class MemberName implements Member, Cloneable {
MemberName result = resolveOrNull(m, searchSupers, lookupClass); MemberName result = resolveOrNull(m, searchSupers, lookupClass);
if (result != null) if (result != null)
return result; return result;
ReflectiveOperationException ex = newNoAccessException(m); ReflectiveOperationException ex = m.makeAccessException("no access");
if (ex instanceof IllegalAccessException) throw (IllegalAccessException) ex; if (ex instanceof IllegalAccessException) throw (IllegalAccessException) ex;
throw nsmClass.cast(ex); throw nsmClass.cast(ex);
} }
......
...@@ -25,13 +25,8 @@ ...@@ -25,13 +25,8 @@
package java.dyn; package java.dyn;
//import sun.dyn.*;
import sun.dyn.Access; import static java.dyn.MethodHandleStatics.*;
import sun.dyn.MethodHandleImpl;
import static java.dyn.MethodHandles.invokers; // package-private API
import static sun.dyn.MemberName.newIllegalArgumentException; // utility
/** /**
* A method handle is a typed, directly executable reference to an underlying method, * A method handle is a typed, directly executable reference to an underlying method,
...@@ -436,12 +431,35 @@ mh.invokeExact(System.out, "Hello, world."); ...@@ -436,12 +431,35 @@ mh.invokeExact(System.out, "Hello, world.");
* @see MethodHandles * @see MethodHandles
* @author John Rose, JSR 292 EG * @author John Rose, JSR 292 EG
*/ */
public abstract class MethodHandle public abstract class MethodHandle {
// Note: This is an implementation inheritance hack, and will be removed // { JVM internals:
// with a JVM change which moves the required hidden state onto this class.
extends MethodHandleImpl private byte vmentry; // adapter stub or method entry point
{ //private int vmslots; // optionally, hoist type.form.vmslots
private static Access IMPL_TOKEN = Access.getToken(); /*non-public*/ Object vmtarget; // VM-specific, class-specific target value
// TO DO: vmtarget should be invisible to Java, since the JVM puts internal
// managed pointers into it. Making it visible exposes it to debuggers,
// which can cause errors when they treat the pointer as an Object.
// These two dummy fields are present to force 'I' and 'J' signatures
// into this class's constant pool, so they can be transferred
// to vmentry when this class is loaded.
static final int INT_FIELD = 0;
static final long LONG_FIELD = 0;
// vmentry (a void* field) is used *only* by the JVM.
// The JVM adjusts its type to int or long depending on system wordsize.
// Since it is statically typed as neither int nor long, it is impossible
// to use this field from Java bytecode. (Please don't try to, either.)
// The vmentry is an assembly-language stub which is jumped to
// immediately after the method type is verified.
// For a direct MH, this stub loads the vmtarget's entry point
// and jumps to it.
// } End of JVM internals.
static { MethodHandleImpl.initStatics(); } static { MethodHandleImpl.initStatics(); }
// interface MethodHandle<R throws X extends Exception,A...> // interface MethodHandle<R throws X extends Exception,A...>
...@@ -467,37 +485,16 @@ public abstract class MethodHandle ...@@ -467,37 +485,16 @@ public abstract class MethodHandle
} }
/** /**
* <em>CONSTRUCTOR WILL BE REMOVED FOR PFD:</em> * Package-private constructor for the method handle implementation hierarchy.
* Temporary constructor in early versions of the Reference Implementation. * Method handle inheritance will be contained completely within
* Method handle inheritance (if any) will be contained completely within
* the {@code java.dyn} package. * the {@code java.dyn} package.
*/ */
// The constructor for MethodHandle may only be called by privileged code.
// Subclasses may be in other packages, but must possess
// a token which they obtained from MH with a security check.
// @param token non-null object which proves access permission
// @param type type (permanently assigned) of the new method handle // @param type type (permanently assigned) of the new method handle
protected MethodHandle(Access token, MethodType type) { /*non-public*/ MethodHandle(MethodType type) {
super(token);
Access.check(token);
this.type = type;
}
private void initType(MethodType type) {
type.getClass(); // elicit NPE type.getClass(); // elicit NPE
if (this.type != null) throw new InternalError();
this.type = type; this.type = type;
} }
static {
// This hack allows the implementation package special access to
// the internals of MethodHandle. In particular, the MTImpl has all sorts
// of cached information useful to the implementation code.
MethodHandleImpl.setMethodHandleFriend(IMPL_TOKEN, new MethodHandleImpl.MethodHandleFriend() {
public void initType(MethodHandle mh, MethodType type) { mh.initType(type); }
});
}
/** /**
* Invoke the method handle, allowing any caller type descriptor, but requiring an exact type match. * Invoke the method handle, allowing any caller type descriptor, but requiring an exact type match.
* The type descriptor at the call site of {@code invokeExact} must * The type descriptor at the call site of {@code invokeExact} must
...@@ -608,7 +605,7 @@ public abstract class MethodHandle ...@@ -608,7 +605,7 @@ public abstract class MethodHandle
return asType(MethodType.genericMethodType(argc)).invokeWithArguments(arguments); return asType(MethodType.genericMethodType(argc)).invokeWithArguments(arguments);
} }
if (argc <= 10) { if (argc <= 10) {
MethodHandle invoker = invokers(type).genericInvoker(); MethodHandle invoker = type.invokers().genericInvoker();
switch (argc) { switch (argc) {
case 0: return invoker.invokeExact(this); case 0: return invoker.invokeExact(this);
case 1: return invoker.invokeExact(this, case 1: return invoker.invokeExact(this,
...@@ -647,7 +644,7 @@ public abstract class MethodHandle ...@@ -647,7 +644,7 @@ public abstract class MethodHandle
} }
// more than ten arguments get boxed in a varargs list: // more than ten arguments get boxed in a varargs list:
MethodHandle invoker = invokers(type).spreadInvoker(0); MethodHandle invoker = type.invokers().spreadInvoker(0);
return invoker.invokeExact(this, arguments); return invoker.invokeExact(this, arguments);
} }
/** Equivalent to {@code invokeWithArguments(arguments.toArray())}. */ /** Equivalent to {@code invokeWithArguments(arguments.toArray())}. */
...@@ -1004,6 +1001,6 @@ assert(failed); ...@@ -1004,6 +1001,6 @@ assert(failed);
*/ */
@Override @Override
public String toString() { public String toString() {
return MethodHandleImpl.getNameString(IMPL_TOKEN, this); return getNameString(this);
} }
} }
/* /*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,14 +23,13 @@ ...@@ -23,14 +23,13 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*;
import java.dyn.MethodHandles.Lookup; import java.dyn.MethodHandles.Lookup;
import java.lang.reflect.AccessibleObject; import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import static sun.dyn.MethodHandleNatives.Constants.*; import static java.dyn.MethodHandleNatives.Constants.*;
import static sun.dyn.MethodHandleImpl.IMPL_LOOKUP; import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* The JVM interface for the method handles package is all here. * The JVM interface for the method handles package is all here.
...@@ -81,14 +80,12 @@ class MethodHandleNatives { ...@@ -81,14 +80,12 @@ class MethodHandleNatives {
* This routine is for debugging and reflection. * This routine is for debugging and reflection.
*/ */
static MemberName getMethodName(MethodHandle self) { static MemberName getMethodName(MethodHandle self) {
if (!JVM_SUPPORT) return null;
return (MemberName) getTarget(self, ETF_METHOD_NAME); return (MemberName) getTarget(self, ETF_METHOD_NAME);
} }
/** Fetch the reflective version of the handled method, if available. /** Fetch the reflective version of the handled method, if available.
*/ */
static AccessibleObject getTargetMethod(MethodHandle self) { static AccessibleObject getTargetMethod(MethodHandle self) {
if (!JVM_SUPPORT) return null;
return (AccessibleObject) getTarget(self, ETF_REFLECT_METHOD); return (AccessibleObject) getTarget(self, ETF_REFLECT_METHOD);
} }
...@@ -97,7 +94,6 @@ class MethodHandleNatives { ...@@ -97,7 +94,6 @@ class MethodHandleNatives {
* If it is chained to another method handle, return that handle. * If it is chained to another method handle, return that handle.
*/ */
static Object getTargetInfo(MethodHandle self) { static Object getTargetInfo(MethodHandle self) {
if (!JVM_SUPPORT) return null;
return getTarget(self, ETF_HANDLE_OR_METHOD_NAME); return getTarget(self, ETF_HANDLE_OR_METHOD_NAME);
} }
...@@ -111,11 +107,6 @@ class MethodHandleNatives { ...@@ -111,11 +107,6 @@ class MethodHandleNatives {
*/ */
static native int getConstant(int which); static native int getConstant(int which);
/** True iff this HotSpot JVM has built-in support for method handles.
* If false, some test cases might run, but functionality will be missing.
*/
public static final boolean JVM_SUPPORT;
/** Java copy of MethodHandlePushLimit in range 2..255. */ /** Java copy of MethodHandlePushLimit in range 2..255. */
static final int JVM_PUSH_LIMIT; static final int JVM_PUSH_LIMIT;
/** JVM stack motion (in words) after one slot is pushed, usually -1. /** JVM stack motion (in words) after one slot is pushed, usually -1.
...@@ -127,31 +118,24 @@ class MethodHandleNatives { ...@@ -127,31 +118,24 @@ class MethodHandleNatives {
private static native void registerNatives(); private static native void registerNatives();
static { static {
boolean JVM_SUPPORT_;
int JVM_PUSH_LIMIT_; int JVM_PUSH_LIMIT_;
int JVM_STACK_MOVE_UNIT_; int JVM_STACK_MOVE_UNIT_;
int CONV_OP_IMPLEMENTED_MASK_; int CONV_OP_IMPLEMENTED_MASK_;
try { try {
registerNatives(); registerNatives();
JVM_SUPPORT_ = true;
JVM_PUSH_LIMIT_ = getConstant(Constants.GC_JVM_PUSH_LIMIT); JVM_PUSH_LIMIT_ = getConstant(Constants.GC_JVM_PUSH_LIMIT);
JVM_STACK_MOVE_UNIT_ = getConstant(Constants.GC_JVM_STACK_MOVE_UNIT); JVM_STACK_MOVE_UNIT_ = getConstant(Constants.GC_JVM_STACK_MOVE_UNIT);
CONV_OP_IMPLEMENTED_MASK_ = getConstant(Constants.GC_CONV_OP_IMPLEMENTED_MASK); CONV_OP_IMPLEMENTED_MASK_ = getConstant(Constants.GC_CONV_OP_IMPLEMENTED_MASK);
//sun.reflect.Reflection.registerMethodsToFilter(MethodHandleImpl.class, "init"); //sun.reflect.Reflection.registerMethodsToFilter(MethodHandleImpl.class, "init");
} catch (UnsatisfiedLinkError ee) { } catch (UnsatisfiedLinkError ee) {
// ignore; if we use init() methods later we'll see linkage errors // ignore; if we use init() methods later we'll see linkage errors
JVM_SUPPORT_ = false;
JVM_PUSH_LIMIT_ = 3; // arbitrary JVM_PUSH_LIMIT_ = 3; // arbitrary
JVM_STACK_MOVE_UNIT_ = -1; // arbitrary JVM_STACK_MOVE_UNIT_ = -1; // arbitrary
CONV_OP_IMPLEMENTED_MASK_ = 0; CONV_OP_IMPLEMENTED_MASK_ = 0;
//System.out.println("Warning: Running with JVM_SUPPORT=false");
//System.out.println(ee);
JVM_SUPPORT = JVM_SUPPORT_;
JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_; JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_;
JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_; JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_;
throw ee; // just die; hopeless to try to run with an older JVM throw ee; // just die; hopeless to try to run with an older JVM
} }
JVM_SUPPORT = JVM_SUPPORT_;
JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_; JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_;
JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_; JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_;
if (CONV_OP_IMPLEMENTED_MASK_ == 0) if (CONV_OP_IMPLEMENTED_MASK_ == 0)
...@@ -191,7 +175,7 @@ class MethodHandleNatives { ...@@ -191,7 +175,7 @@ class MethodHandleNatives {
// AdapterMethodHandle // AdapterMethodHandle
/** Conversions recognized by the JVM. /** Conversions recognized by the JVM.
* They must align with the constants in sun.dyn_AdapterMethodHandle, * They must align with the constants in java.dyn_AdapterMethodHandle,
* in the JVM file hotspot/src/share/vm/classfile/javaClasses.hpp. * in the JVM file hotspot/src/share/vm/classfile/javaClasses.hpp.
*/ */
static final int static final int
...@@ -292,7 +276,7 @@ class MethodHandleNatives { ...@@ -292,7 +276,7 @@ class MethodHandleNatives {
return true; return true;
} }
static { static {
if (JVM_SUPPORT) verifyConstants(); verifyConstants();
} }
// Up-calls from the JVM. // Up-calls from the JVM.
...@@ -305,28 +289,47 @@ class MethodHandleNatives { ...@@ -305,28 +289,47 @@ class MethodHandleNatives {
String name, MethodType type, String name, MethodType type,
Object info, Object info,
MemberName callerMethod, int callerBCI) { MemberName callerMethod, int callerBCI) {
return CallSiteImpl.makeSite(bootstrapMethod, name, type, info, callerMethod, callerBCI); return CallSite.makeSite(bootstrapMethod, name, type, info, callerMethod, callerBCI);
}
/**
* Called by the JVM to check the length of a spread array.
*/
static void checkSpreadArgument(Object av, int n) {
MethodHandleStatics.checkSpreadArgument(av, n);
} }
/** /**
* The JVM wants a pointer to a MethodType. Oblige it by finding or creating one. * The JVM wants a pointer to a MethodType. Oblige it by finding or creating one.
*/ */
static MethodType findMethodHandleType(Class<?> rtype, Class<?>[] ptypes) { static MethodType findMethodHandleType(Class<?> rtype, Class<?>[] ptypes) {
MethodType.genericMethodType(0); // trigger initialization return MethodType.makeImpl(rtype, ptypes, true);
return MethodTypeImpl.makeImpl(Access.TOKEN, rtype, ptypes, true);
} }
/** /**
* The JVM wants to use a MethodType with invokeGeneric. Give the runtime fair warning. * The JVM wants to use a MethodType with invokeGeneric. Give the runtime fair warning.
*/ */
static void notifyGenericMethodType(MethodType type) { static void notifyGenericMethodType(MethodType type) {
try { type.form().notifyGenericMethodType();
// Trigger adapter creation. }
InvokeGeneric.genericInvokerOf(type);
} catch (Exception ex) { /**
Error err = new InternalError("Exception while resolving invokeGeneric"); * The JVM wants to raise an exception. Here's the path.
err.initCause(ex); */
throw err; 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);
} }
} }
......
/* /*
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,87 +23,70 @@ ...@@ -23,87 +23,70 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import sun.reflect.Reflection;
/** /**
* Access control to this package. * This class consists exclusively of static names internal to the
* Classes in other packages can attempt to acquire the access token, * method handle implementation.
* but will fail if they are not recognized as friends. * Usage: {@code import static java.dyn.MethodHandleStatics.*}
* Certain methods in this package, although public, require a non-null * @author John Rose, JSR 292 EG
* access token in order to proceed; they act like package-private methods.
* @author jrose
*/ */
/*non-public*/ class MethodHandleStatics {
public class Access { private MethodHandleStatics() { } // do not instantiate
private Access() { }
/**
* The heart of this pattern: The list of classes which are
* permitted to acquire the access token, and become honorary
* members of this package.
*/
private static final String[] FRIENDS = {
"java.dyn.", "sun.dyn."
};
/** /*non-public*/ static String getNameString(MethodHandle target, MethodType type) {
* The following object is NOT public. That's the point of the pattern. if (type == null)
* It is package-private, so that any member of this package type = target.type();
* can acquire the access token, and give it away to trusted friends. MemberName name = null;
*/ if (target != null)
static final Access TOKEN = new Access(); name = MethodHandleNatives.getMethodName(target);
if (name == null)
return "invoke" + type;
return name.getName() + type;
}
/** /*non-public*/ static String getNameString(MethodHandle target, MethodHandle typeHolder) {
* @return Access.TOKEN, if the caller is a friend of this package return getNameString(target, typeHolder == null ? (MethodType) null : typeHolder.type());
*/
public static Access getToken() {
Class<?> callc = Reflection.getCallerClass(2);
if (isFriend(callc))
return TOKEN;
else
throw new IllegalAccessError("bad caller: " + callc);
} }
/** Is the given name the name of a class which could be our friend? */ /*non-public*/ static String getNameString(MethodHandle target) {
public static boolean isFriendName(String name) { return getNameString(target, (MethodType) null);
for (String friend : FRIENDS) {
if (name.startsWith(friend))
return true;
}
return false;
} }
/** Is the given class a friend? True if {@link #isFriendName}, /*non-public*/ static String addTypeString(Object obj, MethodHandle target) {
* and the given class also shares a class loader with us. String str = String.valueOf(obj);
*/ if (target == null) return str;
public static boolean isFriend(Class<?> c) { int paren = str.indexOf('(');
return isFriendName(c.getName()) && c.getClassLoader() == CLASS_LOADER; if (paren >= 0) str = str.substring(0, paren);
return str + target.type();
} }
private static final ClassLoader CLASS_LOADER = Access.class.getClassLoader(); static void checkSpreadArgument(Object av, int n) {
if (av == null ? n != 0 : ((Object[])av).length != n)
throw newIllegalArgumentException("Array is not of length "+n);
}
/** // handy shared exception makers (they simplify the common case code)
* Throw an IllegalAccessError if the caller does not possess /*non-public*/ static RuntimeException newIllegalStateException(String message) {
* the Access.TOKEN. return new IllegalStateException(message);
* @param must be Access.TOKEN
*/
public static void check(Access token) {
if (token == null)
fail();
// else it must be the unique Access.TOKEN
assert(token == Access.TOKEN);
} }
private static void fail() { /*non-public*/ static RuntimeException newIllegalStateException(String message, Object obj) {
final int CALLER_DEPTH = 3; return new IllegalStateException(message(message, obj));
// 0: Reflection.getCC, 1: this.fail, 2: Access.*, 3: caller
Class<?> callc = Reflection.getCallerClass(CALLER_DEPTH);
throw new IllegalAccessError("bad caller: " + callc);
} }
/*non-public*/ static RuntimeException newIllegalArgumentException(String message) {
static { return new IllegalArgumentException(message);
//sun.reflect.Reflection.registerMethodsToFilter(MH.class, "getToken"); }
/*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj) {
return new IllegalArgumentException(message(message, obj));
}
/*non-public*/ static Error uncaughtException(Exception ex) {
Error err = new InternalError("uncaught exception");
err.initCause(ex);
return err;
}
private static String message(String message, Object obj) {
if (obj != null) message = message + ": " + obj;
return message;
} }
} }
...@@ -29,12 +29,8 @@ import java.util.Arrays; ...@@ -29,12 +29,8 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import sun.dyn.Access;
import sun.dyn.Invokers;
import sun.dyn.MethodHandleImpl;
import sun.dyn.MethodTypeImpl;
import sun.dyn.util.BytecodeDescriptor; import sun.dyn.util.BytecodeDescriptor;
import static sun.dyn.MemberName.newIllegalArgumentException; import static java.dyn.MethodHandleStatics.*;
/** /**
* A method type represents the arguments and return type accepted and * A method type represents the arguments and return type accepted and
...@@ -96,34 +92,6 @@ class MethodType implements java.io.Serializable { ...@@ -96,34 +92,6 @@ class MethodType implements java.io.Serializable {
private MethodType wrapAlt; // alternative wrapped/unwrapped version private MethodType wrapAlt; // alternative wrapped/unwrapped version
private Invokers invokers; // cache of handy higher-order adapters private Invokers invokers; // cache of handy higher-order adapters
private static final Access IMPL_TOKEN = Access.getToken();
// share a cache with a friend in this package
Invokers getInvokers() { return invokers; }
void setInvokers(Invokers inv) { invokers = inv; }
static {
// This hack allows the implementation package special access to
// the internals of MethodType. In particular, the MTImpl has all sorts
// of cached information useful to the implementation code.
MethodTypeImpl.setMethodTypeFriend(IMPL_TOKEN, new MethodTypeImpl.MethodTypeFriend() {
public Class<?>[] ptypes(MethodType mt) { return mt.ptypes; }
public MethodTypeImpl form(MethodType mt) { return mt.form; }
public void setForm(MethodType mt, MethodTypeImpl form) {
assert(mt.form == null);
mt.form = (MethodTypeForm) form;
}
public MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
return MethodType.makeImpl(rtype, ptypes, trusted);
}
public MethodTypeImpl newMethodTypeForm(MethodType mt) {
return new MethodTypeForm(mt);
}
public Invokers getInvokers(MethodType mt) { return mt.invokers; }
public void setInvokers(MethodType mt, Invokers inv) { mt.invokers = inv; }
});
}
/** /**
* Check the given parameters for validity and store them into the final fields. * Check the given parameters for validity and store them into the final fields.
*/ */
...@@ -134,6 +102,10 @@ class MethodType implements java.io.Serializable { ...@@ -134,6 +102,10 @@ class MethodType implements java.io.Serializable {
this.ptypes = ptypes; this.ptypes = ptypes;
} }
/*trusted*/ MethodTypeForm form() { return form; }
/*trusted*/ Class<?> rtype() { return rtype; }
/*trusted*/ Class<?>[] ptypes() { return ptypes; }
private static void checkRtype(Class<?> rtype) { private static void checkRtype(Class<?> rtype) {
rtype.equals(rtype); // null check rtype.equals(rtype); // null check
} }
...@@ -253,7 +225,7 @@ class MethodType implements java.io.Serializable { ...@@ -253,7 +225,7 @@ class MethodType implements java.io.Serializable {
* @param trusted whether the ptypes can be used without cloning * @param trusted whether the ptypes can be used without cloning
* @return the unique method type of the desired structure * @return the unique method type of the desired structure
*/ */
private static /*trusted*/ static
MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes, boolean trusted) { MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
if (ptypes == null || ptypes.length == 0) { if (ptypes == null || ptypes.length == 0) {
ptypes = NO_PTYPES; trusted = true; ptypes = NO_PTYPES; trusted = true;
...@@ -269,7 +241,12 @@ class MethodType implements java.io.Serializable { ...@@ -269,7 +241,12 @@ class MethodType implements java.io.Serializable {
// defensively copy the array passed in by the user // defensively copy the array passed in by the user
mt1 = new MethodType(rtype, ptypes.clone()); mt1 = new MethodType(rtype, ptypes.clone());
// promote the object to the Real Thing, and reprobe // promote the object to the Real Thing, and reprobe
MethodTypeImpl.initForm(IMPL_TOKEN, mt1); MethodTypeForm form = MethodTypeForm.findForm(mt1);
mt1.form = form;
if (form.erasedType == mt1) {
// This is a principal (erased) type; show it to the JVM.
MethodHandleNatives.init(mt1);
}
synchronized (internTable) { synchronized (internTable) {
mt0 = internTable.get(mt1); mt0 = internTable.get(mt1);
if (mt0 != null) if (mt0 != null)
...@@ -279,12 +256,6 @@ class MethodType implements java.io.Serializable { ...@@ -279,12 +256,6 @@ class MethodType implements java.io.Serializable {
return mt1; return mt1;
} }
// Entry point from JVM. TODO: Change the name & signature.
private static MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes,
boolean ignore1, boolean ignore2) {
return makeImpl(rtype, ptypes, true);
}
private static final MethodType[] objectOnlyTypes = new MethodType[20]; private static final MethodType[] objectOnlyTypes = new MethodType[20];
/** /**
...@@ -535,7 +506,7 @@ class MethodType implements java.io.Serializable { ...@@ -535,7 +506,7 @@ class MethodType implements java.io.Serializable {
MethodType wt = pt.wrapAlt; MethodType wt = pt.wrapAlt;
if (wt == null) { if (wt == null) {
// fill in lazily // fill in lazily
wt = MethodTypeImpl.canonicalize(pt, MethodTypeImpl.WRAP, MethodTypeImpl.WRAP); wt = MethodTypeForm.canonicalize(pt, MethodTypeForm.WRAP, MethodTypeForm.WRAP);
assert(wt != null); assert(wt != null);
pt.wrapAlt = wt; pt.wrapAlt = wt;
} }
...@@ -547,7 +518,7 @@ class MethodType implements java.io.Serializable { ...@@ -547,7 +518,7 @@ class MethodType implements java.io.Serializable {
MethodType uwt = wt.wrapAlt; MethodType uwt = wt.wrapAlt;
if (uwt == null) { if (uwt == null) {
// fill in lazily // fill in lazily
uwt = MethodTypeImpl.canonicalize(wt, MethodTypeImpl.UNWRAP, MethodTypeImpl.UNWRAP); uwt = MethodTypeForm.canonicalize(wt, MethodTypeForm.UNWRAP, MethodTypeForm.UNWRAP);
if (uwt == null) if (uwt == null)
uwt = wt; // type has no wrappers or prims at all uwt = wt; // type has no wrappers or prims at all
wt.wrapAlt = uwt; wt.wrapAlt = uwt;
...@@ -666,12 +637,18 @@ class MethodType implements java.io.Serializable { ...@@ -666,12 +637,18 @@ class MethodType implements java.io.Serializable {
* This method is included for the benfit of applications that must * This method is included for the benfit of applications that must
* generate bytecodes that process method handles and invokedynamic. * generate bytecodes that process method handles and invokedynamic.
* @return the number of JVM stack slots for this type's parameters * @return the number of JVM stack slots for this type's parameters
* @deprecated Will be removed for PFD.
*/ */
public int parameterSlotCount() { /*non-public*/ int parameterSlotCount() {
return form.parameterSlotCount(); return form.parameterSlotCount();
} }
/*non-public*/ Invokers invokers() {
Invokers inv = invokers;
if (inv != null) return inv;
invokers = inv = new Invokers(this);
return inv;
}
/** Reports the number of JVM stack slots which carry all parameters including and after /** Reports the number of JVM stack slots which carry all parameters including and after
* the given position, which must be in the range of 0 to * the given position, which must be in the range of 0 to
* {@code parameterCount} inclusive. Successive parameters are * {@code parameterCount} inclusive. Successive parameters are
...@@ -694,9 +671,8 @@ class MethodType implements java.io.Serializable { ...@@ -694,9 +671,8 @@ class MethodType implements java.io.Serializable {
* @return the index of the (shallowest) JVM stack slot transmitting the * @return the index of the (shallowest) JVM stack slot transmitting the
* given parameter * given parameter
* @throws IllegalArgumentException if {@code num} is negative or greater than {@code parameterCount()} * @throws IllegalArgumentException if {@code num} is negative or greater than {@code parameterCount()}
* @deprecated Will be removed for PFD.
*/ */
public int parameterSlotDepth(int num) { /*non-public*/ int parameterSlotDepth(int num) {
if (num < 0 || num > ptypes.length) if (num < 0 || num > ptypes.length)
parameterType(num); // force a range check parameterType(num); // force a range check
return form.parameterToArgSlot(num-1); return form.parameterToArgSlot(num-1);
...@@ -710,9 +686,9 @@ class MethodType implements java.io.Serializable { ...@@ -710,9 +686,9 @@ class MethodType implements java.io.Serializable {
* This method is included for the benfit of applications that must * This method is included for the benfit of applications that must
* generate bytecodes that process method handles and invokedynamic. * generate bytecodes that process method handles and invokedynamic.
* @return the number of JVM stack slots (0, 1, or 2) for this type's return value * @return the number of JVM stack slots (0, 1, or 2) for this type's return value
* @deprecated Will be removed for PFD. * Will be removed for PFD.
*/ */
public int returnSlotCount() { /*non-public*/ int returnSlotCount() {
return form.returnSlotCount(); return form.returnSlotCount();
} }
......
/* /*
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,15 +25,452 @@ ...@@ -25,15 +25,452 @@
package java.dyn; package java.dyn;
import sun.dyn.util.Wrapper;
import static java.dyn.MethodHandleStatics.*;
/** /**
* TO DO: Temporary shim; remove after refactoring effects are complete in JVM. * Shared information for a group of method types, which differ
* only by reference types, and therefore share a common erasure
* and wrapping.
* <p>
* For an empirical discussion of the structure of method types,
* see <a href="http://groups.google.com/group/jvm-languages/browse_thread/thread/ac9308ae74da9b7e/">
* the thread "Avoiding Boxing" on jvm-languages</a>.
* There are approximately 2000 distinct erased method types in the JDK.
* There are a little over 10 times that number of unerased types.
* No more than half of these are likely to be loaded at once.
* @author John Rose * @author John Rose
*/ */
import sun.dyn.MethodTypeImpl; class MethodTypeForm {
final int[] argToSlotTable, slotToArgTable;
final long argCounts; // packed slot & value counts
final long primCounts; // packed prim & double counts
final int vmslots; // total number of parameter slots
final MethodType erasedType; // the canonical erasure
/*lazy*/ MethodType primsAsBoxes; // replace prims by wrappers
/*lazy*/ MethodType primArgsAsBoxes; // wrap args only; make raw return
/*lazy*/ MethodType primsAsInts; // replace prims by int/long
/*lazy*/ MethodType primsAsLongs; // replace prims by long
/*lazy*/ MethodType primsAtEnd; // reorder primitives to the end
// Cached adapter information:
/*lazy*/ ToGeneric toGeneric; // convert cs. with prims to w/o
/*lazy*/ FromGeneric fromGeneric; // convert cs. w/o prims to with
/*lazy*/ SpreadGeneric[] spreadGeneric; // expand one argument to many
/*lazy*/ FilterGeneric filterGeneric; // convert argument(s) on the fly
/*lazy*/ MethodHandle genericInvoker; // hook for invokeGeneric
public MethodType erasedType() {
return erasedType;
}
protected MethodTypeForm(MethodType erasedType) {
this.erasedType = erasedType;
Class<?>[] ptypes = erasedType.ptypes();
int ptypeCount = ptypes.length;
int pslotCount = ptypeCount; // temp. estimate
int rtypeCount = 1; // temp. estimate
int rslotCount = 1; // temp. estimate
int[] argToSlotTab = null, slotToArgTab = null;
// Walk the argument types, looking for primitives.
int pac = 0, lac = 0, prc = 0, lrc = 0;
Class<?> epts[] = ptypes;
for (int i = 0; i < epts.length; i++) {
Class<?> pt = epts[i];
if (pt != Object.class) {
assert(pt.isPrimitive());
++pac;
if (hasTwoArgSlots(pt)) ++lac;
}
}
pslotCount += lac; // #slots = #args + #longs
Class<?> rt = erasedType.returnType();
if (rt != Object.class) {
++prc; // even void.class counts as a prim here
if (hasTwoArgSlots(rt)) ++lrc;
// adjust #slots, #args
if (rt == void.class)
rtypeCount = rslotCount = 0;
else
rslotCount += lrc;
}
if (lac != 0) {
int slot = ptypeCount + lac;
slotToArgTab = new int[slot+1];
argToSlotTab = new int[1+ptypeCount];
argToSlotTab[0] = slot; // argument "-1" is past end of slots
for (int i = 0; i < epts.length; i++) {
Class<?> pt = epts[i];
if (hasTwoArgSlots(pt)) --slot;
--slot;
slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
argToSlotTab[1+i] = slot;
}
assert(slot == 0); // filled the table
}
this.primCounts = pack(lrc, prc, lac, pac);
this.argCounts = pack(rslotCount, rtypeCount, pslotCount, ptypeCount);
if (slotToArgTab == null) {
int slot = ptypeCount; // first arg is deepest in stack
slotToArgTab = new int[slot+1];
argToSlotTab = new int[1+ptypeCount];
argToSlotTab[0] = slot; // argument "-1" is past end of slots
for (int i = 0; i < ptypeCount; i++) {
--slot;
slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
argToSlotTab[1+i] = slot;
}
}
this.argToSlotTable = argToSlotTab;
this.slotToArgTable = slotToArgTab;
if (pslotCount >= 256) throw newIllegalArgumentException("too many arguments");
// send a few bits down to the JVM:
this.vmslots = parameterSlotCount();
// short circuit some no-op canonicalizations:
if (!hasPrimitives()) {
primsAsBoxes = erasedType;
primArgsAsBoxes = erasedType;
primsAsInts = erasedType;
primsAsLongs = erasedType;
primsAtEnd = erasedType;
}
}
/** Turn all primitive types to corresponding wrapper types.
*/
public MethodType primsAsBoxes() {
MethodType ct = primsAsBoxes;
if (ct != null) return ct;
MethodType t = erasedType;
ct = canonicalize(erasedType, WRAP, WRAP);
if (ct == null) ct = t; // no prims to box
return primsAsBoxes = ct;
}
/** Turn all primitive argument types to corresponding wrapper types.
* Subword and void return types are promoted to int.
*/
public MethodType primArgsAsBoxes() {
MethodType ct = primArgsAsBoxes;
if (ct != null) return ct;
MethodType t = erasedType;
ct = canonicalize(erasedType, RAW_RETURN, WRAP);
if (ct == null) ct = t; // no prims to box
return primArgsAsBoxes = ct;
}
/** Turn all primitive types to either int or long.
* Floating point return types are not changed, because
* they may require special calling sequences.
* A void return value is turned to int.
*/
public MethodType primsAsInts() {
MethodType ct = primsAsInts;
if (ct != null) return ct;
MethodType t = erasedType;
ct = canonicalize(t, RAW_RETURN, INTS);
if (ct == null) ct = t; // no prims to int-ify
return primsAsInts = ct;
}
/** Turn all primitive types to either int or long.
* Floating point return types are not changed, because
* they may require special calling sequences.
* A void return value is turned to int.
*/
public MethodType primsAsLongs() {
MethodType ct = primsAsLongs;
if (ct != null) return ct;
MethodType t = erasedType;
ct = canonicalize(t, RAW_RETURN, LONGS);
if (ct == null) ct = t; // no prims to int-ify
return primsAsLongs = ct;
}
/** Stably sort parameters into 3 buckets: ref, int, long. */
public MethodType primsAtEnd() {
MethodType ct = primsAtEnd;
if (ct != null) return ct;
MethodType t = erasedType;
int pac = primitiveParameterCount();
if (pac == 0)
return primsAtEnd = t;
int argc = parameterCount();
int lac = longPrimitiveParameterCount();
if (pac == argc && (lac == 0 || lac == argc))
return primsAtEnd = t;
// known to have a mix of 2 or 3 of ref, int, long
int[] reorder = primsAtEndOrder(t);
ct = reorderParameters(t, reorder, null);
//System.out.println("t="+t+" / reorder="+java.util.Arrays.toString(reorder)+" => "+ct);
return primsAtEnd = ct;
}
/** Compute a new ordering of parameters so that all references
* are before all ints or longs, and all ints are before all longs.
* For this ordering, doubles count as longs, and all other primitive
* values count as ints.
* As a special case, if the parameters are already in the specified
* order, this method returns a null reference, rather than an array
* specifying a null permutation.
* <p>
* For example, the type {@code (int,boolean,int,Object,String)void}
* produces the order {@code {3,4,0,1,2}}, the type
* {@code (long,int,String)void} produces {@code {2,1,2}}, and
* the type {@code (Object,int)Object} produces {@code null}.
*/
public static int[] primsAtEndOrder(MethodType mt) {
MethodTypeForm form = mt.form();
if (form.primsAtEnd == form.erasedType)
// quick check shows no reordering is necessary
return null;
int argc = form.parameterCount();
int[] paramOrder = new int[argc];
// 3-way bucket sort:
int pac = form.primitiveParameterCount();
int lac = form.longPrimitiveParameterCount();
int rfill = 0, ifill = argc - pac, lfill = argc - lac;
Class<?>[] ptypes = mt.ptypes();
boolean changed = false;
for (int i = 0; i < ptypes.length; i++) {
Class<?> pt = ptypes[i];
int ord;
if (!pt.isPrimitive()) ord = rfill++;
else if (!hasTwoArgSlots(pt)) ord = ifill++;
else ord = lfill++;
if (ord != i) changed = true;
assert(paramOrder[ord] == 0);
paramOrder[ord] = i;
}
assert(rfill == argc - pac && ifill == argc - lac && lfill == argc);
if (!changed) {
form.primsAtEnd = form.erasedType;
return null;
}
return paramOrder;
}
class MethodTypeForm extends MethodTypeImpl { /** Put the existing parameters of mt into a new order, given by newParamOrder.
* The third argument is logically appended to mt.parameterArray,
* so that elements of newParamOrder can index either pre-existing or
* new parameter types.
*/
public static MethodType reorderParameters(MethodType mt, int[] newParamOrder, Class<?>[] moreParams) {
if (newParamOrder == null) return mt; // no-op reordering
Class<?>[] ptypes = mt.ptypes();
Class<?>[] ntypes = new Class<?>[newParamOrder.length];
int maxParam = ptypes.length + (moreParams == null ? 0 : moreParams.length);
boolean changed = (ntypes.length != ptypes.length);
for (int i = 0; i < newParamOrder.length; i++) {
int param = newParamOrder[i];
if (param != i) changed = true;
Class<?> nt;
if (param < ptypes.length) nt = ptypes[param];
else if (param == maxParam) nt = mt.returnType();
else nt = moreParams[param - ptypes.length];
ntypes[i] = nt;
}
if (!changed) return mt;
return MethodType.makeImpl(mt.returnType(), ntypes, true);
}
private static boolean hasTwoArgSlots(Class<?> type) {
return type == long.class || type == double.class;
}
private static long pack(int a, int b, int c, int d) {
assert(((a|b|c|d) & ~0xFFFF) == 0);
long hw = ((a << 16) | b), lw = ((c << 16) | d);
return (hw << 32) | lw;
}
private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
assert(word <= 3);
return (char)(packed >> ((3-word) * 16));
}
MethodTypeForm(MethodType erasedType) { public int parameterCount() { // # outgoing values
super(erasedType); return unpack(argCounts, 3);
}
public int parameterSlotCount() { // # outgoing interpreter slots
return unpack(argCounts, 2);
}
public int returnCount() { // = 0 (V), or 1
return unpack(argCounts, 1);
}
public int returnSlotCount() { // = 0 (V), 2 (J/D), or 1
return unpack(argCounts, 0);
}
public int primitiveParameterCount() {
return unpack(primCounts, 3);
}
public int longPrimitiveParameterCount() {
return unpack(primCounts, 2);
}
public int primitiveReturnCount() { // = 0 (obj), or 1
return unpack(primCounts, 1);
}
public int longPrimitiveReturnCount() { // = 1 (J/D), or 0
return unpack(primCounts, 0);
}
public boolean hasPrimitives() {
return primCounts != 0;
}
// public boolean hasNonVoidPrimitives() {
// if (primCounts == 0) return false;
// if (primitiveParameterCount() != 0) return true;
// return (primitiveReturnCount() != 0 && returnCount() != 0);
// }
public boolean hasLongPrimitives() {
return (longPrimitiveParameterCount() | longPrimitiveReturnCount()) != 0;
}
public int parameterToArgSlot(int i) {
return argToSlotTable[1+i];
} }
public int argSlotToParameter(int argSlot) {
// Note: Empty slots are represented by zero in this table.
// Valid arguments slots contain incremented entries, so as to be non-zero.
// We return -1 the caller to mean an empty slot.
return slotToArgTable[argSlot] - 1;
}
static MethodTypeForm findForm(MethodType mt) {
MethodType erased = canonicalize(mt, ERASE, ERASE);
if (erased == null) {
// It is already erased. Make a new MethodTypeForm.
return new MethodTypeForm(mt);
} else {
// Share the MethodTypeForm with the erased version.
return erased.form();
}
}
/** Codes for {@link #canonicalize(java.lang.Class, int)}.
* ERASE means change every reference to {@code Object}.
* WRAP means convert primitives (including {@code void} to their
* corresponding wrapper types. UNWRAP means the reverse of WRAP.
* INTS means convert all non-void primitive types to int or long,
* according to size. LONGS means convert all non-void primitives
* to long, regardless of size. RAW_RETURN means convert a type
* (assumed to be a return type) to int if it is smaller than an int,
* or if it is void.
*/
public static final int NO_CHANGE = 0, ERASE = 1, WRAP = 2, UNWRAP = 3, INTS = 4, LONGS = 5, RAW_RETURN = 6;
/** Canonicalize the types in the given method type.
* If any types change, intern the new type, and return it.
* Otherwise return null.
*/
public static MethodType canonicalize(MethodType mt, int howRet, int howArgs) {
Class<?>[] ptypes = mt.ptypes();
Class<?>[] ptc = MethodTypeForm.canonicalizes(ptypes, howArgs);
Class<?> rtype = mt.returnType();
Class<?> rtc = MethodTypeForm.canonicalize(rtype, howRet);
if (ptc == null && rtc == null) {
// It is already canonical.
return null;
}
// Find the erased version of the method type:
if (rtc == null) rtc = rtype;
if (ptc == null) ptc = ptypes;
return MethodType.makeImpl(rtc, ptc, true);
}
/** Canonicalize the given return or param type.
* Return null if the type is already canonicalized.
*/
static Class<?> canonicalize(Class<?> t, int how) {
Class<?> ct;
if (t == Object.class) {
// no change, ever
} else if (!t.isPrimitive()) {
switch (how) {
case UNWRAP:
ct = Wrapper.asPrimitiveType(t);
if (ct != t) return ct;
break;
case RAW_RETURN:
case ERASE:
return Object.class;
}
} else if (t == void.class) {
// no change, usually
switch (how) {
case RAW_RETURN:
return int.class;
case WRAP:
return Void.class;
}
} else {
// non-void primitive
switch (how) {
case WRAP:
return Wrapper.asWrapperType(t);
case INTS:
if (t == int.class || t == long.class)
return null; // no change
if (t == double.class)
return long.class;
return int.class;
case LONGS:
if (t == long.class)
return null; // no change
return long.class;
case RAW_RETURN:
if (t == int.class || t == long.class ||
t == float.class || t == double.class)
return null; // no change
// everything else returns as an int
return int.class;
}
}
// no change; return null to signify
return null;
}
/** Canonicalize each param type in the given array.
* Return null if all types are already canonicalized.
*/
static Class<?>[] canonicalizes(Class<?>[] ts, int how) {
Class<?>[] cs = null;
for (int imax = ts.length, i = 0; i < imax; i++) {
Class<?> c = canonicalize(ts[i], how);
if (c != null) {
if (cs == null)
cs = ts.clone();
cs[i] = c;
}
}
return cs;
}
/*non-public*/ void notifyGenericMethodType() {
if (genericInvoker != null) return;
try {
// Trigger adapter creation.
genericInvoker = InvokeGeneric.genericInvokerOf(erasedType);
} catch (Exception ex) {
Error err = new InternalError("Exception while resolving invokeGeneric");
err.initCause(ex);
throw err;
}
}
@Override
public String toString() {
return "Form"+erasedType;
}
} }
...@@ -25,8 +25,6 @@ ...@@ -25,8 +25,6 @@
package java.dyn; package java.dyn;
import sun.dyn.*;
import sun.dyn.empty.Empty;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
......
/* /*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,14 +23,14 @@ ...@@ -23,14 +23,14 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*; import sun.dyn.util.ValueConversions;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import sun.dyn.util.ValueConversions; import static java.dyn.MethodHandleStatics.*;
import static sun.dyn.MemberName.newIllegalArgumentException; import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* Generic spread adapter. * Generic spread adapter.
...@@ -110,7 +110,7 @@ class SpreadGeneric { ...@@ -110,7 +110,7 @@ class SpreadGeneric {
static SpreadGeneric of(MethodType targetType, int spreadCount) { static SpreadGeneric of(MethodType targetType, int spreadCount) {
if (targetType != targetType.generic()) if (targetType != targetType.generic())
throw new UnsupportedOperationException("NYI type="+targetType); throw new UnsupportedOperationException("NYI type="+targetType);
MethodTypeImpl form = MethodTypeImpl.of(targetType); MethodTypeForm form = targetType.form();
int outcount = form.parameterCount(); int outcount = form.parameterCount();
assert(spreadCount <= outcount); assert(spreadCount <= outcount);
SpreadGeneric[] spreadGens = form.spreadGeneric; SpreadGeneric[] spreadGens = form.spreadGeneric;
...@@ -129,7 +129,7 @@ class SpreadGeneric { ...@@ -129,7 +129,7 @@ class SpreadGeneric {
// This mini-api is called from an Adapter to manage the spread. // This mini-api is called from an Adapter to manage the spread.
/** A check/coercion that happens once before any selections. */ /** A check/coercion that happens once before any selections. */
protected Object check(Object av, int n) { protected Object check(Object av, int n) {
MethodHandleImpl.checkSpreadArgument(av, n); checkSpreadArgument(av, n);
return av; return av;
} }
...@@ -166,7 +166,7 @@ class SpreadGeneric { ...@@ -166,7 +166,7 @@ class SpreadGeneric {
// see if it has the required invoke method // see if it has the required invoke method
MethodHandle entryPoint = null; MethodHandle entryPoint = null;
try { try {
entryPoint = MethodHandleImpl.IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls); entryPoint = IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
} }
if (entryPoint == null) continue; if (entryPoint == null) continue;
...@@ -221,21 +221,21 @@ class SpreadGeneric { ...@@ -221,21 +221,21 @@ class SpreadGeneric {
@Override @Override
public String toString() { public String toString() {
return MethodHandleImpl.addTypeString(target, this); return addTypeString(target, this);
} }
static final MethodHandle NO_ENTRY = ValueConversions.identity(); static final MethodHandle NO_ENTRY = ValueConversions.identity();
protected boolean isPrototype() { return target == null; } protected boolean isPrototype() { return target == null; }
protected Adapter(SpreadGeneric outer) { protected Adapter(SpreadGeneric outer) {
super(Access.TOKEN, NO_ENTRY); super(NO_ENTRY);
this.outer = outer; this.outer = outer;
this.target = null; this.target = null;
assert(isPrototype()); assert(isPrototype());
} }
protected Adapter(SpreadGeneric outer, MethodHandle target) { protected Adapter(SpreadGeneric outer, MethodHandle target) {
super(Access.TOKEN, outer.entryPoint); super(outer.entryPoint);
this.outer = outer; this.outer = outer;
this.target = target; this.target = target;
} }
...@@ -251,7 +251,7 @@ class SpreadGeneric { ...@@ -251,7 +251,7 @@ class SpreadGeneric {
return outer.select(av, n); return outer.select(av, n);
} }
static private final String CLASS_PREFIX; // "sun.dyn.SpreadGeneric$" static private final String CLASS_PREFIX; // "java.dyn.SpreadGeneric$"
static { static {
String aname = Adapter.class.getName(); String aname = Adapter.class.getName();
String sname = Adapter.class.getSimpleName(); String sname = Adapter.class.getSimpleName();
......
...@@ -23,15 +23,14 @@ ...@@ -23,15 +23,14 @@
* questions. * questions.
*/ */
package sun.dyn; package java.dyn;
import java.dyn.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import sun.dyn.util.ValueConversions; import sun.dyn.util.ValueConversions;
import sun.dyn.util.Wrapper; import sun.dyn.util.Wrapper;
import static sun.dyn.MemberName.newIllegalArgumentException; import static java.dyn.MethodHandleStatics.*;
import static sun.dyn.MethodTypeImpl.invokers; import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/** /**
* Adapters which mediate between incoming calls which are not generic * Adapters which mediate between incoming calls which are not generic
...@@ -73,7 +72,7 @@ class ToGeneric { ...@@ -73,7 +72,7 @@ class ToGeneric {
assert(entryType.erase() == entryType); // for now assert(entryType.erase() == entryType); // for now
// incoming call will first "forget" all reference types except Object // incoming call will first "forget" all reference types except Object
this.entryType = entryType; this.entryType = entryType;
MethodHandle invoker0 = invokers(entryType.generic()).exactInvoker(); MethodHandle invoker0 = entryType.generic().invokers().exactInvoker();
MethodType rawEntryTypeInit; MethodType rawEntryTypeInit;
Adapter ad = findAdapter(rawEntryTypeInit = entryType); Adapter ad = findAdapter(rawEntryTypeInit = entryType);
if (ad != null) { if (ad != null) {
...@@ -89,15 +88,15 @@ class ToGeneric { ...@@ -89,15 +88,15 @@ class ToGeneric {
} }
// next, it will reorder primitives after references // next, it will reorder primitives after references
MethodType primsAtEnd = MethodTypeImpl.of(entryType).primsAtEnd(); MethodType primsAtEnd = entryType.form().primsAtEnd();
// at the same time, it will "forget" all primitive types except int/long // at the same time, it will "forget" all primitive types except int/long
this.primsAtEndOrder = MethodTypeImpl.primsAtEndOrder(entryType); this.primsAtEndOrder = MethodTypeForm.primsAtEndOrder(entryType);
if (primsAtEndOrder != null) { if (primsAtEndOrder != null) {
// reordering is required; build on top of a simpler ToGeneric // reordering is required; build on top of a simpler ToGeneric
ToGeneric va2 = ToGeneric.of(primsAtEnd); ToGeneric va2 = ToGeneric.of(primsAtEnd);
this.adapter = va2.adapter; this.adapter = va2.adapter;
if (true) throw new UnsupportedOperationException("NYI: primitive parameters must follow references; entryType = "+entryType); if (true) throw new UnsupportedOperationException("NYI: primitive parameters must follow references; entryType = "+entryType);
this.entryPoint = MethodHandleImpl.convertArguments(Access.TOKEN, this.entryPoint = MethodHandleImpl.convertArguments(
va2.entryPoint, primsAtEnd, entryType, primsAtEndOrder); va2.entryPoint, primsAtEnd, entryType, primsAtEndOrder);
// example: for entryType of (int,Object,Object), the reordered // example: for entryType of (int,Object,Object), the reordered
// type is (Object,Object,int) and the order is {1,2,0}, // type is (Object,Object,int) and the order is {1,2,0},
...@@ -107,7 +106,7 @@ class ToGeneric { ...@@ -107,7 +106,7 @@ class ToGeneric {
// after any needed argument reordering, it will reinterpret // after any needed argument reordering, it will reinterpret
// primitive arguments according to their "raw" types int/long // primitive arguments according to their "raw" types int/long
MethodType intsAtEnd = MethodTypeImpl.of(primsAtEnd).primsAsInts(); MethodType intsAtEnd = primsAtEnd.form().primsAsInts();
ad = findAdapter(rawEntryTypeInit = intsAtEnd); ad = findAdapter(rawEntryTypeInit = intsAtEnd);
MethodHandle rawEntryPoint; MethodHandle rawEntryPoint;
if (ad != null) { if (ad != null) {
...@@ -116,7 +115,7 @@ class ToGeneric { ...@@ -116,7 +115,7 @@ class ToGeneric {
// Perhaps the adapter is available only for longs. // Perhaps the adapter is available only for longs.
// If so, we can use it, but there will have to be a little // If so, we can use it, but there will have to be a little
// more stack motion on each call. // more stack motion on each call.
MethodType longsAtEnd = MethodTypeImpl.of(primsAtEnd).primsAsLongs(); MethodType longsAtEnd = primsAtEnd.form().primsAsLongs();
ad = findAdapter(rawEntryTypeInit = longsAtEnd); ad = findAdapter(rawEntryTypeInit = longsAtEnd);
if (ad != null) { if (ad != null) {
MethodType eptWithLongs = longsAtEnd.insertParameterTypes(0, ad.getClass()); MethodType eptWithLongs = longsAtEnd.insertParameterTypes(0, ad.getClass());
...@@ -128,7 +127,7 @@ class ToGeneric { ...@@ -128,7 +127,7 @@ class ToGeneric {
assert(midType.parameterType(i) == long.class); assert(midType.parameterType(i) == long.class);
assert(eptWithInts.parameterType(i) == int.class); assert(eptWithInts.parameterType(i) == int.class);
MethodType nextType = midType.changeParameterType(i, int.class); MethodType nextType = midType.changeParameterType(i, int.class);
rawEntryPoint = MethodHandle.convertArguments(Access.TOKEN, rawEntryPoint = MethodHandleImpl.convertArguments(
rawEntryPoint, nextType, midType, null); rawEntryPoint, nextType, midType, null);
midType = nextType; midType = nextType;
} }
...@@ -143,7 +142,7 @@ class ToGeneric { ...@@ -143,7 +142,7 @@ class ToGeneric {
} }
MethodType tepType = entryType.insertParameterTypes(0, ad.getClass()); MethodType tepType = entryType.insertParameterTypes(0, ad.getClass());
this.entryPoint = this.entryPoint =
AdapterMethodHandle.makeRetypeRaw(Access.TOKEN, tepType, rawEntryPoint); AdapterMethodHandle.makeRetypeRaw(tepType, rawEntryPoint);
if (this.entryPoint == null) if (this.entryPoint == null)
throw new UnsupportedOperationException("cannot retype to "+entryType throw new UnsupportedOperationException("cannot retype to "+entryType
+" from "+rawEntryPoint.type().dropParameterTypes(0, 1)); +" from "+rawEntryPoint.type().dropParameterTypes(0, 1));
...@@ -168,7 +167,7 @@ class ToGeneric { ...@@ -168,7 +167,7 @@ class ToGeneric {
assert(src.isPrimitive() && dst.isPrimitive()); assert(src.isPrimitive() && dst.isPrimitive());
if (filteredInvoker == null) { if (filteredInvoker == null) {
filteredInvoker = filteredInvoker =
AdapterMethodHandle.makeCheckCast(Access.TOKEN, AdapterMethodHandle.makeCheckCast(
invoker.type().generic(), invoker, 0, MethodHandle.class); invoker.type().generic(), invoker, 0, MethodHandle.class);
if (filteredInvoker == null) throw new UnsupportedOperationException("NYI"); if (filteredInvoker == null) throw new UnsupportedOperationException("NYI");
} }
...@@ -177,7 +176,7 @@ class ToGeneric { ...@@ -177,7 +176,7 @@ class ToGeneric {
if (filteredInvoker == null) throw new InternalError(); if (filteredInvoker == null) throw new InternalError();
} }
if (filteredInvoker == null) return invoker; if (filteredInvoker == null) return invoker;
return AdapterMethodHandle.makeRetypeOnly(Access.TOKEN, invoker.type(), filteredInvoker); return AdapterMethodHandle.makeRetypeOnly(invoker.type(), filteredInvoker);
} }
/** /**
...@@ -227,7 +226,7 @@ class ToGeneric { ...@@ -227,7 +226,7 @@ class ToGeneric {
// retype erased reference arguments (the cast makes it safe to do this) // retype erased reference arguments (the cast makes it safe to do this)
MethodType tepType = type.insertParameterTypes(0, adapter.getClass()); MethodType tepType = type.insertParameterTypes(0, adapter.getClass());
MethodHandle typedEntryPoint = MethodHandle typedEntryPoint =
AdapterMethodHandle.makeRetypeRaw(Access.TOKEN, tepType, entryPoint); AdapterMethodHandle.makeRetypeRaw(tepType, entryPoint);
return adapter.makeInstance(typedEntryPoint, invoker, convert, genericTarget); return adapter.makeInstance(typedEntryPoint, invoker, convert, genericTarget);
} }
...@@ -248,7 +247,7 @@ class ToGeneric { ...@@ -248,7 +247,7 @@ class ToGeneric {
/** Return the adapter information for this type's erasure. */ /** Return the adapter information for this type's erasure. */
static ToGeneric of(MethodType type) { static ToGeneric of(MethodType type) {
MethodTypeImpl form = MethodTypeImpl.of(type); MethodTypeForm form = type.form();
ToGeneric toGen = form.toGeneric; ToGeneric toGen = form.toGeneric;
if (toGen == null) if (toGen == null)
form.toGeneric = toGen = new ToGeneric(form.erasedType()); form.toGeneric = toGen = new ToGeneric(form.erasedType());
...@@ -262,7 +261,7 @@ class ToGeneric { ...@@ -262,7 +261,7 @@ class ToGeneric {
/* Create an adapter for the given incoming call type. */ /* Create an adapter for the given incoming call type. */
static Adapter findAdapter(MethodType entryPointType) { static Adapter findAdapter(MethodType entryPointType) {
MethodTypeImpl form = MethodTypeImpl.of(entryPointType); MethodTypeForm form = entryPointType.form();
Class<?> rtype = entryPointType.returnType(); Class<?> rtype = entryPointType.returnType();
int argc = form.parameterCount(); int argc = form.parameterCount();
int lac = form.longPrimitiveParameterCount(); int lac = form.longPrimitiveParameterCount();
...@@ -283,7 +282,7 @@ class ToGeneric { ...@@ -283,7 +282,7 @@ class ToGeneric {
for (String iname : inames) { for (String iname : inames) {
MethodHandle entryPoint = null; MethodHandle entryPoint = null;
try { try {
entryPoint = MethodHandleImpl.IMPL_LOOKUP. entryPoint = IMPL_LOOKUP.
findSpecial(acls, iname, entryPointType, acls); findSpecial(acls, iname, entryPointType, acls);
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
} }
...@@ -338,13 +337,13 @@ class ToGeneric { ...@@ -338,13 +337,13 @@ class ToGeneric {
@Override @Override
public String toString() { public String toString() {
return target == null ? "prototype:"+convert : MethodHandleImpl.addTypeString(target, this); return target == null ? "prototype:"+convert : addTypeString(target, this);
} }
protected boolean isPrototype() { return target == null; } protected boolean isPrototype() { return target == null; }
/* Prototype constructor. */ /* Prototype constructor. */
protected Adapter(MethodHandle entryPoint) { protected Adapter(MethodHandle entryPoint) {
super(Access.TOKEN, entryPoint); super(entryPoint);
this.invoker = null; this.invoker = null;
this.convert = entryPoint; this.convert = entryPoint;
this.target = null; this.target = null;
...@@ -356,7 +355,7 @@ class ToGeneric { ...@@ -356,7 +355,7 @@ class ToGeneric {
} }
protected Adapter(MethodHandle entryPoint, MethodHandle invoker, MethodHandle convert, MethodHandle target) { protected Adapter(MethodHandle entryPoint, MethodHandle invoker, MethodHandle convert, MethodHandle target) {
super(Access.TOKEN, entryPoint); super(entryPoint);
this.invoker = invoker; this.invoker = invoker;
this.convert = convert; this.convert = convert;
this.target = target; this.target = target;
...@@ -396,7 +395,7 @@ class ToGeneric { ...@@ -396,7 +395,7 @@ class ToGeneric {
protected float return_F(Object res) throws Throwable { return (float) convert.invokeExact(res); } protected float return_F(Object res) throws Throwable { return (float) convert.invokeExact(res); }
protected double return_D(Object res) throws Throwable { return (double)convert.invokeExact(res); } protected double return_D(Object res) throws Throwable { return (double)convert.invokeExact(res); }
static private final String CLASS_PREFIX; // "sun.dyn.ToGeneric$" static private final String CLASS_PREFIX; // "java.dyn.ToGeneric$"
static { static {
String aname = Adapter.class.getName(); String aname = Adapter.class.getName();
String sname = Adapter.class.getSimpleName(); String sname = Adapter.class.getSimpleName();
......
...@@ -25,8 +25,6 @@ ...@@ -25,8 +25,6 @@
package java.dyn; package java.dyn;
import java.util.List;
/** /**
* A {@code VolatileCallSite} is a {@link CallSite} whose target acts like a volatile variable. * A {@code VolatileCallSite} is a {@link CallSite} whose target acts like a volatile variable.
* An {@code invokedynamic} instruction linked to a {@code VolatileCallSite} sees updates * An {@code invokedynamic} instruction linked to a {@code VolatileCallSite} sees updates
......
/* /*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
......
/*
* Copyright (c) 2008, 2010, 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 sun.dyn;
import java.dyn.*;
import static sun.dyn.MemberName.uncaughtException;
/**
* Parts of CallSite known to the JVM.
* @author jrose
*/
public class CallSiteImpl {
// this implements the upcall from the JVM, MethodHandleNatives.makeDynamicCallSite:
static CallSite makeSite(MethodHandle bootstrapMethod,
// Callee information:
String name, MethodType type,
// Extra arguments for BSM, if any:
Object info,
// Caller information:
MemberName callerMethod, int callerBCI) {
Class<?> callerClass = callerMethod.getDeclaringClass();
Object caller;
if (bootstrapMethod.type().parameterType(0) == Class.class && TRANSITIONAL_BEFORE_PFD)
caller = callerClass; // remove for PFD
else
caller = MethodHandleImpl.IMPL_LOOKUP.in(callerClass);
if (bootstrapMethod == null && TRANSITIONAL_BEFORE_PFD) {
// If there is no bootstrap method, throw IncompatibleClassChangeError.
// This is a valid generic error type for resolution (JLS 12.3.3).
throw new IncompatibleClassChangeError
("Class "+callerClass.getName()+" has not declared a bootstrap method for invokedynamic");
}
CallSite site;
try {
Object binding;
info = maybeReBox(info);
if (info == null) {
binding = bootstrapMethod.invokeGeneric(caller, name, type);
} else if (!info.getClass().isArray()) {
binding = bootstrapMethod.invokeGeneric(caller, name, type, info);
} else {
Object[] argv = (Object[]) info;
if (3 + argv.length > 255)
new InvokeDynamicBootstrapError("too many bootstrap method arguments");
MethodType bsmType = bootstrapMethod.type();
if (bsmType.parameterCount() == 4 && bsmType.parameterType(3) == Object[].class)
binding = bootstrapMethod.invokeGeneric(caller, name, type, argv);
else
binding = MethodHandles.spreadInvoker(bsmType, 3)
.invokeGeneric(bootstrapMethod, caller, name, type, argv);
}
//System.out.println("BSM for "+name+type+" => "+binding);
if (binding instanceof CallSite) {
site = (CallSite) binding;
} else if (binding instanceof MethodHandle && TRANSITIONAL_BEFORE_PFD) {
// Transitional!
MethodHandle target = (MethodHandle) binding;
site = new ConstantCallSite(target);
} else {
throw new ClassCastException("bootstrap method failed to produce a CallSite");
}
if (TRANSITIONAL_BEFORE_PFD)
PRIVATE_INITIALIZE_CALL_SITE.invokeExact(site, name, type,
callerMethod, callerBCI);
assert(site.getTarget() != null);
assert(site.getTarget().type().equals(type));
} catch (Throwable ex) {
InvokeDynamicBootstrapError bex;
if (ex instanceof InvokeDynamicBootstrapError)
bex = (InvokeDynamicBootstrapError) ex;
else
bex = new InvokeDynamicBootstrapError("call site initialization exception", ex);
throw bex;
}
return site;
}
private static boolean TRANSITIONAL_BEFORE_PFD = true; // FIXME: remove for PFD
private static Object maybeReBox(Object x) {
if (x instanceof Integer) {
int xi = (int) x;
if (xi == (byte) xi)
x = xi; // must rebox; see JLS 5.1.7
return x;
} else if (x instanceof Object[]) {
Object[] xa = (Object[]) x;
for (int i = 0; i < xa.length; i++) {
if (xa[i] instanceof Integer)
xa[i] = maybeReBox(xa[i]);
}
return xa;
} else {
return x;
}
}
// This method is private in CallSite because it touches private fields in CallSite.
// These private fields (vmmethod, vmindex) are specific to the JVM.
private static final MethodHandle PRIVATE_INITIALIZE_CALL_SITE;
static {
try {
PRIVATE_INITIALIZE_CALL_SITE =
!TRANSITIONAL_BEFORE_PFD ? null :
MethodHandleImpl.IMPL_LOOKUP.findVirtual(CallSite.class, "initializeFromJVM",
MethodType.methodType(void.class,
String.class, MethodType.class,
MemberName.class, int.class));
} catch (ReflectiveOperationException ex) {
throw uncaughtException(ex);
}
}
public static void setCallSiteTarget(Access token, CallSite site, MethodHandle target) {
Access.check(token);
MethodHandleNatives.setCallSiteTarget(site, target);
}
}
此差异已折叠。
...@@ -25,20 +25,17 @@ ...@@ -25,20 +25,17 @@
package sun.dyn.util; package sun.dyn.util;
import java.dyn.*; import java.dyn.MethodHandle;
import java.dyn.MethodHandles;
import java.dyn.MethodHandles.Lookup; import java.dyn.MethodHandles.Lookup;
import java.dyn.MethodType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.List; import java.util.List;
import sun.dyn.Access;
import sun.dyn.AdapterMethodHandle;
import sun.dyn.MethodHandleImpl;
import static sun.dyn.MemberName.uncaughtException;
public class ValueConversions { public class ValueConversions {
private static final Access IMPL_TOKEN = Access.getToken(); private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
private static final Lookup IMPL_LOOKUP = MethodHandleImpl.getLookup(IMPL_TOKEN);
private static EnumMap<Wrapper, MethodHandle>[] newWrapperCaches(int n) { private static EnumMap<Wrapper, MethodHandle>[] newWrapperCaches(int n) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
...@@ -157,7 +154,7 @@ public class ValueConversions { ...@@ -157,7 +154,7 @@ public class ValueConversions {
mh = null; mh = null;
} }
} else { } else {
mh = retype(type, unbox(wrap, !exact, raw)); mh = unbox(wrap, !exact, raw).asType(type);
} }
if (mh != null) { if (mh != null) {
cache.put(wrap, mh); cache.put(wrap, mh);
...@@ -293,7 +290,7 @@ public class ValueConversions { ...@@ -293,7 +290,7 @@ public class ValueConversions {
mh = null; mh = null;
} }
} else { } else {
mh = retype(type.erase(), box(wrap, !exact, raw)); mh = box(wrap, !exact, raw).asType(type.erase());
} }
if (mh != null) { if (mh != null) {
cache.put(wrap, mh); cache.put(wrap, mh);
...@@ -412,7 +409,7 @@ public class ValueConversions { ...@@ -412,7 +409,7 @@ public class ValueConversions {
mh = null; mh = null;
} }
} else { } else {
mh = retype(IDENTITY.type(), rebox(wrap, !exact)); mh = rebox(wrap, !exact).asType(IDENTITY.type());
} }
if (mh != null) { if (mh != null) {
cache.put(wrap, mh); cache.put(wrap, mh);
...@@ -504,8 +501,8 @@ public class ValueConversions { ...@@ -504,8 +501,8 @@ public class ValueConversions {
// use the raw method // use the raw method
Wrapper rawWrap = wrap.rawPrimitive(); Wrapper rawWrap = wrap.rawPrimitive();
if (rawWrap != wrap) { if (mh == null && rawWrap != wrap) {
mh = retype(type, zeroConstantFunction(rawWrap)); mh = MethodHandles.explicitCastArguments(zeroConstantFunction(rawWrap), type);
} }
if (mh != null) { if (mh != null) {
cache.put(wrap, mh); cache.put(wrap, mh);
...@@ -552,6 +549,22 @@ public class ValueConversions { ...@@ -552,6 +549,22 @@ public class ValueConversions {
return x; return x;
} }
static byte identity(byte x) {
return x;
}
static short identity(short x) {
return x;
}
static boolean identity(boolean x) {
return x;
}
static char identity(char x) {
return x;
}
/** /**
* Identity function on longs. * Identity function on longs.
* @param x an arbitrary long value * @param x an arbitrary long value
...@@ -561,6 +574,14 @@ public class ValueConversions { ...@@ -561,6 +574,14 @@ public class ValueConversions {
return x; return x;
} }
static float identity(float x) {
return x;
}
static double identity(double x) {
return x;
}
/** /**
* Identity function, with reference cast. * Identity function, with reference cast.
* @param t an arbitrary reference type * @param t an arbitrary reference type
...@@ -590,7 +611,9 @@ public class ValueConversions { ...@@ -590,7 +611,9 @@ public class ValueConversions {
IGNORE = IMPL_LOOKUP.findStatic(ValueConversions.class, "ignore", ignoreType); IGNORE = IMPL_LOOKUP.findStatic(ValueConversions.class, "ignore", ignoreType);
EMPTY = IMPL_LOOKUP.findStatic(ValueConversions.class, "empty", ignoreType.dropParameterTypes(0, 1)); EMPTY = IMPL_LOOKUP.findStatic(ValueConversions.class, "empty", ignoreType.dropParameterTypes(0, 1));
} catch (Exception ex) { } catch (Exception ex) {
throw uncaughtException(ex); Error err = new InternalError("uncaught exception");
err.initCause(ex);
throw err;
} }
} }
...@@ -622,7 +645,8 @@ public class ValueConversions { ...@@ -622,7 +645,8 @@ public class ValueConversions {
mh = MethodHandles.insertArguments(CAST_REFERENCE, 0, type); mh = MethodHandles.insertArguments(CAST_REFERENCE, 0, type);
if (exact) { if (exact) {
MethodType xmt = MethodType.methodType(type, Object.class); MethodType xmt = MethodType.methodType(type, Object.class);
mh = AdapterMethodHandle.makeRetypeRaw(IMPL_TOKEN, xmt, mh); mh = MethodHandles.explicitCastArguments(mh, xmt);
//mh = AdapterMethodHandle.makeRetypeRaw(IMPL_TOKEN, xmt, mh);
} }
if (cache != null) if (cache != null)
cache.put(wrap, mh); cache.put(wrap, mh);
...@@ -634,15 +658,11 @@ public class ValueConversions { ...@@ -634,15 +658,11 @@ public class ValueConversions {
} }
public static MethodHandle identity(Class<?> type) { public static MethodHandle identity(Class<?> type) {
if (type == Object.class) // This stuff has been moved into MethodHandles:
return IDENTITY; return MethodHandles.identity(type);
else if (!type.isPrimitive())
return retype(MethodType.methodType(type, type), IDENTITY);
else
return identity(Wrapper.forPrimitiveType(type));
} }
static MethodHandle identity(Wrapper wrap) { public static MethodHandle identity(Wrapper wrap) {
EnumMap<Wrapper, MethodHandle> cache = CONSTANT_FUNCTIONS[1]; EnumMap<Wrapper, MethodHandle> cache = CONSTANT_FUNCTIONS[1];
MethodHandle mh = cache.get(wrap); MethodHandle mh = cache.get(wrap);
if (mh != null) { if (mh != null) {
...@@ -665,12 +685,6 @@ public class ValueConversions { ...@@ -665,12 +685,6 @@ public class ValueConversions {
return mh; return mh;
} }
// use a raw conversion
if (wrap.isSingleWord() && wrap != Wrapper.INT) {
mh = retype(type, identity(Wrapper.INT));
} else if (wrap.isDoubleWord() && wrap != Wrapper.LONG) {
mh = retype(type, identity(Wrapper.LONG));
}
if (mh != null) { if (mh != null) {
cache.put(wrap, mh); cache.put(wrap, mh);
return mh; return mh;
...@@ -678,10 +692,6 @@ public class ValueConversions { ...@@ -678,10 +692,6 @@ public class ValueConversions {
throw new IllegalArgumentException("cannot find identity for " + wrap); throw new IllegalArgumentException("cannot find identity for " + wrap);
} }
private static MethodHandle retype(MethodType type, MethodHandle mh) {
return AdapterMethodHandle.makeRetypeRaw(IMPL_TOKEN, type, mh);
}
private static final Object[] NO_ARGS_ARRAY = {}; private static final Object[] NO_ARGS_ARRAY = {};
private static Object[] makeArray(Object... args) { return args; } private static Object[] makeArray(Object... args) { return args; }
private static Object[] array() { return NO_ARGS_ARRAY; } private static Object[] array() { return NO_ARGS_ARRAY; }
......
...@@ -26,9 +26,6 @@ ...@@ -26,9 +26,6 @@
package sun.dyn.util; package sun.dyn.util;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import sun.dyn.MemberName;
import sun.dyn.MethodHandleImpl;
import sun.dyn.empty.Empty;
import static java.lang.reflect.Modifier.*; import static java.lang.reflect.Modifier.*;
/** /**
......
/*
* Copyright (c) 2010, 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.
*
* 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.
*
*/
/**
* @test
* @bug 6987555
* @summary JSR 292 unboxing to a boolean value fails on big-endian SPARC
*
* @run main/othervm -Xint -ea -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles -XX:+EnableInvokeDynamic -XX:+UnlockDiagnosticVMOptions -XX:+VerifyMethodHandles Test6987555
*/
import java.dyn.*;
public class Test6987555 {
private static final Class CLASS = Test6987555.class;
private static final String NAME = "foo";
private static final boolean DEBUG = false;
public static void main(String[] args) throws Throwable {
testboolean();
testbyte();
testchar();
testshort();
testint();
}
// boolean
static void testboolean() throws Throwable {
doboolean(false);
doboolean(true);
}
static void doboolean(boolean x) throws Throwable {
if (DEBUG) System.out.println("boolean=" + x);
MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(boolean.class, boolean.class));
MethodHandle mh2 = mh1.asType(MethodType.methodType(boolean.class, Boolean.class));
boolean a = (boolean) mh1.invokeExact(x);
boolean b = (boolean) mh2.invokeExact(Boolean.valueOf(x));
assert a == b : a + " != " + b;
}
// byte
static void testbyte() throws Throwable {
byte[] a = new byte[] {
Byte.MIN_VALUE,
Byte.MIN_VALUE + 1,
-0x0F,
-1,
0,
1,
0x0F,
Byte.MAX_VALUE - 1,
Byte.MAX_VALUE
};
for (int i = 0; i < a.length; i++) {
dobyte(a[i]);
}
}
static void dobyte(byte x) throws Throwable {
if (DEBUG) System.out.println("byte=" + x);
MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(byte.class, byte.class));
MethodHandle mh2 = mh1.asType(MethodType.methodType(byte.class, Byte.class));
byte a = (byte) mh1.invokeExact(x);
byte b = (byte) mh2.invokeExact(Byte.valueOf(x));
assert a == b : a + " != " + b;
}
// char
static void testchar() throws Throwable {
char[] a = new char[] {
Character.MIN_VALUE,
Character.MIN_VALUE + 1,
0x000F,
0x00FF,
0x0FFF,
Character.MAX_VALUE - 1,
Character.MAX_VALUE
};
for (int i = 0; i < a.length; i++) {
dochar(a[i]);
}
}
static void dochar(char x) throws Throwable {
if (DEBUG) System.out.println("char=" + x);
MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(char.class, char.class));
MethodHandle mh2 = mh1.asType(MethodType.methodType(char.class, Character.class));
char a = (char) mh1.invokeExact(x);
char b = (char) mh2.invokeExact(Character.valueOf(x));
assert a == b : a + " != " + b;
}
// short
static void testshort() throws Throwable {
short[] a = new short[] {
Short.MIN_VALUE,
Short.MIN_VALUE + 1,
-0x0FFF,
-0x00FF,
-0x000F,
-1,
0,
1,
0x000F,
0x00FF,
0x0FFF,
Short.MAX_VALUE - 1,
Short.MAX_VALUE
};
for (int i = 0; i < a.length; i++) {
doshort(a[i]);
}
}
static void doshort(short x) throws Throwable {
if (DEBUG) System.out.println("short=" + x);
MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(short.class, short.class));
MethodHandle mh2 = mh1.asType(MethodType.methodType(short.class, Short.class));
short a = (short) mh1.invokeExact(x);
short b = (short) mh2.invokeExact(Short.valueOf(x));
assert a == b : a + " != " + b;
}
// int
static void testint() throws Throwable {
int[] a = new int[] {
Integer.MIN_VALUE,
Integer.MIN_VALUE + 1,
-0x00000FFF,
-0x000000FF,
-0x0000000F,
-1,
0,
1,
0x0000000F,
0x000000FF,
0x00000FFF,
Integer.MAX_VALUE - 1,
Integer.MAX_VALUE
};
for (int i = 0; i < a.length; i++) {
doint(a[i]);
}
}
static void doint(int x) throws Throwable {
if (DEBUG) System.out.println("int=" + x);
MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(int.class, int.class));
MethodHandle mh2 = mh1.asType(MethodType.methodType(int.class, Integer.class));
int a = (int) mh1.invokeExact(x);
int b = (int) mh2.invokeExact(Integer.valueOf(x));
assert a == b : a + " != " + b;
}
public static boolean foo(boolean i) { return i; }
public static byte foo(byte i) { return i; }
public static char foo(char i) { return i; }
public static short foo(short i) { return i; }
public static int foo(int i) { return i; }
}
此差异已折叠。
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
package test.java.dyn; package test.java.dyn;
import sun.dyn.MemberName;
import java.dyn.MethodType; import java.dyn.MethodType;
import java.lang.reflect.Method; import java.lang.reflect.Method;
...@@ -163,18 +162,6 @@ public class MethodTypeTest { ...@@ -163,18 +162,6 @@ public class MethodTypeTest {
assertSame(expResult, result); assertSame(expResult, result);
} }
/**
* Test of make method, of class MethodType.
*/
@Test
public void testMake_Method() {
System.out.println("make (via MemberName.getMethodType)");
MethodType expResult = MethodType.methodType(int.class, String.class);
MemberName name = new MemberName(compareTo);
MethodType result = name.getMethodType();
assertSame(expResult, result);
}
/** /**
* Test of make method, of class MethodType. * Test of make method, of class MethodType.
*/ */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册