8199149: Improve the exception message thrown by VarHandle of unsupported operation

Reviewed-by: liach, jvernee
This commit is contained in:
Mandy Chung 2023-07-20 20:21:32 +00:00
parent 354c6605e3
commit d7b9416406
4 changed files with 12 additions and 5 deletions

@ -103,6 +103,6 @@ import java.util.function.BiFunction;
@Override @Override
MethodHandle getMethodHandleUncached(int mode) { MethodHandle getMethodHandleUncached(int mode) {
MethodHandle targetHandle = target.getMethodHandle(mode); // might throw UOE of access mode is not supported, which is ok MethodHandle targetHandle = target.getMethodHandle(mode); // might throw UOE of access mode is not supported, which is ok
return handleFactory.apply(AccessMode.values()[mode], targetHandle); return handleFactory.apply(AccessMode.valueFromOrdinal(mode), targetHandle);
} }
} }

@ -26,6 +26,7 @@ package java.lang.invoke;
import jdk.internal.vm.annotation.DontInline; import jdk.internal.vm.annotation.DontInline;
import jdk.internal.vm.annotation.ForceInline; import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Hidden;
import jdk.internal.vm.annotation.Stable; import jdk.internal.vm.annotation.Stable;
import java.lang.invoke.VarHandle.AccessMode; import java.lang.invoke.VarHandle.AccessMode;
@ -108,6 +109,7 @@ final class VarForm {
} }
@ForceInline @ForceInline
@Hidden
final MemberName getMemberName(int mode) { final MemberName getMemberName(int mode) {
// Can be simplified by calling getMemberNameOrNull, but written in this // Can be simplified by calling getMemberNameOrNull, but written in this
// form to improve interpreter/coldpath performance. // form to improve interpreter/coldpath performance.
@ -115,7 +117,7 @@ final class VarForm {
if (mn == null) { if (mn == null) {
mn = resolveMemberName(mode); mn = resolveMemberName(mode);
if (mn == null) { if (mn == null) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException(AccessMode.valueFromOrdinal(mode).methodName());
} }
} }
return mn; return mn;
@ -132,7 +134,7 @@ final class VarForm {
@DontInline @DontInline
MemberName resolveMemberName(int mode) { MemberName resolveMemberName(int mode) {
AccessMode value = AccessMode.values()[mode]; AccessMode value = AccessMode.valueFromOrdinal(mode);
String methodName = value.methodName(); String methodName = value.methodName();
MethodType type = methodType_table[value.at.ordinal()].insertParameterTypes(0, VarHandle.class); MethodType type = methodType_table[value.at.ordinal()].insertParameterTypes(0, VarHandle.class);
return memberName_table[mode] = MethodHandles.Lookup.IMPL_LOOKUP return memberName_table[mode] = MethodHandles.Lookup.IMPL_LOOKUP

@ -2003,6 +2003,11 @@ public abstract sealed class VarHandle implements Constable
default -> throw new IllegalArgumentException("No AccessMode value for method name " + methodName); default -> throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
}; };
} }
private static final @Stable AccessMode[] VALUES = values();
static AccessMode valueFromOrdinal(int mode) {
return VALUES[mode];
}
} }
static final class AccessDescriptor { static final class AccessDescriptor {
@ -2215,7 +2220,7 @@ public abstract sealed class VarHandle implements Constable
* @throws UnsupportedOperationException if the access mode is not supported * @throws UnsupportedOperationException if the access mode is not supported
*/ */
MethodHandle getMethodHandleUncached(int mode) { MethodHandle getMethodHandleUncached(int mode) {
MethodType mt = accessModeType(AccessMode.values()[mode]). MethodType mt = accessModeType(AccessMode.valueFromOrdinal(mode)).
insertParameterTypes(0, VarHandle.class); insertParameterTypes(0, VarHandle.class);
MemberName mn = vform.getMemberName(mode); MemberName mn = vform.getMemberName(mode);
DirectMethodHandle dmh = DirectMethodHandle.make(mn); DirectMethodHandle dmh = DirectMethodHandle.make(mn);

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it