8254354: Add a withInvokeExactBehavior() VarHandle combinator
Reviewed-by: psandoz, chegar
This commit is contained in:
parent
d6f1463cb3
commit
0a41ca6b75
@ -53,7 +53,12 @@ import java.util.function.BiFunction;
|
||||
private final Class<?>[] coordinates;
|
||||
|
||||
IndirectVarHandle(VarHandle target, Class<?> value, Class<?>[] coordinates, BiFunction<AccessMode, MethodHandle, MethodHandle> handleFactory) {
|
||||
super(new VarForm(value, coordinates));
|
||||
this(target, value, coordinates, handleFactory, new VarForm(value, coordinates), false);
|
||||
}
|
||||
|
||||
private IndirectVarHandle(VarHandle target, Class<?> value, Class<?>[] coordinates,
|
||||
BiFunction<AccessMode, MethodHandle, MethodHandle> handleFactory, VarForm form, boolean exact) {
|
||||
super(form, exact);
|
||||
this.handleFactory = handleFactory;
|
||||
this.target = target;
|
||||
this.directTarget = target.asDirect();
|
||||
@ -72,8 +77,8 @@ import java.util.function.BiFunction;
|
||||
}
|
||||
|
||||
@Override
|
||||
MethodType accessModeTypeUncached(AccessMode accessMode) {
|
||||
return accessMode.at.accessModeType(directTarget.getClass(), value, coordinates);
|
||||
MethodType accessModeTypeUncached(AccessType at) {
|
||||
return at.accessModeType(null, value, coordinates);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -90,6 +95,20 @@ import java.util.function.BiFunction;
|
||||
return target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VarHandle withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior()
|
||||
? this
|
||||
: new IndirectVarHandle(target, value, coordinates, handleFactory, vform, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VarHandle withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior()
|
||||
? this
|
||||
: new IndirectVarHandle(target, value, coordinates, handleFactory, vform, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ForceInline
|
||||
MethodHandle getMethodHandle(int mode) {
|
||||
|
@ -27,6 +27,7 @@ package java.lang.invoke;
|
||||
|
||||
import jdk.internal.vm.annotation.DontInline;
|
||||
import jdk.internal.vm.annotation.ForceInline;
|
||||
import jdk.internal.vm.annotation.Hidden;
|
||||
import jdk.internal.vm.annotation.Stable;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
@ -463,7 +464,12 @@ class Invokers {
|
||||
|
||||
@ForceInline
|
||||
/*non-public*/
|
||||
@Hidden
|
||||
static MethodHandle checkVarHandleGenericType(VarHandle handle, VarHandle.AccessDescriptor ad) {
|
||||
if (handle.hasInvokeExactBehavior() && handle.accessModeType(ad.type) != ad.symbolicMethodTypeExact) {
|
||||
throw new WrongMethodTypeException("expected " + handle.accessModeType(ad.type) + " but found "
|
||||
+ ad.symbolicMethodTypeExact);
|
||||
}
|
||||
// Test for exact match on invoker types
|
||||
// TODO match with erased types and add cast of return value to lambda form
|
||||
MethodHandle mh = handle.getMethodHandle(ad.mode);
|
||||
|
@ -42,8 +42,8 @@ abstract class MemoryAccessVarHandleBase extends VarHandle {
|
||||
/** alignment constraint (in bytes, expressed as a bit mask) **/
|
||||
final long alignmentMask;
|
||||
|
||||
MemoryAccessVarHandleBase(VarForm form, boolean be, long length, long offset, long alignmentMask) {
|
||||
super(form);
|
||||
MemoryAccessVarHandleBase(VarForm form, boolean be, long length, long offset, long alignmentMask, boolean exact) {
|
||||
super(form, exact);
|
||||
this.be = be;
|
||||
this.length = length;
|
||||
this.offset = offset;
|
||||
|
@ -71,6 +71,7 @@ import static jdk.internal.org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
|
||||
import static jdk.internal.org.objectweb.asm.Opcodes.LALOAD;
|
||||
import static jdk.internal.org.objectweb.asm.Opcodes.LASTORE;
|
||||
import static jdk.internal.org.objectweb.asm.Opcodes.LLOAD;
|
||||
import static jdk.internal.org.objectweb.asm.Opcodes.NEW;
|
||||
import static jdk.internal.org.objectweb.asm.Opcodes.NEWARRAY;
|
||||
import static jdk.internal.org.objectweb.asm.Opcodes.PUTFIELD;
|
||||
import static jdk.internal.org.objectweb.asm.Opcodes.PUTSTATIC;
|
||||
@ -95,6 +96,12 @@ class MemoryAccessVarHandleGenerator {
|
||||
private final static MethodHandle ADD_OFFSETS_HANDLE;
|
||||
private final static MethodHandle MUL_OFFSETS_HANDLE;
|
||||
|
||||
private final static MethodType CONSTR_TYPE = MethodType.methodType(void.class, VarForm.class,
|
||||
boolean.class, long.class, long.class, long.class, boolean.class, long[].class);
|
||||
// MemoryAccessVarHandleBase
|
||||
private final static MethodType SUPER_CONTR_TYPE = MethodType.methodType(void.class, VarForm.class,
|
||||
boolean.class, long.class, long.class, long.class, boolean.class);
|
||||
|
||||
static {
|
||||
helperClassCache = new HashMap<>();
|
||||
helperClassCache.put(byte.class, MemoryAccessVarHandleByteHelper.class);
|
||||
@ -140,7 +147,7 @@ class MemoryAccessVarHandleGenerator {
|
||||
Arrays.fill(components, long.class);
|
||||
this.form = new VarForm(BASE_CLASS, MemoryAddressProxy.class, carrier, components);
|
||||
this.helperClass = helperClassCache.get(carrier);
|
||||
this.implClassName = helperClass.getName().replace('.', '/') + dimensions;
|
||||
this.implClassName = internalName(helperClass) + dimensions;
|
||||
// live constants
|
||||
Class<?>[] intermediate = new Class<?>[dimensions];
|
||||
Arrays.fill(intermediate, long.class);
|
||||
@ -164,8 +171,7 @@ class MemoryAccessVarHandleGenerator {
|
||||
|
||||
VarForm form = new VarForm(implCls, MemoryAddressProxy.class, carrier, components);
|
||||
|
||||
MethodType constrType = MethodType.methodType(void.class, VarForm.class, boolean.class, long.class, long.class, long.class, long[].class);
|
||||
MethodHandle constr = lookup.findConstructor(implCls, constrType);
|
||||
MethodHandle constr = lookup.findConstructor(implCls, CONSTR_TYPE);
|
||||
constr = MethodHandles.insertArguments(constr, 0, form);
|
||||
return constr;
|
||||
} catch (Throwable ex) {
|
||||
@ -202,6 +208,9 @@ class MemoryAccessVarHandleGenerator {
|
||||
|
||||
addCarrierAccessor(cw);
|
||||
|
||||
addAsExact(cw);
|
||||
addAsGeneric(cw);
|
||||
|
||||
for (VarHandle.AccessMode mode : VarHandle.AccessMode.values()) {
|
||||
addAccessModeMethodIfNeeded(mode, cw);
|
||||
}
|
||||
@ -253,23 +262,23 @@ class MemoryAccessVarHandleGenerator {
|
||||
}
|
||||
|
||||
void addConstructor(ClassWriter cw) {
|
||||
MethodType constrType = MethodType.methodType(void.class, VarForm.class, boolean.class, long.class, long.class, long.class, long[].class);
|
||||
MethodVisitor mv = cw.visitMethod(0, "<init>", constrType.toMethodDescriptorString(), null, null);
|
||||
MethodVisitor mv = cw.visitMethod(0, "<init>", CONSTR_TYPE.toMethodDescriptorString(), null, null);
|
||||
mv.visitCode();
|
||||
//super call
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitVarInsn(ALOAD, 1); // vform
|
||||
mv.visitTypeInsn(CHECKCAST, Type.getInternalName(VarForm.class));
|
||||
mv.visitVarInsn(ILOAD, 2);
|
||||
mv.visitVarInsn(LLOAD, 3);
|
||||
mv.visitVarInsn(LLOAD, 5);
|
||||
mv.visitVarInsn(LLOAD, 7);
|
||||
mv.visitVarInsn(ILOAD, 2); // be
|
||||
mv.visitVarInsn(LLOAD, 3); // length
|
||||
mv.visitVarInsn(LLOAD, 5); // offset
|
||||
mv.visitVarInsn(LLOAD, 7); // alignmentMask
|
||||
mv.visitVarInsn(ILOAD, 9); // exact
|
||||
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(BASE_CLASS), "<init>",
|
||||
MethodType.methodType(void.class, VarForm.class, boolean.class, long.class, long.class, long.class).toMethodDescriptorString(), false);
|
||||
SUPER_CONTR_TYPE.toMethodDescriptorString(), false);
|
||||
//init dimensions
|
||||
for (int i = 0 ; i < dimensions ; i++) {
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitVarInsn(ALOAD, 9);
|
||||
mv.visitVarInsn(ALOAD, 10);
|
||||
mv.visitLdcInsn(i);
|
||||
mv.visitInsn(LALOAD);
|
||||
mv.visitFieldInsn(PUTFIELD, implClassName, "dim" + i, "J");
|
||||
@ -280,11 +289,10 @@ class MemoryAccessVarHandleGenerator {
|
||||
}
|
||||
|
||||
void addAccessModeTypeMethod(ClassWriter cw) {
|
||||
MethodType modeMethType = MethodType.methodType(MethodType.class, VarHandle.AccessMode.class);
|
||||
MethodType modeMethType = MethodType.methodType(MethodType.class, VarHandle.AccessType.class);
|
||||
MethodVisitor mv = cw.visitMethod(ACC_FINAL, "accessModeTypeUncached", modeMethType.toMethodDescriptorString(), null, null);
|
||||
mv.visitCode();
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitFieldInsn(GETFIELD, Type.getInternalName(VarHandle.AccessMode.class), "at", VarHandle.AccessType.class.descriptorString());
|
||||
mv.visitLdcInsn(Type.getType(MemoryAddressProxy.class));
|
||||
mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Class.class));
|
||||
mv.visitFieldInsn(GETSTATIC, implClassName, "carrier", Class.class.descriptorString());
|
||||
@ -409,6 +417,38 @@ class MemoryAccessVarHandleGenerator {
|
||||
mv.visitEnd();
|
||||
}
|
||||
|
||||
private void addAsExact(ClassWriter cw) {
|
||||
addAsExactOrAsGeneric(cw, "asExact", true);
|
||||
}
|
||||
|
||||
private void addAsGeneric(ClassWriter cw) {
|
||||
addAsExactOrAsGeneric(cw, "asGeneric", false);
|
||||
}
|
||||
|
||||
private void addAsExactOrAsGeneric(ClassWriter cw, String name, boolean exact) {
|
||||
MethodVisitor mv = cw.visitMethod(ACC_FINAL, name, "()Ljava/lang/invoke/VarHandle;", null, null);
|
||||
mv.visitCode();
|
||||
mv.visitTypeInsn(NEW, implClassName);
|
||||
mv.visitInsn(DUP);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, internalName(VarHandle.class), "vform", VarForm.class.descriptorString());
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, internalName(MemoryAccessVarHandleBase.class), "be", boolean.class.descriptorString());
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, internalName(MemoryAccessVarHandleBase.class), "length", long.class.descriptorString());
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, internalName(MemoryAccessVarHandleBase.class), "offset", long.class.descriptorString());
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, internalName(MemoryAccessVarHandleBase.class), "alignmentMask", long.class.descriptorString());
|
||||
mv.visitIntInsn(BIPUSH, exact ? 1 : 0);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, implClassName, "strides", "()[J", false);
|
||||
mv.visitMethodInsn(INVOKESPECIAL, implClassName, "<init>", CONSTR_TYPE.descriptorString(), false);
|
||||
mv.visitInsn(ARETURN);
|
||||
mv.visitMaxs(0, 0);
|
||||
mv.visitEnd();
|
||||
}
|
||||
|
||||
// shared code generation helpers
|
||||
|
||||
private static int getSlotsForType(Class<?> c) {
|
||||
@ -418,6 +458,10 @@ class MemoryAccessVarHandleGenerator {
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static String internalName(Class<?> cls) {
|
||||
return cls.getName().replace('.', '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an actual return instruction conforming to the given return type.
|
||||
*/
|
||||
|
@ -282,8 +282,8 @@ import static java.lang.invoke.MethodHandleStatics.UNSAFE;
|
||||
* match fails, it means that the access mode method which the caller is
|
||||
* invoking is not present on the individual VarHandle being invoked.
|
||||
*
|
||||
* <p>
|
||||
* Invocation of an access mode method behaves as if an invocation of
|
||||
* <p id="invoke-behavior">
|
||||
* Invocation of an access mode method behaves, by default, as if an invocation of
|
||||
* {@link MethodHandle#invoke}, where the receiving method handle accepts the
|
||||
* VarHandle instance as the leading argument. More specifically, the
|
||||
* following, where {@code {access-mode}} corresponds to the access mode method
|
||||
@ -318,7 +318,7 @@ import static java.lang.invoke.MethodHandleStatics.UNSAFE;
|
||||
* widen primitive values, as if by {@link MethodHandle#asType asType} (see also
|
||||
* {@link MethodHandles#varHandleInvoker}).
|
||||
*
|
||||
* More concisely, such behaviour is equivalent to:
|
||||
* More concisely, such behavior is equivalent to:
|
||||
* <pre> {@code
|
||||
* VarHandle vh = ..
|
||||
* VarHandle.AccessMode am = VarHandle.AccessMode.valueFromMethodName("{access-mode}");
|
||||
@ -328,6 +328,37 @@ import static java.lang.invoke.MethodHandleStatics.UNSAFE;
|
||||
* }</pre>
|
||||
* Where, in this case, the method handle is bound to the VarHandle instance.
|
||||
*
|
||||
* <p id="invoke-exact-behavior">
|
||||
* A VarHandle's invocation behavior can be adjusted (see {@link #withInvokeExactBehavior}) such that invocation of
|
||||
* an access mode method behaves as if invocation of {@link MethodHandle#invokeExact},
|
||||
* where the receiving method handle accepts the VarHandle instance as the leading argument.
|
||||
* More specifically, the following, where {@code {access-mode}} corresponds to the access mode method
|
||||
* name:
|
||||
* <pre> {@code
|
||||
* VarHandle vh = ..
|
||||
* R r = (R) vh.{access-mode}(p1, p2, ..., pN);
|
||||
* }</pre>
|
||||
* behaves as if:
|
||||
* <pre> {@code
|
||||
* VarHandle vh = ..
|
||||
* VarHandle.AccessMode am = VarHandle.AccessMode.valueFromMethodName("{access-mode}");
|
||||
* MethodHandle mh = MethodHandles.varHandleExactInvoker(
|
||||
* am,
|
||||
* vh.accessModeType(am));
|
||||
*
|
||||
* R r = (R) mh.invokeExact(vh, p1, p2, ..., pN)
|
||||
* }</pre>
|
||||
* (modulo access mode methods do not declare throwing of {@code Throwable}).
|
||||
*
|
||||
* More concisely, such behavior is equivalent to:
|
||||
* <pre> {@code
|
||||
* VarHandle vh = ..
|
||||
* VarHandle.AccessMode am = VarHandle.AccessMode.valueFromMethodName("{access-mode}");
|
||||
* MethodHandle mh = vh.toMethodHandle(am);
|
||||
*
|
||||
* R r = (R) mh.invokeExact(p1, p2, ..., pN)
|
||||
* }</pre>
|
||||
* Where, in this case, the method handle is bound to the VarHandle instance.
|
||||
*
|
||||
* <h2>Invocation checking</h2>
|
||||
* In typical programs, VarHandle access mode type matching will usually
|
||||
@ -425,7 +456,7 @@ import static java.lang.invoke.MethodHandleStatics.UNSAFE;
|
||||
* {@link java.lang.invoke.MethodHandles#varHandleInvoker}. The
|
||||
* {@link java.lang.invoke.MethodHandles.Lookup#findVirtual Lookup.findVirtual}
|
||||
* API is also able to return a method handle to call an access mode method for
|
||||
* any specified access mode type and is equivalent in behaviour to
|
||||
* any specified access mode type and is equivalent in behavior to
|
||||
* {@link java.lang.invoke.MethodHandles#varHandleInvoker}.
|
||||
*
|
||||
* <h2>Interoperation between VarHandles and Java generics</h2>
|
||||
@ -446,9 +477,15 @@ import static java.lang.invoke.MethodHandleStatics.UNSAFE;
|
||||
*/
|
||||
public abstract class VarHandle implements Constable {
|
||||
final VarForm vform;
|
||||
final boolean exact;
|
||||
|
||||
VarHandle(VarForm vform) {
|
||||
this(vform, false);
|
||||
}
|
||||
|
||||
VarHandle(VarForm vform, boolean exact) {
|
||||
this.vform = vform;
|
||||
this.exact = exact;
|
||||
}
|
||||
|
||||
RuntimeException unsupported() {
|
||||
@ -465,6 +502,18 @@ public abstract class VarHandle implements Constable {
|
||||
|
||||
VarHandle target() { return null; }
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this VarHandle has <a href="#invoke-exact-behavior"><em>invoke-exact behavior</em></a>.
|
||||
*
|
||||
* @see #withInvokeExactBehavior()
|
||||
* @see #withInvokeBehavior()
|
||||
* @return {@code true} if this VarHandle has <a href="#invoke-exact-behavior"><em>invoke-exact behavior</em></a>.
|
||||
* @since 16
|
||||
*/
|
||||
public boolean hasInvokeExactBehavior() {
|
||||
return exact;
|
||||
}
|
||||
|
||||
// Plain accessors
|
||||
|
||||
/**
|
||||
@ -1541,6 +1590,44 @@ public abstract class VarHandle implements Constable {
|
||||
@IntrinsicCandidate
|
||||
Object getAndBitwiseXorRelease(Object... args);
|
||||
|
||||
/**
|
||||
* Returns a VarHandle, with access to the same variable(s) as this VarHandle, but whose
|
||||
* invocation behavior of access mode methods is adjusted to
|
||||
* <a href="#invoke-exact-behavior"><em>invoke-exact behavior</em></a>.
|
||||
* <p>
|
||||
* If this VarHandle already has invoke-exact behavior this VarHandle is returned.
|
||||
* <p>
|
||||
* Invoking {@link #hasInvokeExactBehavior()} on the returned var handle
|
||||
* is guaranteed to return {@code true}.
|
||||
*
|
||||
* @apiNote
|
||||
* Invoke-exact behavior guarantees that upon invocation of an access mode method
|
||||
* the types and arity of the arguments must match the {@link #accessModeType(AccessMode) access mode type},
|
||||
* otherwise a {@link WrongMethodTypeException} is thrown.
|
||||
*
|
||||
* @see #withInvokeBehavior()
|
||||
* @see #hasInvokeExactBehavior()
|
||||
* @return a VarHandle with invoke-exact behavior
|
||||
* @since 16
|
||||
*/
|
||||
public abstract VarHandle withInvokeExactBehavior();
|
||||
|
||||
/**
|
||||
* Returns a VarHandle, with access to the same variable(s) as this VarHandle, but whose
|
||||
* invocation behavior of access mode methods is adjusted to
|
||||
* <a href="#invoke-behavior"><em>invoke behavior</em></a>.
|
||||
* <p>
|
||||
* If this VarHandle already has invoke behavior this VarHandle is returned.
|
||||
* <p>
|
||||
* Invoking {@link #hasInvokeExactBehavior()} on the returned var handle
|
||||
* is guaranteed to return {@code false}.
|
||||
*
|
||||
* @see #withInvokeExactBehavior()
|
||||
* @see #hasInvokeExactBehavior()
|
||||
* @return a VarHandle with invoke behavior
|
||||
* @since 16
|
||||
*/
|
||||
public abstract VarHandle withInvokeBehavior();
|
||||
|
||||
enum AccessType {
|
||||
GET(Object.class),
|
||||
@ -1859,6 +1946,7 @@ public abstract class VarHandle implements Constable {
|
||||
}
|
||||
|
||||
static final class AccessDescriptor {
|
||||
final MethodType symbolicMethodTypeExact;
|
||||
final MethodType symbolicMethodTypeErased;
|
||||
final MethodType symbolicMethodTypeInvoker;
|
||||
final Class<?> returnType;
|
||||
@ -1866,6 +1954,7 @@ public abstract class VarHandle implements Constable {
|
||||
final int mode;
|
||||
|
||||
public AccessDescriptor(MethodType symbolicMethodType, int type, int mode) {
|
||||
this.symbolicMethodTypeExact = symbolicMethodType;
|
||||
this.symbolicMethodTypeErased = symbolicMethodType.erase();
|
||||
this.symbolicMethodTypeInvoker = symbolicMethodType.insertParameterTypes(0, VarHandle.class);
|
||||
this.returnType = symbolicMethodType.returnType();
|
||||
@ -1922,15 +2011,25 @@ public abstract class VarHandle implements Constable {
|
||||
* @return the access mode type for the given access mode
|
||||
*/
|
||||
public final MethodType accessModeType(AccessMode accessMode) {
|
||||
return accessModeType(accessMode.at.ordinal());
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
final MethodType accessModeType(int accessTypeOrdinal) {
|
||||
TypesAndInvokers tis = getTypesAndInvokers();
|
||||
MethodType mt = tis.methodType_table[accessMode.at.ordinal()];
|
||||
MethodType mt = tis.methodType_table[accessTypeOrdinal];
|
||||
if (mt == null) {
|
||||
mt = tis.methodType_table[accessMode.at.ordinal()] =
|
||||
accessModeTypeUncached(accessMode);
|
||||
mt = tis.methodType_table[accessTypeOrdinal] =
|
||||
accessModeTypeUncached(accessTypeOrdinal);
|
||||
}
|
||||
return mt;
|
||||
}
|
||||
abstract MethodType accessModeTypeUncached(AccessMode accessMode);
|
||||
|
||||
final MethodType accessModeTypeUncached(int accessTypeOrdinal) {
|
||||
return accessModeTypeUncached(AccessType.values()[accessTypeOrdinal]);
|
||||
}
|
||||
|
||||
abstract MethodType accessModeTypeUncached(AccessType accessMode);
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the given access mode is supported, otherwise
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -31,8 +31,10 @@ import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@ -43,6 +45,8 @@ import java.util.stream.Stream;
|
||||
import static java.lang.invoke.MethodHandleStatics.UNSAFE;
|
||||
import static java.lang.invoke.MethodHandleStatics.VAR_HANDLE_IDENTITY_ADAPT;
|
||||
import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
final class VarHandles {
|
||||
|
||||
@ -331,7 +335,8 @@ final class VarHandles {
|
||||
.generateHandleFactory());
|
||||
|
||||
try {
|
||||
return maybeAdapt((VarHandle)fac.invoke(be, size, offset, alignmentMask, strides));
|
||||
boolean exact = false;
|
||||
return maybeAdapt((VarHandle)fac.invoke(be, size, offset, alignmentMask, exact, strides));
|
||||
} catch (Throwable ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
@ -341,7 +346,7 @@ final class VarHandles {
|
||||
if (!VAR_HANDLE_IDENTITY_ADAPT) return target;
|
||||
target = filterValue(target,
|
||||
MethodHandles.identity(target.varType()), MethodHandles.identity(target.varType()));
|
||||
MethodType mtype = target.accessModeType(VarHandle.AccessMode.GET).dropParameterTypes(0, 1);
|
||||
MethodType mtype = target.accessModeType(VarHandle.AccessMode.GET);
|
||||
for (int i = 0 ; i < mtype.parameterCount() ; i++) {
|
||||
target = filterCoordinates(target, i, MethodHandles.identity(mtype.parameterType(i)));
|
||||
}
|
||||
@ -671,33 +676,42 @@ final class VarHandles {
|
||||
// static final String GUARD_METHOD_SIG_TEMPLATE = "<RETURN> <NAME>_<SIGNATURE>(<PARAMS>)";
|
||||
//
|
||||
// static final String GUARD_METHOD_TEMPLATE =
|
||||
// "@ForceInline\n" +
|
||||
// "@LambdaForm.Compiled\n" +
|
||||
// "final static <METHOD> throws Throwable {\n" +
|
||||
// " if (handle.isDirect() && handle.vform.methodType_table[ad.type] == ad.symbolicMethodType) {\n" +
|
||||
// " <RESULT_ERASED>MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);<RETURN_ERASED>\n" +
|
||||
// " }\n" +
|
||||
// " else {\n" +
|
||||
// " MethodHandle mh = handle.getMethodHandle(ad.mode);\n" +
|
||||
// " <RETURN>mh.asType(ad.symbolicMethodTypeInvoker).invokeBasic(<LINK_TO_INVOKER_ARGS>);\n" +
|
||||
// " }\n" +
|
||||
// "}";
|
||||
// """
|
||||
// @ForceInline
|
||||
// @LambdaForm.Compiled
|
||||
// @Hidden
|
||||
// final static <METHOD> throws Throwable {
|
||||
// if (handle.hasInvokeExactBehavior() && handle.accessModeType(ad.type) != ad.symbolicMethodTypeExact) {
|
||||
// throw new WrongMethodTypeException("expected " + handle.accessModeType(ad.type) + " but found "
|
||||
// + ad.symbolicMethodTypeExact);
|
||||
// }
|
||||
// if (handle.isDirect() && handle.vform.methodType_table[ad.type] == ad.symbolicMethodTypeErased) {
|
||||
// <RESULT_ERASED>MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);<RETURN_ERASED>
|
||||
// } else {
|
||||
// MethodHandle mh = handle.getMethodHandle(ad.mode);
|
||||
// <RETURN>mh.asType(ad.symbolicMethodTypeInvoker).invokeBasic(<LINK_TO_INVOKER_ARGS>);
|
||||
// }
|
||||
// }""";
|
||||
//
|
||||
// static final String GUARD_METHOD_TEMPLATE_V =
|
||||
// "@ForceInline\n" +
|
||||
// "@LambdaForm.Compiled\n" +
|
||||
// "final static <METHOD> throws Throwable {\n" +
|
||||
// " if (handle.isDirect() && handle.vform.methodType_table[ad.type] == ad.symbolicMethodType) {\n" +
|
||||
// " MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);\n" +
|
||||
// " }\n" +
|
||||
// " else if (handle.isDirect() && handle.vform.getMethodType_V(ad.type) == ad.symbolicMethodType) {\n" +
|
||||
// " MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);\n" +
|
||||
// " }\n" +
|
||||
// " else {\n" +
|
||||
// " MethodHandle mh = handle.getMethodHandle(ad.mode);\n" +
|
||||
// " mh.asType(ad.symbolicMethodTypeInvoker).invokeBasic(<LINK_TO_INVOKER_ARGS>);\n" +
|
||||
// " }\n" +
|
||||
// "}";
|
||||
// """
|
||||
// @ForceInline
|
||||
// @LambdaForm.Compiled
|
||||
// @Hidden
|
||||
// final static <METHOD> throws Throwable {
|
||||
// if (handle.hasInvokeExactBehavior() && handle.accessModeType(ad.type) != ad.symbolicMethodTypeExact) {
|
||||
// throw new WrongMethodTypeException("expected " + handle.accessModeType(ad.type) + " but found "
|
||||
// + ad.symbolicMethodTypeExact);
|
||||
// }
|
||||
// if (handle.isDirect() && handle.vform.methodType_table[ad.type] == ad.symbolicMethodTypeErased) {
|
||||
// MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);
|
||||
// } else if (handle.isDirect() && handle.vform.getMethodType_V(ad.type) == ad.symbolicMethodTypeErased) {
|
||||
// MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);
|
||||
// } else {
|
||||
// MethodHandle mh = handle.getMethodHandle(ad.mode);
|
||||
// mh.asType(ad.symbolicMethodTypeInvoker).invokeBasic(<LINK_TO_INVOKER_ARGS>);
|
||||
// }
|
||||
// }""";
|
||||
//
|
||||
// // A template for deriving the operations
|
||||
// // could be supported by annotating VarHandle directly with the
|
||||
@ -733,6 +747,7 @@ final class VarHandles {
|
||||
// System.out.println("package java.lang.invoke;");
|
||||
// System.out.println();
|
||||
// System.out.println("import jdk.internal.vm.annotation.ForceInline;");
|
||||
// System.out.println("import jdk.internal.vm.annotation.Hidden;");
|
||||
// System.out.println();
|
||||
// System.out.println("// This class is auto-generated by " +
|
||||
// GuardMethodGenerator.class.getName() +
|
||||
@ -785,11 +800,8 @@ final class VarHandles {
|
||||
// hts.flatMap(ht -> Stream.of(VarHandleTemplate.class.getMethods()).
|
||||
// map(m -> generateMethodType(m, ht.receiver, ht.value, ht.intermediates))).
|
||||
// distinct().
|
||||
// map(mt -> generateMethod(mt)).
|
||||
// forEach(s -> {
|
||||
// System.out.println(s);
|
||||
// System.out.println();
|
||||
// });
|
||||
// map(GuardMethodGenerator::generateMethod).
|
||||
// forEach(System.out::println);
|
||||
//
|
||||
// System.out.println("}");
|
||||
// }
|
||||
@ -845,6 +857,7 @@ final class VarHandles {
|
||||
//
|
||||
// List<String> LINK_TO_INVOKER_ARGS = params.keySet().stream().
|
||||
// collect(toList());
|
||||
// LINK_TO_INVOKER_ARGS.set(0, LINK_TO_INVOKER_ARGS.get(0) + ".asDirect()");
|
||||
//
|
||||
// RETURN = returnType == void.class
|
||||
// ? ""
|
||||
@ -860,7 +873,7 @@ final class VarHandles {
|
||||
//
|
||||
// String RETURN_ERASED = returnType != Object.class
|
||||
// ? ""
|
||||
// : " return ad.returnType.cast(r);";
|
||||
// : "\n return ad.returnType.cast(r);";
|
||||
//
|
||||
// String template = returnType == void.class
|
||||
// ? GUARD_METHOD_TEMPLATE_V
|
||||
@ -877,7 +890,7 @@ final class VarHandles {
|
||||
// collect(joining(", "))).
|
||||
// replace("<LINK_TO_INVOKER_ARGS>", LINK_TO_INVOKER_ARGS.stream().
|
||||
// collect(joining(", ")))
|
||||
// ;
|
||||
// .indent(4);
|
||||
// }
|
||||
//
|
||||
// static String className(Class<?> c) {
|
||||
|
@ -45,12 +45,12 @@ final class VarHandle$Type$s {
|
||||
#end[Object]
|
||||
|
||||
FieldInstanceReadOnly(Class<?> receiverType, long fieldOffset{#if[Object]?, Class<?> fieldType}) {
|
||||
this(receiverType, fieldOffset{#if[Object]?, fieldType}, FieldInstanceReadOnly.FORM);
|
||||
this(receiverType, fieldOffset{#if[Object]?, fieldType}, FieldInstanceReadOnly.FORM, false);
|
||||
}
|
||||
|
||||
protected FieldInstanceReadOnly(Class<?> receiverType, long fieldOffset{#if[Object]?, Class<?> fieldType},
|
||||
VarForm form) {
|
||||
super(form);
|
||||
VarForm form, boolean exact) {
|
||||
super(form, exact);
|
||||
this.fieldOffset = fieldOffset;
|
||||
this.receiverType = receiverType;
|
||||
#if[Object]
|
||||
@ -59,8 +59,22 @@ final class VarHandle$Type$s {
|
||||
}
|
||||
|
||||
@Override
|
||||
final MethodType accessModeTypeUncached(AccessMode accessMode) {
|
||||
return accessMode.at.accessModeType(receiverType, {#if[Object]?fieldType:$type$.class});
|
||||
public FieldInstanceReadOnly withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior()
|
||||
? this
|
||||
: new FieldInstanceReadOnly(receiverType, fieldOffset{#if[Object]?, fieldType}, vform, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldInstanceReadOnly withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior()
|
||||
? this
|
||||
: new FieldInstanceReadOnly(receiverType, fieldOffset{#if[Object]?, fieldType}, vform, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
final MethodType accessModeTypeUncached(AccessType at) {
|
||||
return at.accessModeType(receiverType, {#if[Object]?fieldType:$type$.class});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -110,7 +124,26 @@ final class VarHandle$Type$s {
|
||||
static final class FieldInstanceReadWrite extends FieldInstanceReadOnly {
|
||||
|
||||
FieldInstanceReadWrite(Class<?> receiverType, long fieldOffset{#if[Object]?, Class<?> fieldType}) {
|
||||
super(receiverType, fieldOffset{#if[Object]?, fieldType}, FieldInstanceReadWrite.FORM);
|
||||
this(receiverType, fieldOffset{#if[Object]?, fieldType}, false);
|
||||
}
|
||||
|
||||
private FieldInstanceReadWrite(Class<?> receiverType, long fieldOffset{#if[Object]?, Class<?> fieldType},
|
||||
boolean exact) {
|
||||
super(receiverType, fieldOffset{#if[Object]?, fieldType}, FieldInstanceReadWrite.FORM, exact);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldInstanceReadWrite withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior()
|
||||
? this
|
||||
: new FieldInstanceReadWrite(receiverType, fieldOffset{#if[Object]?, fieldType}, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldInstanceReadWrite withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior()
|
||||
? this
|
||||
: new FieldInstanceReadWrite(receiverType, fieldOffset{#if[Object]?, fieldType}, false);
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
@ -356,12 +389,12 @@ final class VarHandle$Type$s {
|
||||
#end[Object]
|
||||
|
||||
FieldStaticReadOnly(Object base, long fieldOffset{#if[Object]?, Class<?> fieldType}) {
|
||||
this(base, fieldOffset{#if[Object]?, fieldType}, FieldStaticReadOnly.FORM);
|
||||
this(base, fieldOffset{#if[Object]?, fieldType}, FieldStaticReadOnly.FORM, false);
|
||||
}
|
||||
|
||||
protected FieldStaticReadOnly(Object base, long fieldOffset{#if[Object]?, Class<?> fieldType},
|
||||
VarForm form) {
|
||||
super(form);
|
||||
VarForm form, boolean exact) {
|
||||
super(form, exact);
|
||||
this.base = base;
|
||||
this.fieldOffset = fieldOffset;
|
||||
#if[Object]
|
||||
@ -369,6 +402,20 @@ final class VarHandle$Type$s {
|
||||
#end[Object]
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldStaticReadOnly withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior()
|
||||
? this
|
||||
: new FieldStaticReadOnly(base, fieldOffset{#if[Object]?, fieldType}, vform, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldStaticReadOnly withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior()
|
||||
? this
|
||||
: new FieldStaticReadOnly(base, fieldOffset{#if[Object]?, fieldType}, vform, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<VarHandleDesc> describeConstable() {
|
||||
var fieldTypeRef = {#if[Object]?fieldType:$type$.class}.describeConstable();
|
||||
@ -385,8 +432,8 @@ final class VarHandle$Type$s {
|
||||
}
|
||||
|
||||
@Override
|
||||
final MethodType accessModeTypeUncached(AccessMode accessMode) {
|
||||
return accessMode.at.accessModeType(null, {#if[Object]?fieldType:$type$.class});
|
||||
final MethodType accessModeTypeUncached(AccessType at) {
|
||||
return at.accessModeType(null, {#if[Object]?fieldType:$type$.class});
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
@ -423,7 +470,26 @@ final class VarHandle$Type$s {
|
||||
static final class FieldStaticReadWrite extends FieldStaticReadOnly {
|
||||
|
||||
FieldStaticReadWrite(Object base, long fieldOffset{#if[Object]?, Class<?> fieldType}) {
|
||||
super(base, fieldOffset{#if[Object]?, fieldType}, FieldStaticReadWrite.FORM);
|
||||
this(base, fieldOffset{#if[Object]?, fieldType}, false);
|
||||
}
|
||||
|
||||
private FieldStaticReadWrite(Object base, long fieldOffset{#if[Object]?, Class<?> fieldType},
|
||||
boolean exact) {
|
||||
super(base, fieldOffset{#if[Object]?, fieldType}, FieldStaticReadWrite.FORM, exact);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldStaticReadWrite withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior()
|
||||
? this
|
||||
: new FieldStaticReadWrite(base, fieldOffset{#if[Object]?, fieldType}, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldStaticReadWrite withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior()
|
||||
? this
|
||||
: new FieldStaticReadWrite(base, fieldOffset{#if[Object]?, fieldType}, false);
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
@ -670,7 +736,11 @@ final class VarHandle$Type$s {
|
||||
#end[Object]
|
||||
|
||||
Array(int abase, int ashift{#if[Object]?, Class<?> arrayType}) {
|
||||
super(Array.FORM);
|
||||
this(abase, ashift{#if[Object]?, arrayType}, false);
|
||||
}
|
||||
|
||||
private Array(int abase, int ashift{#if[Object]?, Class<?> arrayType}, boolean exact) {
|
||||
super(Array.FORM, exact);
|
||||
this.abase = abase;
|
||||
this.ashift = ashift;
|
||||
#if[Object]
|
||||
@ -679,6 +749,20 @@ final class VarHandle$Type$s {
|
||||
#end[Object]
|
||||
}
|
||||
|
||||
@Override
|
||||
public Array withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior()
|
||||
? this
|
||||
: new Array(abase, ashift{#if[Object]?, arrayType}, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Array withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior()
|
||||
? this
|
||||
: new Array(abase, ashift{#if[Object]?, arrayType}, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<VarHandleDesc> describeConstable() {
|
||||
var arrayTypeRef = {#if[Object]?arrayType:$type$[].class}.describeConstable();
|
||||
@ -689,8 +773,8 @@ final class VarHandle$Type$s {
|
||||
}
|
||||
|
||||
@Override
|
||||
final MethodType accessModeTypeUncached(AccessMode accessMode) {
|
||||
return accessMode.at.accessModeType({#if[Object]?arrayType:$type$[].class}, {#if[Object]?arrayType.getComponentType():$type$.class}, int.class);
|
||||
final MethodType accessModeTypeUncached(AccessType at) {
|
||||
return at.accessModeType({#if[Object]?arrayType:$type$[].class}, {#if[Object]?arrayType.getComponentType():$type$.class}, int.class);
|
||||
}
|
||||
|
||||
#if[Object]
|
||||
|
@ -68,8 +68,8 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
|
||||
private static abstract class ByteArrayViewVarHandle extends VarHandle {
|
||||
final boolean be;
|
||||
|
||||
ByteArrayViewVarHandle(VarForm form, boolean be) {
|
||||
super(form);
|
||||
ByteArrayViewVarHandle(VarForm form, boolean be, boolean exact) {
|
||||
super(form, exact);
|
||||
this.be = be;
|
||||
}
|
||||
}
|
||||
@ -77,12 +77,30 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
|
||||
static final class ArrayHandle extends ByteArrayViewVarHandle {
|
||||
|
||||
ArrayHandle(boolean be) {
|
||||
super(ArrayHandle.FORM, be);
|
||||
this(be, false);
|
||||
}
|
||||
|
||||
private ArrayHandle(boolean be, boolean exact) {
|
||||
super(ArrayHandle.FORM, be, exact);
|
||||
}
|
||||
|
||||
@Override
|
||||
final MethodType accessModeTypeUncached(AccessMode accessMode) {
|
||||
return accessMode.at.accessModeType(byte[].class, $type$.class, int.class);
|
||||
public ArrayHandle withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior()
|
||||
? this
|
||||
: new ArrayHandle(be, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayHandle withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior()
|
||||
? this
|
||||
: new ArrayHandle(be, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
final MethodType accessModeTypeUncached(AccessType at) {
|
||||
return at.accessModeType(byte[].class, $type$.class, int.class);
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
@ -555,12 +573,30 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
|
||||
static final class ByteBufferHandle extends ByteArrayViewVarHandle {
|
||||
|
||||
ByteBufferHandle(boolean be) {
|
||||
super(ByteBufferHandle.FORM, be);
|
||||
this(be, false);
|
||||
}
|
||||
|
||||
private ByteBufferHandle(boolean be, boolean exact) {
|
||||
super(ByteBufferHandle.FORM, be, exact);
|
||||
}
|
||||
|
||||
@Override
|
||||
final MethodType accessModeTypeUncached(AccessMode accessMode) {
|
||||
return accessMode.at.accessModeType(ByteBuffer.class, $type$.class, int.class);
|
||||
public ByteBufferHandle withInvokeExactBehavior() {
|
||||
return hasInvokeExactBehavior()
|
||||
? this
|
||||
: new ByteBufferHandle(be, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBufferHandle withInvokeBehavior() {
|
||||
return !hasInvokeExactBehavior()
|
||||
? this
|
||||
: new ByteBufferHandle(be, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
final MethodType accessModeTypeUncached(AccessType at) {
|
||||
return at.accessModeType(ByteBuffer.class, $type$.class, int.class);
|
||||
}
|
||||
|
||||
@ForceInline
|
||||
|
430
test/jdk/java/lang/invoke/VarHandles/VarHandleTestExact.java
Normal file
430
test/jdk/java/lang/invoke/VarHandles/VarHandleTestExact.java
Normal file
@ -0,0 +1,430 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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
|
||||
* @modules jdk.incubator.foreign
|
||||
*
|
||||
* @run testng/othervm -Xverify:all VarHandleTestExact
|
||||
* @run testng/othervm -Xverify:all -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true VarHandleTestExact
|
||||
* @run testng/othervm -Xverify:all -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false VarHandleTestExact
|
||||
* @run testng/othervm -Xverify:all -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true VarHandleTestExact
|
||||
*/
|
||||
|
||||
import jdk.incubator.foreign.MemoryAddress;
|
||||
import jdk.incubator.foreign.MemoryHandles;
|
||||
import jdk.incubator.foreign.MemorySegment;
|
||||
import org.testng.SkipException;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.lang.invoke.WrongMethodTypeException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class VarHandleTestExact {
|
||||
|
||||
private static class Widget {
|
||||
static Object objectField_SRW;
|
||||
static long longField_SRW;
|
||||
static double doubleField_SRW;
|
||||
static Long aLongField_SRW;
|
||||
|
||||
Object objectField_RW;
|
||||
long longField_RW;
|
||||
double doubleField_RW;
|
||||
Long aLongField_RW;
|
||||
|
||||
final static Object objectField_SRO = new Object();
|
||||
final static long longField_SRO = 1234L;
|
||||
final static double doubleField_SRO = 1234D;
|
||||
final static Long aLongField_SRO = 1234L;
|
||||
|
||||
final Object objectField_RO = new Object();
|
||||
final long longField_RO = 1234L;
|
||||
final double doubleField_RO = 1234D;
|
||||
final Long aLongField_RO = 1234L;
|
||||
}
|
||||
|
||||
@Test(dataProvider = "dataObjectAccess")
|
||||
public void testExactSet(String fieldBaseName, Class<?> fieldType, boolean ro, Object testValue,
|
||||
SetX setter, GetX getter,
|
||||
SetStaticX staticSetter, GetStaticX staticGetter)
|
||||
throws NoSuchFieldException, IllegalAccessException {
|
||||
if (ro) throw new SkipException("Can not test setter with read only field");
|
||||
VarHandle vh = MethodHandles.lookup().findVarHandle(Widget.class, fieldBaseName + "_RW", fieldType);
|
||||
assertFalse(vh.hasInvokeExactBehavior());
|
||||
Widget w = new Widget();
|
||||
|
||||
try {
|
||||
vh.set(w, testValue);
|
||||
vh.withInvokeBehavior().set(w, testValue);
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
fail("Unexpected exception", wmte);
|
||||
}
|
||||
|
||||
vh = vh.withInvokeExactBehavior();
|
||||
assertTrue(vh.hasInvokeExactBehavior());
|
||||
try {
|
||||
setter.set(vh, w, testValue); // should throw
|
||||
fail("Exception expected");
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
assertMatches(wmte.getMessage(),".*\\Qexpected (Widget," + fieldType.getSimpleName() + ")void \\E.*");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dataProvider = "dataObjectAccess")
|
||||
public void testExactGet(String fieldBaseName, Class<?> fieldType, boolean ro, Object testValue,
|
||||
SetX setter, GetX getter,
|
||||
SetStaticX staticSetter, GetStaticX staticGetter)
|
||||
throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle vh = MethodHandles.lookup().findVarHandle(Widget.class, fieldBaseName + (ro ? "_RO" : "_RW"), fieldType);
|
||||
assertFalse(vh.hasInvokeExactBehavior());
|
||||
Widget w = new Widget();
|
||||
|
||||
try {
|
||||
Object o = vh.get(w);
|
||||
Object o2 = vh.withInvokeBehavior().get(w);
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
fail("Unexpected exception", wmte);
|
||||
}
|
||||
|
||||
vh = vh.withInvokeExactBehavior();
|
||||
assertTrue(vh.hasInvokeExactBehavior());
|
||||
try {
|
||||
getter.get(vh, w); // should throw
|
||||
fail("Exception expected");
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
assertMatches(wmte.getMessage(),".*\\Qexpected (Widget)" + fieldType.getSimpleName() + " \\E.*");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dataProvider = "dataObjectAccess")
|
||||
public void testExactSetStatic(String fieldBaseName, Class<?> fieldType, boolean ro, Object testValue,
|
||||
SetX setter, GetX getter,
|
||||
SetStaticX staticSetter, GetStaticX staticGetter)
|
||||
throws NoSuchFieldException, IllegalAccessException {
|
||||
if (ro) throw new SkipException("Can not test setter with read only field");
|
||||
VarHandle vh = MethodHandles.lookup().findStaticVarHandle(Widget.class, fieldBaseName + "_SRW", fieldType);
|
||||
assertFalse(vh.hasInvokeExactBehavior());
|
||||
|
||||
try {
|
||||
vh.set(testValue);
|
||||
vh.withInvokeBehavior().set(testValue);
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
fail("Unexpected exception", wmte);
|
||||
}
|
||||
|
||||
vh = vh.withInvokeExactBehavior();
|
||||
assertTrue(vh.hasInvokeExactBehavior());
|
||||
try {
|
||||
staticSetter.set(vh, testValue); // should throw
|
||||
fail("Exception expected");
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
assertMatches(wmte.getMessage(),".*\\Qexpected (" + fieldType.getSimpleName() + ")void \\E.*");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dataProvider = "dataObjectAccess")
|
||||
public void testExactGetStatic(String fieldBaseName, Class<?> fieldType, boolean ro, Object testValue,
|
||||
SetX setter, GetX getter,
|
||||
SetStaticX staticSetter, GetStaticX staticGetter)
|
||||
throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle vh = MethodHandles.lookup().findStaticVarHandle(Widget.class, fieldBaseName + (ro ? "_SRO" : "_SRW"), fieldType);
|
||||
assertFalse(vh.hasInvokeExactBehavior());
|
||||
|
||||
try {
|
||||
Object o = vh.get();
|
||||
Object o2 = vh.withInvokeBehavior().get();
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
fail("Unexpected exception", wmte);
|
||||
}
|
||||
|
||||
vh = vh.withInvokeExactBehavior();
|
||||
assertTrue(vh.hasInvokeExactBehavior());
|
||||
try {
|
||||
staticGetter.get(vh); // should throw
|
||||
fail("Exception expected");
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
assertMatches(wmte.getMessage(),".*\\Qexpected ()" + fieldType.getSimpleName() + " \\E.*");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dataProvider = "dataSetArray")
|
||||
public void testExactArraySet(Class<?> arrayClass, Object testValue, SetArrayX setter) {
|
||||
VarHandle vh = MethodHandles.arrayElementVarHandle(arrayClass);
|
||||
Object arr = Array.newInstance(arrayClass.componentType(), 1);
|
||||
assertFalse(vh.hasInvokeExactBehavior());
|
||||
|
||||
try {
|
||||
vh.set(arr, 0, testValue);
|
||||
vh.withInvokeBehavior().set(arr, 0, testValue);
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
fail("Unexpected exception", wmte);
|
||||
}
|
||||
|
||||
vh = vh.withInvokeExactBehavior();
|
||||
assertTrue(vh.hasInvokeExactBehavior());
|
||||
try {
|
||||
setter.set(vh, arr, testValue); // should throw
|
||||
fail("Exception expected");
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
assertMatches(wmte.getMessage(),
|
||||
".*\\Qexpected (" + arrayClass.getSimpleName() + ",int," + arrayClass.componentType().getSimpleName() + ")void \\E.*");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dataProvider = "dataSetBuffer")
|
||||
public void testExactBufferSet(Class<?> arrayClass, Object testValue, SetBufferX setter) {
|
||||
VarHandle vh = MethodHandles.byteBufferViewVarHandle(arrayClass, ByteOrder.nativeOrder());
|
||||
assertFalse(vh.hasInvokeExactBehavior());
|
||||
ByteBuffer buff = ByteBuffer.allocateDirect(8);
|
||||
|
||||
try {
|
||||
vh.set(buff, 0, testValue);
|
||||
vh.withInvokeBehavior().set(buff, 0, testValue);
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
fail("Unexpected exception", wmte);
|
||||
}
|
||||
|
||||
vh = vh.withInvokeExactBehavior();
|
||||
assertTrue(vh.hasInvokeExactBehavior());
|
||||
try {
|
||||
setter.set(vh, buff, testValue); // should throw
|
||||
fail("Exception expected");
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
assertMatches(wmte.getMessage(),
|
||||
".*\\Qexpected (ByteBuffer,int," + arrayClass.componentType().getSimpleName() + ")void \\E.*");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dataProvider = "dataSetMemorySegment")
|
||||
public void testExactSegmentSet(Class<?> carrier, Object testValue, SetSegmentX setter) {
|
||||
VarHandle vh = MemoryHandles.varHandle(carrier, ByteOrder.nativeOrder());
|
||||
assertFalse(vh.hasInvokeExactBehavior());
|
||||
try (MemorySegment seg = MemorySegment.allocateNative(8)) {
|
||||
MemoryAddress base = seg.baseAddress();
|
||||
try {
|
||||
vh.set(base, testValue);
|
||||
vh.withInvokeBehavior().set(base, testValue);
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
fail("Unexpected exception", wmte);
|
||||
}
|
||||
|
||||
vh = vh.withInvokeExactBehavior();
|
||||
assertTrue(vh.hasInvokeExactBehavior());
|
||||
try {
|
||||
setter.set(vh, base, testValue); // should throw
|
||||
fail("Exception expected");
|
||||
} catch (WrongMethodTypeException wmte) {
|
||||
assertMatches(wmte.getMessage(),
|
||||
".*\\Qexpected (MemoryAddress," + carrier.getSimpleName() + ")void \\E.*");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertMatches(String str, String pattern) {
|
||||
if (!str.matches(pattern)) {
|
||||
throw new AssertionError("'" + str + "' did not match the pattern '" + pattern + "'.");
|
||||
}
|
||||
}
|
||||
|
||||
private interface SetX {
|
||||
void set(VarHandle vh, Widget w, Object testValue);
|
||||
}
|
||||
|
||||
private interface SetStaticX {
|
||||
void set(VarHandle vh, Object testValue);
|
||||
}
|
||||
|
||||
private interface GetX {
|
||||
void get(VarHandle vh, Widget w);
|
||||
}
|
||||
|
||||
private interface GetStaticX {
|
||||
void get(VarHandle vh);
|
||||
}
|
||||
|
||||
private interface SetArrayX {
|
||||
void set(VarHandle vh, Object array, Object testValue);
|
||||
}
|
||||
|
||||
private interface SetBufferX {
|
||||
void set(VarHandle vh, ByteBuffer buff, Object testValue);
|
||||
}
|
||||
|
||||
private interface SetSegmentX {
|
||||
void set(VarHandle vh, MemoryAddress addr, Object testValue);
|
||||
}
|
||||
|
||||
private static void consume(Object o) {}
|
||||
|
||||
private static void testCaseObjectAccess(List<Object[]> cases, String fieldBaseName, Class<?> fieldType, Object testValue,
|
||||
SetX setter, GetX getter,
|
||||
SetStaticX staticSetter, GetStaticX staticGetter) {
|
||||
cases.add(new Object[] { fieldBaseName, fieldType, false, testValue, setter, getter, staticSetter, staticGetter });
|
||||
cases.add(new Object[] { fieldBaseName, fieldType, true, testValue, setter, getter, staticSetter, staticGetter });
|
||||
}
|
||||
|
||||
private static void testCaseArraySet(List<Object[]> cases, Class<?> arrayType, Object testValue, SetArrayX setter) {
|
||||
cases.add(new Object[] { arrayType, testValue, setter });
|
||||
}
|
||||
|
||||
private static void testCaseBufferSet(List<Object[]> cases, Class<?> arrayType, Object testValue, SetBufferX setter) {
|
||||
cases.add(new Object[] { arrayType, testValue, setter });
|
||||
}
|
||||
|
||||
private static void testCaseSegmentSet(List<Object[]> cases, Class<?> carrier, Object testValue, SetSegmentX setter) {
|
||||
cases.add(new Object[] { carrier, testValue, setter });
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public static Object[][] dataObjectAccess() {
|
||||
List<Object[]> cases = new ArrayList<>();
|
||||
|
||||
// create a bunch of different sig-poly call sites
|
||||
testCaseObjectAccess(cases, "objectField", Object.class, "abcd",
|
||||
(vh, w, tv) -> vh.set(w, (String) tv),
|
||||
(vh, w) -> consume((String) vh.get(w)),
|
||||
(vh, tv) -> vh.set((String) tv),
|
||||
(vh) -> consume((String) vh.get()));
|
||||
testCaseObjectAccess(cases, "objectField", Object.class, Integer.valueOf(1234),
|
||||
(vh, w, tv) -> vh.set(w, (Integer) tv),
|
||||
(vh, w) -> consume((Integer) vh.get(w)),
|
||||
(vh, tv) -> vh.set((Integer) tv),
|
||||
(vh) -> consume((Integer) vh.get()));
|
||||
testCaseObjectAccess(cases, "longField", long.class, 1234,
|
||||
(vh, w, tv) -> vh.set(w, (int) tv),
|
||||
(vh, w) -> consume((int) vh.get(w)),
|
||||
(vh, tv) -> vh.set((int) tv),
|
||||
(vh) -> consume((int) vh.get()));
|
||||
testCaseObjectAccess(cases, "longField", long.class, (short) 1234,
|
||||
(vh, w, tv) -> vh.set(w, (short) tv),
|
||||
(vh, w) -> consume((short) vh.get(w)),
|
||||
(vh, tv) -> vh.set((short) tv),
|
||||
(vh) -> consume((short) vh.get()));
|
||||
testCaseObjectAccess(cases, "longField", long.class, (char) 1234,
|
||||
(vh, w, tv) -> vh.set(w, (char) tv),
|
||||
(vh, w) -> consume((char) vh.get(w)),
|
||||
(vh, tv) -> vh.set((char) tv),
|
||||
(vh) -> consume((char) vh.get()));
|
||||
testCaseObjectAccess(cases, "longField", long.class, (byte) 1234,
|
||||
(vh, w, tv) -> vh.set(w, (byte) tv),
|
||||
(vh, w) -> consume((byte) vh.get(w)),
|
||||
(vh, tv) -> vh.set((byte) tv),
|
||||
(vh) -> consume((byte) vh.get()));
|
||||
testCaseObjectAccess(cases, "longField", long.class, Long.valueOf(1234L),
|
||||
(vh, w, tv) -> vh.set(w, (Long) tv),
|
||||
(vh, w) -> consume((Long) vh.get(w)),
|
||||
(vh, tv) -> vh.set((Long) tv),
|
||||
(vh) -> consume((Long) vh.get()));
|
||||
testCaseObjectAccess(cases, "doubleField", double.class, 1234F,
|
||||
(vh, w, tv) -> vh.set(w, (float) tv),
|
||||
(vh, w) -> consume((float) vh.get(w)),
|
||||
(vh, tv) -> vh.set((float) tv),
|
||||
(vh) -> consume((float) vh.get()));
|
||||
testCaseObjectAccess(cases, "doubleField", double.class, 1234,
|
||||
(vh, w, tv) -> vh.set(w, (int) tv),
|
||||
(vh, w) -> consume((int) vh.get(w)),
|
||||
(vh, tv) -> vh.set((int) tv),
|
||||
(vh) -> consume((int) vh.get()));
|
||||
testCaseObjectAccess(cases, "doubleField", double.class, 1234L,
|
||||
(vh, w, tv) -> vh.set(w, (long) tv),
|
||||
(vh, w) -> consume((long) vh.get(w)),
|
||||
(vh, tv) -> vh.set((long) tv),
|
||||
(vh) -> consume((long) vh.get()));
|
||||
testCaseObjectAccess(cases, "doubleField", double.class, Double.valueOf(1234D),
|
||||
(vh, w, tv) -> vh.set(w, (Double) tv),
|
||||
(vh, w) -> consume((Double) vh.get(w)),
|
||||
(vh, tv) -> vh.set((Double) tv),
|
||||
(vh) -> consume((Double) vh.get()));
|
||||
testCaseObjectAccess(cases, "aLongField", Long.class, 1234L,
|
||||
(vh, w, tv) -> vh.set(w, (long) tv),
|
||||
(vh, w) -> consume((long) vh.get(w)),
|
||||
(vh, tv) -> vh.set((long) tv),
|
||||
(vh) -> consume((long) vh.get()));
|
||||
|
||||
return cases.toArray(Object[][]::new);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public static Object[][] dataSetArray() {
|
||||
List<Object[]> cases = new ArrayList<>();
|
||||
|
||||
// create a bunch of different sig-poly call sites
|
||||
testCaseArraySet(cases, Object[].class, "abcd", (vh, arr, tv) -> vh.set((Object[]) arr, 0, (String) tv));
|
||||
testCaseArraySet(cases, Object[].class, Integer.valueOf(1234), (vh, arr, tv) -> vh.set((Object[]) arr, (Integer) tv));
|
||||
testCaseArraySet(cases, long[].class, 1234, (vh, arr, tv) -> vh.set((long[]) arr, 0, (int) tv));
|
||||
testCaseArraySet(cases, long[].class, (short) 1234, (vh, arr, tv) -> vh.set((long[]) arr, 0, (short) tv));
|
||||
testCaseArraySet(cases, long[].class, (char) 1234, (vh, arr, tv) -> vh.set((long[]) arr, 0, (char) tv));
|
||||
testCaseArraySet(cases, long[].class, (byte) 1234, (vh, arr, tv) -> vh.set((long[]) arr, 0, (byte) tv));
|
||||
testCaseArraySet(cases, long[].class, Long.valueOf(1234L), (vh, arr, tv) -> vh.set((long[]) arr, 0, (Long) tv));
|
||||
testCaseArraySet(cases, double[].class, 1234F, (vh, arr, tv) -> vh.set((double[]) arr, 0, (float) tv));
|
||||
testCaseArraySet(cases, double[].class, 1234, (vh, arr, tv) -> vh.set((double[]) arr, 0, (int) tv));
|
||||
testCaseArraySet(cases, double[].class, 1234L, (vh, arr, tv) -> vh.set((double[]) arr, 0, (long) tv));
|
||||
testCaseArraySet(cases, double[].class, Double.valueOf(1234D), (vh, arr, tv) -> vh.set((double[]) arr, 0, (Double) tv));
|
||||
testCaseArraySet(cases, Long[].class, 1234L, (vh, arr, tv) -> vh.set((Long[]) arr, 0, (long) tv));
|
||||
|
||||
return cases.toArray(Object[][]::new);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public static Object[][] dataSetBuffer() {
|
||||
List<Object[]> cases = new ArrayList<>();
|
||||
|
||||
// create a bunch of different sig-poly call sites
|
||||
testCaseBufferSet(cases, long[].class, 1234, (vh, buff, tv) -> vh.set(buff, 0, (int) tv));
|
||||
testCaseBufferSet(cases, long[].class, (short) 1234, (vh, buff, tv) -> vh.set(buff, 0, (short) tv));
|
||||
testCaseBufferSet(cases, long[].class, (char) 1234, (vh, buff, tv) -> vh.set(buff, 0, (char) tv));
|
||||
testCaseBufferSet(cases, long[].class, (byte) 1234, (vh, buff, tv) -> vh.set(buff, 0, (byte) tv));
|
||||
testCaseBufferSet(cases, long[].class, Long.valueOf(1234L), (vh, buff, tv) -> vh.set(buff, 0, (Long) tv));
|
||||
testCaseBufferSet(cases, double[].class, 1234F, (vh, buff, tv) -> vh.set(buff, 0, (float) tv));
|
||||
testCaseBufferSet(cases, double[].class, 1234, (vh, buff, tv) -> vh.set(buff, 0, (int) tv));
|
||||
testCaseBufferSet(cases, double[].class, 1234L, (vh, buff, tv) -> vh.set(buff, 0, (long) tv));
|
||||
testCaseBufferSet(cases, double[].class, Double.valueOf(1234D), (vh, buff, tv) -> vh.set(buff, 0, (Double) tv));
|
||||
|
||||
return cases.toArray(Object[][]::new);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public static Object[][] dataSetMemorySegment() {
|
||||
List<Object[]> cases = new ArrayList<>();
|
||||
|
||||
// create a bunch of different sig-poly call sites
|
||||
testCaseSegmentSet(cases, long.class, 1234, (vh, addr, tv) -> vh.set(addr, (int) tv));
|
||||
testCaseSegmentSet(cases, long.class, (char) 1234, (vh, addr, tv) -> vh.set(addr, (char) tv));
|
||||
testCaseSegmentSet(cases, long.class, (short) 1234, (vh, addr, tv) -> vh.set(addr, (short) tv));
|
||||
testCaseSegmentSet(cases, long.class, (byte) 1234, (vh, addr, tv) -> vh.set(addr, (byte) tv));
|
||||
testCaseSegmentSet(cases, double.class, 1234F, (vh, addr, tv) -> vh.set(addr, (float) tv));
|
||||
|
||||
return cases.toArray(Object[][]::new);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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.
|
||||
*/
|
||||
package org.openjdk.bench.java.lang.invoke;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
|
||||
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
|
||||
@State(org.openjdk.jmh.annotations.Scope.Thread)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Fork(3)
|
||||
public class VarHandleExact {
|
||||
|
||||
static final VarHandle exact;
|
||||
static final VarHandle generic;
|
||||
|
||||
static {
|
||||
try {
|
||||
generic = MethodHandles.lookup().findVarHandle(Data.class, "longField", long.class);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
exact = generic.withInvokeExactBehavior();
|
||||
}
|
||||
|
||||
Data data;
|
||||
|
||||
static class Data {
|
||||
long longField;
|
||||
}
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
data = new Data();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void exact_exactInvocation() {
|
||||
exact.set(data, (long) 42);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void generic_genericInvocation() {
|
||||
generic.set(data, 42);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void generic_exactInvocation() {
|
||||
generic.set(data, (long) 42);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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.
|
||||
*/
|
||||
package org.openjdk.bench.jdk.incubator.foreign;
|
||||
|
||||
import jdk.incubator.foreign.MemoryHandles;
|
||||
import jdk.incubator.foreign.MemorySegment;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.TearDown;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
|
||||
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
|
||||
@State(org.openjdk.jmh.annotations.Scope.Thread)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Fork(value = 3, jvmArgsAppend = { "--add-modules", "jdk.incubator.foreign" })
|
||||
public class VarHandleExact {
|
||||
|
||||
static final VarHandle exact;
|
||||
static final VarHandle generic;
|
||||
|
||||
static {
|
||||
generic = MemoryHandles.withStride(MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder()), 4);
|
||||
exact = generic.withInvokeExactBehavior();
|
||||
}
|
||||
|
||||
MemorySegment data;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
data = MemorySegment.allocateNative(JAVA_INT);
|
||||
}
|
||||
|
||||
@TearDown
|
||||
public void tearDown() {
|
||||
data.close();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void exact_exactInvocation() {
|
||||
exact.set(data.baseAddress(), (long) 0, 42);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void generic_genericInvocation() {
|
||||
generic.set(data.baseAddress(), 0, 42);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void generic_exactInvocation() {
|
||||
generic.set(data.baseAddress(), (long) 0, 42);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user