Skip to content

Commit 9760dba

Browse files
committed
8267321: Use switch expression for VarHandle$AccessMode lookup
Reviewed-by: jvernee
1 parent fdd0352 commit 9760dba

File tree

2 files changed

+34
-59
lines changed

2 files changed

+34
-59
lines changed

src/java.base/share/classes/java/lang/invoke/VarHandle.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,18 +1890,6 @@ public enum AccessMode {
18901890
GET_AND_BITWISE_XOR_ACQUIRE("getAndBitwiseXorAcquire", AccessType.GET_AND_UPDATE),
18911891
;
18921892

1893-
static final Map<String, AccessMode> methodNameToAccessMode;
1894-
static {
1895-
AccessMode[] values = AccessMode.values();
1896-
// Initial capacity of # values divided by the load factor is sufficient
1897-
// to avoid resizes for the smallest table size (64)
1898-
int initialCapacity = (int)(values.length / 0.75f) + 1;
1899-
methodNameToAccessMode = new HashMap<>(initialCapacity);
1900-
for (AccessMode am : values) {
1901-
methodNameToAccessMode.put(am.methodName, am);
1902-
}
1903-
}
1904-
19051893
final String methodName;
19061894
final AccessType at;
19071895

@@ -1934,9 +1922,40 @@ public String methodName() {
19341922
* @see #methodName()
19351923
*/
19361924
public static AccessMode valueFromMethodName(String methodName) {
1937-
AccessMode am = methodNameToAccessMode.get(methodName);
1938-
if (am != null) return am;
1939-
throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
1925+
return switch (methodName) {
1926+
case "get" -> GET;
1927+
case "set" -> SET;
1928+
case "getVolatile" -> GET_VOLATILE;
1929+
case "setVolatile" -> SET_VOLATILE;
1930+
case "getAcquire" -> GET_ACQUIRE;
1931+
case "setRelease" -> SET_RELEASE;
1932+
case "getOpaque" -> GET_OPAQUE;
1933+
case "setOpaque" -> SET_OPAQUE;
1934+
case "compareAndSet" -> COMPARE_AND_SET;
1935+
case "compareAndExchange" -> COMPARE_AND_EXCHANGE;
1936+
case "compareAndExchangeAcquire" -> COMPARE_AND_EXCHANGE_ACQUIRE;
1937+
case "compareAndExchangeRelease" -> COMPARE_AND_EXCHANGE_RELEASE;
1938+
case "weakCompareAndSet" -> WEAK_COMPARE_AND_SET;
1939+
case "weakCompareAndSetPlain" -> WEAK_COMPARE_AND_SET_PLAIN;
1940+
case "weakCompareAndSetAcquire" -> WEAK_COMPARE_AND_SET_ACQUIRE;
1941+
case "weakCompareAndSetRelease" -> WEAK_COMPARE_AND_SET_RELEASE;
1942+
case "getAndSet" -> GET_AND_SET;
1943+
case "getAndSetAcquire" -> GET_AND_SET_ACQUIRE;
1944+
case "getAndSetRelease" -> GET_AND_SET_RELEASE;
1945+
case "getAndAdd" -> GET_AND_ADD;
1946+
case "getAndAddAcquire" -> GET_AND_ADD_ACQUIRE;
1947+
case "getAndAddRelease" -> GET_AND_ADD_RELEASE;
1948+
case "getAndBitwiseOr" -> GET_AND_BITWISE_OR;
1949+
case "getAndBitwiseOrRelease" -> GET_AND_BITWISE_OR_RELEASE;
1950+
case "getAndBitwiseOrAcquire" -> GET_AND_BITWISE_OR_ACQUIRE;
1951+
case "getAndBitwiseAnd" -> GET_AND_BITWISE_AND;
1952+
case "getAndBitwiseAndRelease" -> GET_AND_BITWISE_AND_RELEASE;
1953+
case "getAndBitwiseAndAcquire" -> GET_AND_BITWISE_AND_ACQUIRE;
1954+
case "getAndBitwiseXor" -> GET_AND_BITWISE_XOR;
1955+
case "getAndBitwiseXorRelease" -> GET_AND_BITWISE_XOR_RELEASE;
1956+
case "getAndBitwiseXorAcquire" -> GET_AND_BITWISE_XOR_ACQUIRE;
1957+
default -> throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
1958+
};
19401959
}
19411960
}
19421961

test/jdk/java/lang/invoke/VarHandle/AccessMode/OptimalMapSize.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)