提交 11b323cb 编写于 作者: 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
上级 3b49f37c
/*
* 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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -23,15 +23,11 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import sun.dyn.util.VerifyType;
import sun.dyn.util.Wrapper;
import java.dyn.*;
import java.util.List;
import sun.dyn.MethodHandleNatives.Constants;
import static sun.dyn.MethodHandleImpl.IMPL_LOOKUP;
import static sun.dyn.MemberName.newIllegalArgumentException;
import static java.dyn.MethodHandleStatics.*;
/**
* The flavor of method handle which emulates an invoke instruction
......@@ -39,37 +35,29 @@ import static sun.dyn.MemberName.newIllegalArgumentException;
* when the handle is created, not when it is invoked.
* @author jrose
*/
public class BoundMethodHandle extends MethodHandle {
class BoundMethodHandle extends MethodHandle {
//MethodHandle vmtarget; // next BMH or final DMH or methodOop
private final Object argument; // argument to insert
private final int vmargslot; // position at which it is inserted
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.
/** Bind a direct MH to its receiver (or first ref. argument).
* The JVM will pre-dispatch the MH if it is not already static.
*/
BoundMethodHandle(DirectMethodHandle mh, Object argument) {
super(Access.TOKEN, mh.type().dropParameterTypes(0, 1));
/*non-public*/ BoundMethodHandle(DirectMethodHandle mh, Object argument) {
super(mh.type().dropParameterTypes(0, 1));
// check the type now, once for all:
this.argument = checkReferenceArgument(argument, mh, 0);
this.vmargslot = this.type().parameterSlotCount();
if (MethodHandleNatives.JVM_SUPPORT) {
this.vmtarget = null; // maybe updated by JVM
MethodHandleNatives.init(this, mh, 0);
} else {
this.vmtarget = mh;
}
initTarget(mh, 0);
}
/** Insert an argument into an arbitrary method handle.
* If argnum is zero, inserts the first argument, etc.
* 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),
mh, argument, argnum);
}
......@@ -77,8 +65,8 @@ public class BoundMethodHandle extends MethodHandle {
/** Insert an argument into an arbitrary method handle.
* If argnum is zero, inserts the first argument, etc.
*/
BoundMethodHandle(MethodType type, MethodHandle mh, Object argument, int argnum) {
super(Access.TOKEN, type);
/*non-public*/ BoundMethodHandle(MethodType type, MethodHandle mh, Object argument, int argnum) {
super(type);
if (mh.type().parameterType(argnum).isPrimitive())
this.argument = bindPrimitiveArgument(argument, mh, argnum);
else {
......@@ -89,18 +77,14 @@ public class BoundMethodHandle extends MethodHandle {
}
private void initTarget(MethodHandle mh, int argnum) {
if (MethodHandleNatives.JVM_SUPPORT) {
this.vmtarget = null; // maybe updated by JVM
MethodHandleNatives.init(this, mh, argnum);
} else {
this.vmtarget = mh;
}
//this.vmtarget = mh; // maybe updated by JVM
MethodHandleNatives.init(this, mh, argnum);
}
/** For the AdapterMethodHandle subclass.
*/
BoundMethodHandle(MethodType type, Object argument, int vmargslot) {
super(Access.TOKEN, type);
/*non-public*/ BoundMethodHandle(MethodType type, Object argument, int vmargslot) {
super(type);
this.argument = argument;
this.vmargslot = vmargslot;
assert(this instanceof AdapterMethodHandle);
......@@ -112,8 +96,8 @@ public class BoundMethodHandle extends MethodHandle {
* same as {@code entryPoint}, except that the first argument
* type will be dropped.
*/
protected BoundMethodHandle(Access token, MethodHandle entryPoint) {
super(token, entryPoint.type().dropParameterTypes(0, 1));
/*non-public*/ BoundMethodHandle(MethodHandle entryPoint) {
super(entryPoint.type().dropParameterTypes(0, 1));
this.argument = this; // kludge; get rid of
this.vmargslot = this.type().parameterSlotDepth(0);
initTarget(entryPoint, 0);
......@@ -172,7 +156,7 @@ public class BoundMethodHandle extends MethodHandle {
@Override
public String toString() {
return MethodHandleImpl.addTypeString(baseName(), this);
return addTypeString(baseName(), this);
}
/** Component of toString() before the type string. */
......
......@@ -25,10 +25,10 @@
package java.dyn;
import sun.dyn.*;
import sun.dyn.empty.Empty;
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},
......@@ -85,7 +85,6 @@ private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String nam
*/
abstract
public class CallSite {
private static final Access IMPL_TOKEN = Access.getToken();
static { MethodHandleImpl.initStatics(); }
// Fields used only by the JVM. Do not use or change.
......@@ -111,7 +110,7 @@ public class CallSite {
*/
/*package-private*/
CallSite(MethodType type) {
target = MethodHandles.invokers(type).uninitializedCallSite();
target = type.invokers().uninitializedCallSite();
}
/**
......@@ -218,7 +217,7 @@ public class CallSite {
public abstract MethodHandle dynamicInvoker();
/*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());
return MethodHandles.foldArguments(invoker, getTarget);
}
......@@ -226,7 +225,7 @@ public class CallSite {
private static final MethodHandle GET_TARGET;
static {
try {
GET_TARGET = MethodHandles.Lookup.IMPL_LOOKUP.
GET_TARGET = IMPL_LOOKUP.
findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class));
} catch (ReflectiveOperationException ignore) {
throw new InternalError();
......@@ -252,7 +251,6 @@ public class CallSite {
/*package-private*/
void setTargetNormal(MethodHandle newTarget) {
target = newTarget;
//CallSiteImpl.setCallSiteTarget(IMPL_TOKEN, this, newTarget);
}
/*package-private*/
MethodHandle getTargetVolatile() {
......@@ -261,6 +259,105 @@ public class CallSite {
/*package-private*/
void setTargetVolatile(MethodHandle 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;
import java.util.WeakHashMap;
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.
......
/*
* 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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -23,10 +23,9 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import java.dyn.*;
import static sun.dyn.MethodHandleNatives.Constants.*;
import static java.dyn.MethodHandleNatives.Constants.*;
/**
* The flavor of method handle which emulates invokespecial or invokestatic.
......@@ -39,7 +38,7 @@ class DirectMethodHandle extends MethodHandle {
// Constructors in this class *must* be package scoped or private.
DirectMethodHandle(MethodType mtype, MemberName m, boolean doDispatch, Class<?> lookupClass) {
super(Access.TOKEN, mtype);
super(mtype);
assert(m.isMethod() || !doDispatch && m.isConstructor());
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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -23,11 +23,11 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import java.dyn.*;
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
......@@ -123,7 +123,7 @@ class FilterGeneric {
MethodType entryType = entryType(kind, pos, filterType, targetType);
if (entryType.generic() != entryType)
throw newIllegalArgumentException("must be generic: "+entryType);
MethodTypeImpl form = MethodTypeImpl.of(entryType);
MethodTypeForm form = entryType.form();
FilterGeneric filterGen = form.filterGeneric;
if (filterGen == null)
form.filterGeneric = filterGen = new FilterGeneric(entryType);
......@@ -186,7 +186,7 @@ class FilterGeneric {
// see if it has the required invoke method
MethodHandle entryPoint = null;
try {
entryPoint = MethodHandleImpl.IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
entryPoint = IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
} catch (ReflectiveOperationException ex) {
}
if (entryPoint == null) continue;
......@@ -231,7 +231,7 @@ class FilterGeneric {
@Override
public String toString() {
return MethodHandleImpl.addTypeString(target, this);
return addTypeString(target, this);
}
protected boolean isPrototype() { return target == null; }
......@@ -246,7 +246,7 @@ class FilterGeneric {
protected Adapter(MethodHandle entryPoint,
MethodHandle filter, MethodHandle target) {
super(Access.TOKEN, entryPoint);
super(entryPoint);
this.filter = filter;
this.target = target;
}
......@@ -256,7 +256,7 @@ class FilterGeneric {
MethodHandle filter, MethodHandle 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 {
String aname = Adapter.class.getName();
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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -23,10 +23,10 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import java.dyn.*;
import static sun.dyn.MemberName.uncaughtException;
import static java.dyn.MethodHandleStatics.*;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/**
* Unary function composition, useful for many small plumbing jobs.
......@@ -36,7 +36,7 @@ import static sun.dyn.MemberName.uncaughtException;
* final method type is the responsibility of a JVM-level adapter.
* @author jrose
*/
public class FilterOneArgument extends BoundMethodHandle {
class FilterOneArgument extends BoundMethodHandle {
protected final MethodHandle filter; // Object -> Object
protected final MethodHandle target; // Object -> Object
......@@ -54,15 +54,15 @@ public class FilterOneArgument extends BoundMethodHandle {
static {
try {
INVOKE =
MethodHandleImpl.IMPL_LOOKUP.findVirtual(FilterOneArgument.class, "invoke",
MethodType.genericMethodType(1));
IMPL_LOOKUP.findVirtual(FilterOneArgument.class, "invoke",
MethodType.genericMethodType(1));
} catch (ReflectiveOperationException ex) {
throw uncaughtException(ex);
}
}
protected FilterOneArgument(MethodHandle filter, MethodHandle target) {
super(Access.TOKEN, INVOKE);
super(INVOKE);
this.filter = filter;
this.target = target;
}
......
......@@ -23,12 +23,13 @@
* 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 sun.dyn.util.*;
import static sun.dyn.MethodTypeImpl.invokers;
import static java.dyn.MethodHandleStatics.*;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/**
* Adapters which mediate between incoming calls which are generic
......@@ -82,8 +83,8 @@ class FromGeneric {
}
// outgoing primitive arguments will be wrapped; unwrap them
MethodType primsAsObj = MethodTypeImpl.of(targetType).primArgsAsBoxes();
MethodType objArgsRawRet = MethodTypeImpl.of(primsAsObj).primsAsInts();
MethodType primsAsObj = targetType.form().primArgsAsBoxes();
MethodType objArgsRawRet = primsAsObj.form().primsAsInts();
if (objArgsRawRet != targetType)
ad = findAdapter(internalType0 = objArgsRawRet);
if (ad == null) {
......@@ -129,16 +130,16 @@ class FromGeneric {
MethodType targetType, MethodType internalType) {
// All the adapters we have here have reference-untyped internal calls.
assert(internalType == internalType.erase());
MethodHandle invoker = invokers(targetType).exactInvoker();
MethodHandle invoker = targetType.invokers().exactInvoker();
// cast all narrow reference types, unbox all primitive arguments:
MethodType fixArgsType = internalType.changeReturnType(targetType.returnType());
MethodHandle fixArgs = AdapterMethodHandle.convertArguments(Access.TOKEN,
MethodHandle fixArgs = MethodHandleImpl.convertArguments(
invoker, Invokers.invokerType(fixArgsType),
invoker.type(), null);
if (fixArgs == null)
throw new InternalError("bad fixArgs");
// reinterpret the calling sequence as raw:
MethodHandle retyper = AdapterMethodHandle.makeRetypeRaw(Access.TOKEN,
MethodHandle retyper = AdapterMethodHandle.makeRetypeRaw(
Invokers.invokerType(internalType), fixArgs);
if (retyper == null)
throw new InternalError("bad retyper");
......@@ -171,7 +172,7 @@ class FromGeneric {
/** Return the adapter information for this type's erasure. */
static FromGeneric of(MethodType type) {
MethodTypeImpl form = MethodTypeImpl.of(type);
MethodTypeForm form = type.form();
FromGeneric fromGen = form.fromGeneric;
if (fromGen == null)
form.fromGeneric = fromGen = new FromGeneric(form.erasedType());
......@@ -185,7 +186,7 @@ class FromGeneric {
/* Create an adapter that handles spreading calls for the given type. */
static Adapter findAdapter(MethodType internalType) {
MethodType entryType = internalType.generic();
MethodTypeImpl form = MethodTypeImpl.of(internalType);
MethodTypeForm form = internalType.form();
Class<?> rtype = internalType.returnType();
int argc = form.parameterCount();
int lac = form.longPrimitiveParameterCount();
......@@ -203,7 +204,7 @@ class FromGeneric {
// see if it has the required invoke method
MethodHandle entryPoint = null;
try {
entryPoint = MethodHandleImpl.IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
entryPoint = IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
} catch (ReflectiveOperationException ex) {
}
if (entryPoint == null) continue;
......@@ -257,7 +258,7 @@ class FromGeneric {
@Override
public String toString() {
return MethodHandleImpl.addTypeString(target, this);
return addTypeString(target, this);
}
protected boolean isPrototype() { return target == null; }
......@@ -272,7 +273,7 @@ class FromGeneric {
protected Adapter(MethodHandle entryPoint,
MethodHandle invoker, MethodHandle convert, MethodHandle target) {
super(Access.TOKEN, entryPoint);
super(entryPoint);
this.invoker = invoker;
this.convert = convert;
this.target = target;
......@@ -290,7 +291,7 @@ class FromGeneric {
protected Object convert_F(float 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 {
String aname = Adapter.class.getName();
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.
*
* This code is free software; you can redistribute it and/or modify it
......
......@@ -23,15 +23,13 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import java.dyn.*;
import java.lang.reflect.*;
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.
* @author jrose
*/
......@@ -44,7 +42,8 @@ class InvokeGeneric {
/** Compute and cache information for this adapter, so that it can
* 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.initialInvoker = makeInitialInvoker();
assert initialInvoker.type().equals(erasedCallerType
......@@ -53,22 +52,13 @@ class InvokeGeneric {
}
private static MethodHandles.Lookup lookup() {
return MethodHandleImpl.IMPL_LOOKUP;
return IMPL_LOOKUP;
}
/** Return the adapter information for this type's erasure. */
static MethodHandle genericInvokerOf(MethodType type) {
MethodTypeImpl form = MethodTypeImpl.of(type);
MethodHandle genericInvoker = form.genericInvoker;
if (genericInvoker == null) {
try {
InvokeGeneric gen = new InvokeGeneric(form.erasedType());
form.genericInvoker = genericInvoker = gen.initialInvoker;
} catch (ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
}
return genericInvoker;
/*non-public*/ static MethodHandle genericInvokerOf(MethodType erasedCallerType) throws ReflectiveOperationException {
InvokeGeneric gen = new InvokeGeneric(erasedCallerType);
return gen.initialInvoker;
}
private MethodHandle makeInitialInvoker() throws ReflectiveOperationException {
......@@ -88,7 +78,7 @@ class InvokeGeneric {
private MethodHandle makePostDispatchInvoker() {
// Take (MH'; MT, MH; A...) and run MH'(MT, MH; A...).
MethodType invokerType = erasedCallerType.insertParameterTypes(0, EXTRA_ARGS);
return invokers(invokerType).exactInvoker();
return invokerType.invokers().exactInvoker();
}
private MethodHandle dropDispatchArguments(MethodHandle targetInvoker) {
assert(targetInvoker.type().parameterType(0) == MethodHandle.class);
......@@ -112,7 +102,7 @@ class InvokeGeneric {
if (USE_AS_TYPE_PATH || target.isVarargsCollector()) {
MethodHandle newTarget = target.asType(callerType);
targetType = callerType;
Invokers invokers = MethodTypeImpl.invokers(Access.TOKEN, targetType);
Invokers invokers = targetType.invokers();
MethodHandle invoker = invokers.erasedInvokerWithDrops;
if (invoker == null) {
invokers.erasedInvokerWithDrops = invoker =
......
......@@ -23,16 +23,16 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import java.dyn.*;
import sun.dyn.empty.Empty;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/**
* Construction and caching of often-used invokers.
* @author jrose
*/
public class Invokers {
class Invokers {
// exact type (sans leading taget MH) for the outgoing call
private final MethodType targetType;
......@@ -60,15 +60,15 @@ public class Invokers {
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);
}
public MethodHandle exactInvoker() {
/*non-public*/ MethodHandle exactInvoker() {
MethodHandle invoker = exactInvoker;
if (invoker != null) return invoker;
try {
invoker = MethodHandleImpl.IMPL_LOOKUP.findVirtual(MethodHandle.class, "invokeExact", targetType);
invoker = IMPL_LOOKUP.findVirtual(MethodHandle.class, "invokeExact", targetType);
} catch (ReflectiveOperationException ex) {
throw new InternalError("JVM cannot find invoker for "+targetType);
}
......@@ -77,7 +77,7 @@ public class Invokers {
return invoker;
}
public MethodHandle genericInvoker() {
/*non-public*/ MethodHandle genericInvoker() {
MethodHandle invoker1 = exactInvoker();
MethodHandle invoker = genericInvoker;
if (invoker != null) return invoker;
......@@ -87,7 +87,7 @@ public class Invokers {
return invoker;
}
public MethodHandle erasedInvoker() {
/*non-public*/ MethodHandle erasedInvoker() {
MethodHandle invoker1 = exactInvoker();
MethodHandle invoker = erasedInvoker;
if (invoker != null) return invoker;
......@@ -100,7 +100,7 @@ public class Invokers {
return invoker;
}
public MethodHandle spreadInvoker(int objectArgCount) {
/*non-public*/ MethodHandle spreadInvoker(int objectArgCount) {
MethodHandle vaInvoker = spreadInvokers[objectArgCount];
if (vaInvoker != null) return vaInvoker;
MethodHandle gInvoker = genericInvoker();
......@@ -111,12 +111,12 @@ public class Invokers {
private static MethodHandle THROW_UCS = null;
public MethodHandle uninitializedCallSite() {
/*non-public*/ MethodHandle uninitializedCallSite() {
MethodHandle invoker = uninitializedCallSite;
if (invoker != null) return invoker;
if (targetType.parameterCount() > 0) {
MethodType type0 = targetType.dropParameterTypes(0, targetType.parameterCount());
Invokers invokers0 = MethodTypeImpl.invokers(type0);
Invokers invokers0 = type0.invokers();
invoker = MethodHandles.dropArguments(invokers0.uninitializedCallSite(),
0, targetType.parameterList());
assert(invoker.type().equals(targetType));
......@@ -125,14 +125,14 @@ public class Invokers {
}
if (THROW_UCS == null) {
try {
THROW_UCS = MethodHandleImpl.IMPL_LOOKUP
THROW_UCS = IMPL_LOOKUP
.findStatic(CallSite.class, "uninitializedCallSite",
MethodType.methodType(Empty.class));
} catch (ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
}
invoker = AdapterMethodHandle.makeRetypeRaw(Access.TOKEN, targetType, THROW_UCS);
invoker = AdapterMethodHandle.makeRetypeRaw(targetType, THROW_UCS);
assert(invoker.type().equals(targetType));
uninitializedCallSite = invoker;
return invoker;
......
......@@ -25,13 +25,9 @@
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 java.dyn.MethodHandles.Lookup;
import sun.reflect.Reflection;
import static sun.dyn.MemberName.newIllegalArgumentException;
/**
* <em>CLASS WILL BE REMOVED FOR PFD:</em>
......@@ -41,8 +37,6 @@ import static sun.dyn.MemberName.newIllegalArgumentException;
* @deprecated This class will be removed in the Public Final Draft.
*/
public class Linkage {
private static final Access IMPL_TOKEN = Access.getToken();
private Linkage() {} // do not instantiate
/**
......@@ -56,7 +50,7 @@ public class Linkage {
Class callc = Reflection.getCallerClass(2);
if (callc != null && !VerifyAccess.isSamePackage(callerClass, callc))
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 {
private static
void registerBootstrapMethodLookup(Class<?> callerClass, Class<?> runtime, String name) {
Lookup lookup = new Lookup(IMPL_TOKEN, callerClass);
Lookup lookup = new Lookup(callerClass);
MethodHandle bootstrapMethod;
try {
bootstrapMethod = lookup.findStatic(runtime, name, BOOTSTRAP_METHOD_TYPE);
} catch (ReflectiveOperationException 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
......
......@@ -23,10 +23,9 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import sun.dyn.util.BytecodeDescriptor;
import java.dyn.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
......@@ -37,7 +36,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
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
......@@ -66,7 +66,7 @@ import static sun.dyn.MethodHandleNatives.Constants.*;
* and those seven fields omit much of the information in Method.
* @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 String name; // 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 {
/** Query whether this member name is resolved to a non-static, non-final method.
*/
public boolean hasReceiverTypeDispatch() {
return (isMethod() && getVMIndex(Access.TOKEN) >= 0);
return (isMethod() && getVMIndex() >= 0);
}
/** Produce a string form of this member name.
......@@ -490,59 +490,38 @@ public final class MemberName implements Member, Cloneable {
// Queries to the JVM:
/** Document? */
public int getVMIndex(Access token) {
Access.check(token);
/*non-public*/ int getVMIndex() {
if (!isResolved())
throw newIllegalStateException("not resolved");
throw newIllegalStateException("not resolved", this);
return vmindex;
}
// public Object getVMTarget(Access token) {
// Access.check(token);
// /*non-public*/ Object getVMTarget() {
// if (!isResolved())
// throw newIllegalStateException("not resolved");
// throw newIllegalStateException("not resolved", this);
// return vmtarget;
// }
private RuntimeException newIllegalStateException(String message) {
return new IllegalStateException(message+": "+this);
}
// handy shared exception makers (they simplify the common case code)
public static RuntimeException newIllegalArgumentException(String message) {
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;
public IllegalAccessException makeAccessException(String message, Object from) {
message = message + ": "+ toString();
if (from != null) message += ", from " + from;
return new IllegalAccessException(message);
}
public static ReflectiveOperationException newNoAccessException(MemberName name) {
if (name.isResolved())
return new IllegalAccessException(name.toString());
else if (name.isConstructor())
return new NoSuchMethodException(name.toString());
else if (name.isMethod())
return new NoSuchMethodException(name.toString());
public ReflectiveOperationException makeAccessException(String message) {
message = message + ": "+ toString();
if (isResolved())
return new IllegalAccessException(message);
else if (isConstructor())
return new NoSuchMethodException(message);
else if (isMethod())
return new NoSuchMethodException(message);
else
return new NoSuchFieldException(name.toString());
}
public static Error uncaughtException(Exception ex) {
Error err = new InternalError("uncaught exception");
err.initCause(ex);
return err;
return new NoSuchFieldException(message);
}
/** Actually making a query requires an access check. */
public static Factory getFactory(Access token) {
Access.check(token);
/*non-public*/ static Factory getFactory() {
return Factory.INSTANCE;
}
public static Factory getFactory() {
return getFactory(Access.getToken());
}
/** A factory type for resolving member names with the help of the VM.
* TBD: Define access-safe public constructors for this factory.
*/
......@@ -662,7 +641,7 @@ public final class MemberName implements Member, Cloneable {
MemberName result = resolveOrNull(m, searchSupers, lookupClass);
if (result != null)
return result;
ReflectiveOperationException ex = newNoAccessException(m);
ReflectiveOperationException ex = m.makeAccessException("no access");
if (ex instanceof IllegalAccessException) throw (IllegalAccessException) ex;
throw nsmClass.cast(ex);
}
......
......@@ -25,13 +25,8 @@
package java.dyn;
//import sun.dyn.*;
import sun.dyn.Access;
import sun.dyn.MethodHandleImpl;
import static java.dyn.MethodHandles.invokers; // package-private API
import static sun.dyn.MemberName.newIllegalArgumentException; // utility
import static java.dyn.MethodHandleStatics.*;
/**
* A method handle is a typed, directly executable reference to an underlying method,
......@@ -436,12 +431,35 @@ mh.invokeExact(System.out, "Hello, world.");
* @see MethodHandles
* @author John Rose, JSR 292 EG
*/
public abstract class MethodHandle
// Note: This is an implementation inheritance hack, and will be removed
// with a JVM change which moves the required hidden state onto this class.
extends MethodHandleImpl
{
private static Access IMPL_TOKEN = Access.getToken();
public abstract class MethodHandle {
// { JVM internals:
private byte vmentry; // adapter stub or method entry point
//private int vmslots; // optionally, hoist type.form.vmslots
/*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(); }
// interface MethodHandle<R throws X extends Exception,A...>
......@@ -467,37 +485,16 @@ public abstract class MethodHandle
}
/**
* <em>CONSTRUCTOR WILL BE REMOVED FOR PFD:</em>
* Temporary constructor in early versions of the Reference Implementation.
* Method handle inheritance (if any) will be contained completely within
* Package-private constructor for the method handle implementation hierarchy.
* Method handle inheritance will be contained completely within
* 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
protected MethodHandle(Access token, MethodType type) {
super(token);
Access.check(token);
this.type = type;
}
private void initType(MethodType type) {
/*non-public*/ MethodHandle(MethodType type) {
type.getClass(); // elicit NPE
if (this.type != null) throw new InternalError();
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.
* The type descriptor at the call site of {@code invokeExact} must
......@@ -608,7 +605,7 @@ public abstract class MethodHandle
return asType(MethodType.genericMethodType(argc)).invokeWithArguments(arguments);
}
if (argc <= 10) {
MethodHandle invoker = invokers(type).genericInvoker();
MethodHandle invoker = type.invokers().genericInvoker();
switch (argc) {
case 0: return invoker.invokeExact(this);
case 1: return invoker.invokeExact(this,
......@@ -647,7 +644,7 @@ public abstract class MethodHandle
}
// 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);
}
/** Equivalent to {@code invokeWithArguments(arguments.toArray())}. */
......@@ -1004,6 +1001,6 @@ assert(failed);
*/
@Override
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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -23,14 +23,13 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import java.dyn.*;
import java.dyn.MethodHandles.Lookup;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import static sun.dyn.MethodHandleNatives.Constants.*;
import static sun.dyn.MethodHandleImpl.IMPL_LOOKUP;
import static java.dyn.MethodHandleNatives.Constants.*;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/**
* The JVM interface for the method handles package is all here.
......@@ -81,14 +80,12 @@ class MethodHandleNatives {
* This routine is for debugging and reflection.
*/
static MemberName getMethodName(MethodHandle self) {
if (!JVM_SUPPORT) return null;
return (MemberName) getTarget(self, ETF_METHOD_NAME);
}
/** Fetch the reflective version of the handled method, if available.
*/
static AccessibleObject getTargetMethod(MethodHandle self) {
if (!JVM_SUPPORT) return null;
return (AccessibleObject) getTarget(self, ETF_REFLECT_METHOD);
}
......@@ -97,7 +94,6 @@ class MethodHandleNatives {
* If it is chained to another method handle, return that handle.
*/
static Object getTargetInfo(MethodHandle self) {
if (!JVM_SUPPORT) return null;
return getTarget(self, ETF_HANDLE_OR_METHOD_NAME);
}
......@@ -111,11 +107,6 @@ class MethodHandleNatives {
*/
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. */
static final int JVM_PUSH_LIMIT;
/** JVM stack motion (in words) after one slot is pushed, usually -1.
......@@ -127,31 +118,24 @@ class MethodHandleNatives {
private static native void registerNatives();
static {
boolean JVM_SUPPORT_;
int JVM_PUSH_LIMIT_;
int JVM_STACK_MOVE_UNIT_;
int CONV_OP_IMPLEMENTED_MASK_;
try {
registerNatives();
JVM_SUPPORT_ = true;
JVM_PUSH_LIMIT_ = getConstant(Constants.GC_JVM_PUSH_LIMIT);
JVM_STACK_MOVE_UNIT_ = getConstant(Constants.GC_JVM_STACK_MOVE_UNIT);
CONV_OP_IMPLEMENTED_MASK_ = getConstant(Constants.GC_CONV_OP_IMPLEMENTED_MASK);
//sun.reflect.Reflection.registerMethodsToFilter(MethodHandleImpl.class, "init");
} catch (UnsatisfiedLinkError ee) {
// ignore; if we use init() methods later we'll see linkage errors
JVM_SUPPORT_ = false;
JVM_PUSH_LIMIT_ = 3; // arbitrary
JVM_STACK_MOVE_UNIT_ = -1; // arbitrary
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_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_;
throw ee; // just die; hopeless to try to run with an older JVM
}
JVM_SUPPORT = JVM_SUPPORT_;
JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_;
JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_;
if (CONV_OP_IMPLEMENTED_MASK_ == 0)
......@@ -191,7 +175,7 @@ class MethodHandleNatives {
// AdapterMethodHandle
/** 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.
*/
static final int
......@@ -292,7 +276,7 @@ class MethodHandleNatives {
return true;
}
static {
if (JVM_SUPPORT) verifyConstants();
verifyConstants();
}
// Up-calls from the JVM.
......@@ -305,28 +289,47 @@ class MethodHandleNatives {
String name, MethodType type,
Object info,
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.
*/
static MethodType findMethodHandleType(Class<?> rtype, Class<?>[] ptypes) {
MethodType.genericMethodType(0); // trigger initialization
return MethodTypeImpl.makeImpl(Access.TOKEN, rtype, ptypes, true);
return MethodType.makeImpl(rtype, ptypes, true);
}
/**
* The JVM wants to use a MethodType with invokeGeneric. Give the runtime fair warning.
*/
static void notifyGenericMethodType(MethodType type) {
try {
// Trigger adapter creation.
InvokeGeneric.genericInvokerOf(type);
} catch (Exception ex) {
Error err = new InternalError("Exception while resolving invokeGeneric");
err.initCause(ex);
throw err;
type.form().notifyGenericMethodType();
}
/**
* The JVM wants to raise an exception. Here's the path.
*/
static void raiseException(int code, Object actual, Object required) {
String message;
// disregard the identity of the actual object, if it is not a class:
if (!(actual instanceof Class) && !(actual instanceof MethodType))
actual = actual.getClass();
if (actual != null)
message = "required "+required+" but encountered "+actual;
else
message = "required "+required;
switch (code) {
case 192: // checkcast
throw new ClassCastException(message);
default:
throw new InternalError("unexpected code "+code+": "+message);
}
}
......
/*
* 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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -23,87 +23,70 @@
* questions.
*/
package sun.dyn;
import sun.reflect.Reflection;
package java.dyn;
/**
* Access control to this package.
* Classes in other packages can attempt to acquire the access token,
* but will fail if they are not recognized as friends.
* Certain methods in this package, although public, require a non-null
* access token in order to proceed; they act like package-private methods.
* @author jrose
* This class consists exclusively of static names internal to the
* method handle implementation.
* Usage: {@code import static java.dyn.MethodHandleStatics.*}
* @author John Rose, JSR 292 EG
*/
/*non-public*/ class MethodHandleStatics {
public class Access {
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."
};
private MethodHandleStatics() { } // do not instantiate
/**
* The following object is NOT public. That's the point of the pattern.
* It is package-private, so that any member of this package
* can acquire the access token, and give it away to trusted friends.
*/
static final Access TOKEN = new Access();
/*non-public*/ static String getNameString(MethodHandle target, MethodType type) {
if (type == null)
type = target.type();
MemberName name = null;
if (target != null)
name = MethodHandleNatives.getMethodName(target);
if (name == null)
return "invoke" + type;
return name.getName() + type;
}
/**
* @return Access.TOKEN, if the caller is a friend of this package
*/
public static Access getToken() {
Class<?> callc = Reflection.getCallerClass(2);
if (isFriend(callc))
return TOKEN;
else
throw new IllegalAccessError("bad caller: " + callc);
/*non-public*/ static String getNameString(MethodHandle target, MethodHandle typeHolder) {
return getNameString(target, typeHolder == null ? (MethodType) null : typeHolder.type());
}
/** Is the given name the name of a class which could be our friend? */
public static boolean isFriendName(String name) {
for (String friend : FRIENDS) {
if (name.startsWith(friend))
return true;
}
return false;
/*non-public*/ static String getNameString(MethodHandle target) {
return getNameString(target, (MethodType) null);
}
/** Is the given class a friend? True if {@link #isFriendName},
* and the given class also shares a class loader with us.
*/
public static boolean isFriend(Class<?> c) {
return isFriendName(c.getName()) && c.getClassLoader() == CLASS_LOADER;
/*non-public*/ static String addTypeString(Object obj, MethodHandle target) {
String str = String.valueOf(obj);
if (target == null) return str;
int paren = str.indexOf('(');
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);
}
/**
* Throw an IllegalAccessError if the caller does not possess
* the Access.TOKEN.
* @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);
// handy shared exception makers (they simplify the common case code)
/*non-public*/ static RuntimeException newIllegalStateException(String message) {
return new IllegalStateException(message);
}
private static void fail() {
final int CALLER_DEPTH = 3;
// 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 newIllegalStateException(String message, Object obj) {
return new IllegalStateException(message(message, obj));
}
static {
//sun.reflect.Reflection.registerMethodsToFilter(MH.class, "getToken");
/*non-public*/ static RuntimeException newIllegalArgumentException(String message) {
return new IllegalArgumentException(message);
}
/*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;
import java.util.Collections;
import java.util.HashMap;
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 static sun.dyn.MemberName.newIllegalArgumentException;
import static java.dyn.MethodHandleStatics.*;
/**
* A method type represents the arguments and return type accepted and
......@@ -96,34 +92,6 @@ class MethodType implements java.io.Serializable {
private MethodType wrapAlt; // alternative wrapped/unwrapped version
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.
*/
......@@ -134,6 +102,10 @@ class MethodType implements java.io.Serializable {
this.ptypes = ptypes;
}
/*trusted*/ MethodTypeForm form() { return form; }
/*trusted*/ Class<?> rtype() { return rtype; }
/*trusted*/ Class<?>[] ptypes() { return ptypes; }
private static void checkRtype(Class<?> rtype) {
rtype.equals(rtype); // null check
}
......@@ -253,7 +225,7 @@ class MethodType implements java.io.Serializable {
* @param trusted whether the ptypes can be used without cloning
* @return the unique method type of the desired structure
*/
private static
/*trusted*/ static
MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
if (ptypes == null || ptypes.length == 0) {
ptypes = NO_PTYPES; trusted = true;
......@@ -269,7 +241,12 @@ class MethodType implements java.io.Serializable {
// defensively copy the array passed in by the user
mt1 = new MethodType(rtype, ptypes.clone());
// 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) {
mt0 = internTable.get(mt1);
if (mt0 != null)
......@@ -279,12 +256,6 @@ class MethodType implements java.io.Serializable {
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];
/**
......@@ -535,7 +506,7 @@ class MethodType implements java.io.Serializable {
MethodType wt = pt.wrapAlt;
if (wt == null) {
// fill in lazily
wt = MethodTypeImpl.canonicalize(pt, MethodTypeImpl.WRAP, MethodTypeImpl.WRAP);
wt = MethodTypeForm.canonicalize(pt, MethodTypeForm.WRAP, MethodTypeForm.WRAP);
assert(wt != null);
pt.wrapAlt = wt;
}
......@@ -547,7 +518,7 @@ class MethodType implements java.io.Serializable {
MethodType uwt = wt.wrapAlt;
if (uwt == null) {
// fill in lazily
uwt = MethodTypeImpl.canonicalize(wt, MethodTypeImpl.UNWRAP, MethodTypeImpl.UNWRAP);
uwt = MethodTypeForm.canonicalize(wt, MethodTypeForm.UNWRAP, MethodTypeForm.UNWRAP);
if (uwt == null)
uwt = wt; // type has no wrappers or prims at all
wt.wrapAlt = uwt;
......@@ -666,12 +637,18 @@ class MethodType implements java.io.Serializable {
* This method is included for the benfit of applications that must
* generate bytecodes that process method handles and invokedynamic.
* @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();
}
/*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
* the given position, which must be in the range of 0 to
* {@code parameterCount} inclusive. Successive parameters are
......@@ -694,9 +671,8 @@ class MethodType implements java.io.Serializable {
* @return the index of the (shallowest) JVM stack slot transmitting the
* given parameter
* @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)
parameterType(num); // force a range check
return form.parameterToArgSlot(num-1);
......@@ -710,9 +686,9 @@ class MethodType implements java.io.Serializable {
* This method is included for the benfit of applications that must
* 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
* @deprecated Will be removed for PFD.
* Will be removed for PFD.
*/
public int returnSlotCount() {
/*non-public*/ int 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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -25,15 +25,452 @@
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
*/
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) {
super(erasedType);
public int parameterCount() { // # outgoing values
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 @@
package java.dyn;
import sun.dyn.*;
import sun.dyn.empty.Empty;
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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -23,14 +23,14 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import java.dyn.*;
import sun.dyn.util.ValueConversions;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import sun.dyn.util.ValueConversions;
import static sun.dyn.MemberName.newIllegalArgumentException;
import static java.dyn.MethodHandleStatics.*;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/**
* Generic spread adapter.
......@@ -110,7 +110,7 @@ class SpreadGeneric {
static SpreadGeneric of(MethodType targetType, int spreadCount) {
if (targetType != targetType.generic())
throw new UnsupportedOperationException("NYI type="+targetType);
MethodTypeImpl form = MethodTypeImpl.of(targetType);
MethodTypeForm form = targetType.form();
int outcount = form.parameterCount();
assert(spreadCount <= outcount);
SpreadGeneric[] spreadGens = form.spreadGeneric;
......@@ -129,7 +129,7 @@ class SpreadGeneric {
// This mini-api is called from an Adapter to manage the spread.
/** A check/coercion that happens once before any selections. */
protected Object check(Object av, int n) {
MethodHandleImpl.checkSpreadArgument(av, n);
checkSpreadArgument(av, n);
return av;
}
......@@ -166,7 +166,7 @@ class SpreadGeneric {
// see if it has the required invoke method
MethodHandle entryPoint = null;
try {
entryPoint = MethodHandleImpl.IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
entryPoint = IMPL_LOOKUP.findSpecial(acls, iname, entryType, acls);
} catch (ReflectiveOperationException ex) {
}
if (entryPoint == null) continue;
......@@ -221,21 +221,21 @@ class SpreadGeneric {
@Override
public String toString() {
return MethodHandleImpl.addTypeString(target, this);
return addTypeString(target, this);
}
static final MethodHandle NO_ENTRY = ValueConversions.identity();
protected boolean isPrototype() { return target == null; }
protected Adapter(SpreadGeneric outer) {
super(Access.TOKEN, NO_ENTRY);
super(NO_ENTRY);
this.outer = outer;
this.target = null;
assert(isPrototype());
}
protected Adapter(SpreadGeneric outer, MethodHandle target) {
super(Access.TOKEN, outer.entryPoint);
super(outer.entryPoint);
this.outer = outer;
this.target = target;
}
......@@ -251,7 +251,7 @@ class SpreadGeneric {
return outer.select(av, n);
}
static private final String CLASS_PREFIX; // "sun.dyn.SpreadGeneric$"
static private final String CLASS_PREFIX; // "java.dyn.SpreadGeneric$"
static {
String aname = Adapter.class.getName();
String sname = Adapter.class.getSimpleName();
......
......@@ -23,15 +23,14 @@
* questions.
*/
package sun.dyn;
package java.dyn;
import java.dyn.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import sun.dyn.util.ValueConversions;
import sun.dyn.util.Wrapper;
import static sun.dyn.MemberName.newIllegalArgumentException;
import static sun.dyn.MethodTypeImpl.invokers;
import static java.dyn.MethodHandleStatics.*;
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
/**
* Adapters which mediate between incoming calls which are not generic
......@@ -73,7 +72,7 @@ class ToGeneric {
assert(entryType.erase() == entryType); // for now
// incoming call will first "forget" all reference types except Object
this.entryType = entryType;
MethodHandle invoker0 = invokers(entryType.generic()).exactInvoker();
MethodHandle invoker0 = entryType.generic().invokers().exactInvoker();
MethodType rawEntryTypeInit;
Adapter ad = findAdapter(rawEntryTypeInit = entryType);
if (ad != null) {
......@@ -89,15 +88,15 @@ class ToGeneric {
}
// 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
this.primsAtEndOrder = MethodTypeImpl.primsAtEndOrder(entryType);
this.primsAtEndOrder = MethodTypeForm.primsAtEndOrder(entryType);
if (primsAtEndOrder != null) {
// reordering is required; build on top of a simpler ToGeneric
ToGeneric va2 = ToGeneric.of(primsAtEnd);
this.adapter = va2.adapter;
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);
// example: for entryType of (int,Object,Object), the reordered
// type is (Object,Object,int) and the order is {1,2,0},
......@@ -107,7 +106,7 @@ class ToGeneric {
// after any needed argument reordering, it will reinterpret
// primitive arguments according to their "raw" types int/long
MethodType intsAtEnd = MethodTypeImpl.of(primsAtEnd).primsAsInts();
MethodType intsAtEnd = primsAtEnd.form().primsAsInts();
ad = findAdapter(rawEntryTypeInit = intsAtEnd);
MethodHandle rawEntryPoint;
if (ad != null) {
......@@ -116,7 +115,7 @@ class ToGeneric {
// Perhaps the adapter is available only for longs.
// If so, we can use it, but there will have to be a little
// more stack motion on each call.
MethodType longsAtEnd = MethodTypeImpl.of(primsAtEnd).primsAsLongs();
MethodType longsAtEnd = primsAtEnd.form().primsAsLongs();
ad = findAdapter(rawEntryTypeInit = longsAtEnd);
if (ad != null) {
MethodType eptWithLongs = longsAtEnd.insertParameterTypes(0, ad.getClass());
......@@ -128,7 +127,7 @@ class ToGeneric {
assert(midType.parameterType(i) == long.class);
assert(eptWithInts.parameterType(i) == int.class);
MethodType nextType = midType.changeParameterType(i, int.class);
rawEntryPoint = MethodHandle.convertArguments(Access.TOKEN,
rawEntryPoint = MethodHandleImpl.convertArguments(
rawEntryPoint, nextType, midType, null);
midType = nextType;
}
......@@ -143,7 +142,7 @@ class ToGeneric {
}
MethodType tepType = entryType.insertParameterTypes(0, ad.getClass());
this.entryPoint =
AdapterMethodHandle.makeRetypeRaw(Access.TOKEN, tepType, rawEntryPoint);
AdapterMethodHandle.makeRetypeRaw(tepType, rawEntryPoint);
if (this.entryPoint == null)
throw new UnsupportedOperationException("cannot retype to "+entryType
+" from "+rawEntryPoint.type().dropParameterTypes(0, 1));
......@@ -168,7 +167,7 @@ class ToGeneric {
assert(src.isPrimitive() && dst.isPrimitive());
if (filteredInvoker == null) {
filteredInvoker =
AdapterMethodHandle.makeCheckCast(Access.TOKEN,
AdapterMethodHandle.makeCheckCast(
invoker.type().generic(), invoker, 0, MethodHandle.class);
if (filteredInvoker == null) throw new UnsupportedOperationException("NYI");
}
......@@ -177,7 +176,7 @@ class ToGeneric {
if (filteredInvoker == null) throw new InternalError();
}
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 {
// retype erased reference arguments (the cast makes it safe to do this)
MethodType tepType = type.insertParameterTypes(0, adapter.getClass());
MethodHandle typedEntryPoint =
AdapterMethodHandle.makeRetypeRaw(Access.TOKEN, tepType, entryPoint);
AdapterMethodHandle.makeRetypeRaw(tepType, entryPoint);
return adapter.makeInstance(typedEntryPoint, invoker, convert, genericTarget);
}
......@@ -248,7 +247,7 @@ class ToGeneric {
/** Return the adapter information for this type's erasure. */
static ToGeneric of(MethodType type) {
MethodTypeImpl form = MethodTypeImpl.of(type);
MethodTypeForm form = type.form();
ToGeneric toGen = form.toGeneric;
if (toGen == null)
form.toGeneric = toGen = new ToGeneric(form.erasedType());
......@@ -262,7 +261,7 @@ class ToGeneric {
/* Create an adapter for the given incoming call type. */
static Adapter findAdapter(MethodType entryPointType) {
MethodTypeImpl form = MethodTypeImpl.of(entryPointType);
MethodTypeForm form = entryPointType.form();
Class<?> rtype = entryPointType.returnType();
int argc = form.parameterCount();
int lac = form.longPrimitiveParameterCount();
......@@ -283,7 +282,7 @@ class ToGeneric {
for (String iname : inames) {
MethodHandle entryPoint = null;
try {
entryPoint = MethodHandleImpl.IMPL_LOOKUP.
entryPoint = IMPL_LOOKUP.
findSpecial(acls, iname, entryPointType, acls);
} catch (ReflectiveOperationException ex) {
}
......@@ -338,13 +337,13 @@ class ToGeneric {
@Override
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; }
/* Prototype constructor. */
protected Adapter(MethodHandle entryPoint) {
super(Access.TOKEN, entryPoint);
super(entryPoint);
this.invoker = null;
this.convert = entryPoint;
this.target = null;
......@@ -356,7 +355,7 @@ class ToGeneric {
}
protected Adapter(MethodHandle entryPoint, MethodHandle invoker, MethodHandle convert, MethodHandle target) {
super(Access.TOKEN, entryPoint);
super(entryPoint);
this.invoker = invoker;
this.convert = convert;
this.target = target;
......@@ -396,7 +395,7 @@ class ToGeneric {
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); }
static private final String CLASS_PREFIX; // "sun.dyn.ToGeneric$"
static private final String CLASS_PREFIX; // "java.dyn.ToGeneric$"
static {
String aname = Adapter.class.getName();
String sname = Adapter.class.getSimpleName();
......
......@@ -25,8 +25,6 @@
package java.dyn;
import java.util.List;
/**
* 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
......
/*
* 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.
*
* 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 @@
package sun.dyn.util;
import java.dyn.*;
import java.dyn.MethodHandle;
import java.dyn.MethodHandles;
import java.dyn.MethodHandles.Lookup;
import java.dyn.MethodType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
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 {
private static final Access IMPL_TOKEN = Access.getToken();
private static final Lookup IMPL_LOOKUP = MethodHandleImpl.getLookup(IMPL_TOKEN);
private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
private static EnumMap<Wrapper, MethodHandle>[] newWrapperCaches(int n) {
@SuppressWarnings("unchecked")
......@@ -157,7 +154,7 @@ public class ValueConversions {
mh = null;
}
} else {
mh = retype(type, unbox(wrap, !exact, raw));
mh = unbox(wrap, !exact, raw).asType(type);
}
if (mh != null) {
cache.put(wrap, mh);
......@@ -293,7 +290,7 @@ public class ValueConversions {
mh = null;
}
} else {
mh = retype(type.erase(), box(wrap, !exact, raw));
mh = box(wrap, !exact, raw).asType(type.erase());
}
if (mh != null) {
cache.put(wrap, mh);
......@@ -412,7 +409,7 @@ public class ValueConversions {
mh = null;
}
} else {
mh = retype(IDENTITY.type(), rebox(wrap, !exact));
mh = rebox(wrap, !exact).asType(IDENTITY.type());
}
if (mh != null) {
cache.put(wrap, mh);
......@@ -504,8 +501,8 @@ public class ValueConversions {
// use the raw method
Wrapper rawWrap = wrap.rawPrimitive();
if (rawWrap != wrap) {
mh = retype(type, zeroConstantFunction(rawWrap));
if (mh == null && rawWrap != wrap) {
mh = MethodHandles.explicitCastArguments(zeroConstantFunction(rawWrap), type);
}
if (mh != null) {
cache.put(wrap, mh);
......@@ -552,6 +549,22 @@ public class ValueConversions {
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.
* @param x an arbitrary long value
......@@ -561,6 +574,14 @@ public class ValueConversions {
return x;
}
static float identity(float x) {
return x;
}
static double identity(double x) {
return x;
}
/**
* Identity function, with reference cast.
* @param t an arbitrary reference type
......@@ -590,7 +611,9 @@ public class ValueConversions {
IGNORE = IMPL_LOOKUP.findStatic(ValueConversions.class, "ignore", ignoreType);
EMPTY = IMPL_LOOKUP.findStatic(ValueConversions.class, "empty", ignoreType.dropParameterTypes(0, 1));
} catch (Exception ex) {
throw uncaughtException(ex);
Error err = new InternalError("uncaught exception");
err.initCause(ex);
throw err;
}
}
......@@ -622,7 +645,8 @@ public class ValueConversions {
mh = MethodHandles.insertArguments(CAST_REFERENCE, 0, type);
if (exact) {
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)
cache.put(wrap, mh);
......@@ -634,15 +658,11 @@ public class ValueConversions {
}
public static MethodHandle identity(Class<?> type) {
if (type == Object.class)
return IDENTITY;
else if (!type.isPrimitive())
return retype(MethodType.methodType(type, type), IDENTITY);
else
return identity(Wrapper.forPrimitiveType(type));
// This stuff has been moved into MethodHandles:
return MethodHandles.identity(type);
}
static MethodHandle identity(Wrapper wrap) {
public static MethodHandle identity(Wrapper wrap) {
EnumMap<Wrapper, MethodHandle> cache = CONSTANT_FUNCTIONS[1];
MethodHandle mh = cache.get(wrap);
if (mh != null) {
......@@ -665,12 +685,6 @@ public class ValueConversions {
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) {
cache.put(wrap, mh);
return mh;
......@@ -678,10 +692,6 @@ public class ValueConversions {
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 Object[] makeArray(Object... args) { return args; }
private static Object[] array() { return NO_ARGS_ARRAY; }
......
......@@ -26,9 +26,6 @@
package sun.dyn.util;
import java.lang.reflect.Modifier;
import sun.dyn.MemberName;
import sun.dyn.MethodHandleImpl;
import sun.dyn.empty.Empty;
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 @@
package test.java.dyn;
import sun.dyn.MemberName;
import java.dyn.MethodType;
import java.lang.reflect.Method;
......@@ -163,18 +162,6 @@ public class MethodTypeTest {
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.
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册