Skip to content
Permalink
Browse files
8267321: Use switch expression for VarHandle$AccessMode lookup
Reviewed-by: jvernee
  • Loading branch information
cl4es committed May 19, 2021
1 parent fdd0352 commit 9760dba71c07cf7b0df16590b3e84e23ad587621
Showing with 34 additions and 59 deletions.
  1. +34 −15 src/java.base/share/classes/java/lang/invoke/VarHandle.java
  2. +0 −44 test/jdk/java/lang/invoke/VarHandle/AccessMode/OptimalMapSize.java
@@ -1890,18 +1890,6 @@ private static int fillParameters(Class<?>[] ps,
GET_AND_BITWISE_XOR_ACQUIRE("getAndBitwiseXorAcquire", AccessType.GET_AND_UPDATE),
;

static final Map<String, AccessMode> methodNameToAccessMode;
static {
AccessMode[] values = AccessMode.values();
// Initial capacity of # values divided by the load factor is sufficient
// to avoid resizes for the smallest table size (64)
int initialCapacity = (int)(values.length / 0.75f) + 1;
methodNameToAccessMode = new HashMap<>(initialCapacity);
for (AccessMode am : values) {
methodNameToAccessMode.put(am.methodName, am);
}
}

final String methodName;
final AccessType at;

@@ -1934,9 +1922,40 @@ public String methodName() {
* @see #methodName()
*/
public static AccessMode valueFromMethodName(String methodName) {
AccessMode am = methodNameToAccessMode.get(methodName);
if (am != null) return am;
throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
return switch (methodName) {
case "get" -> GET;
case "set" -> SET;
case "getVolatile" -> GET_VOLATILE;
case "setVolatile" -> SET_VOLATILE;
case "getAcquire" -> GET_ACQUIRE;
case "setRelease" -> SET_RELEASE;
case "getOpaque" -> GET_OPAQUE;
case "setOpaque" -> SET_OPAQUE;
case "compareAndSet" -> COMPARE_AND_SET;
case "compareAndExchange" -> COMPARE_AND_EXCHANGE;
case "compareAndExchangeAcquire" -> COMPARE_AND_EXCHANGE_ACQUIRE;
case "compareAndExchangeRelease" -> COMPARE_AND_EXCHANGE_RELEASE;
case "weakCompareAndSet" -> WEAK_COMPARE_AND_SET;
case "weakCompareAndSetPlain" -> WEAK_COMPARE_AND_SET_PLAIN;
case "weakCompareAndSetAcquire" -> WEAK_COMPARE_AND_SET_ACQUIRE;
case "weakCompareAndSetRelease" -> WEAK_COMPARE_AND_SET_RELEASE;
case "getAndSet" -> GET_AND_SET;
case "getAndSetAcquire" -> GET_AND_SET_ACQUIRE;
case "getAndSetRelease" -> GET_AND_SET_RELEASE;
case "getAndAdd" -> GET_AND_ADD;
case "getAndAddAcquire" -> GET_AND_ADD_ACQUIRE;
case "getAndAddRelease" -> GET_AND_ADD_RELEASE;
case "getAndBitwiseOr" -> GET_AND_BITWISE_OR;
case "getAndBitwiseOrRelease" -> GET_AND_BITWISE_OR_RELEASE;
case "getAndBitwiseOrAcquire" -> GET_AND_BITWISE_OR_ACQUIRE;
case "getAndBitwiseAnd" -> GET_AND_BITWISE_AND;
case "getAndBitwiseAndRelease" -> GET_AND_BITWISE_AND_RELEASE;
case "getAndBitwiseAndAcquire" -> GET_AND_BITWISE_AND_ACQUIRE;
case "getAndBitwiseXor" -> GET_AND_BITWISE_XOR;
case "getAndBitwiseXorRelease" -> GET_AND_BITWISE_XOR_RELEASE;
case "getAndBitwiseXorAcquire" -> GET_AND_BITWISE_XOR_ACQUIRE;
default -> throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
};
}
}

This file was deleted.

0 comments on commit 9760dba

Please sign in to comment.