RFR: 8365620: Using enhanced switch in MethodHandleDesc
ExE Boss
duke at openjdk.org
Fri Aug 15 20:08:46 UTC 2025
On Fri, 15 Aug 2025 06:57:14 GMT, Shaojin Wen <swen at openjdk.org> wrote:
>> src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java line 123:
>>
>>> 121: case VIRTUAL, SPECIAL, INTERFACE_VIRTUAL, INTERFACE_SPECIAL, INTERFACE_STATIC, STATIC, CONSTRUCTOR
>>> 122: -> new DirectMethodHandleDescImpl(kind, owner, name, lookupMethodType);
>>> 123: default -> throw new IllegalArgumentException(kind.toString());
>>
>> I recommend using `case GETTER, SETTER, STATIC_GETTER, STATIC_SETTER` explicitly in case we have more kinds in the future, this will become a compile error and remind us to update. And we can remove the default branch.
>
> If we remove the default case, unexpected behavior may occur when DirectMethodHandleDesc.Kind adds new enum values.
That won’t happen as this is an enhanced switch expression, which must be exhaustive at compile time (and gets an implicit `throw new MatchException(…)` default clause for exhaustiveness checking at runtime).
Suggestion:
case GETTER, SETTER, STATIC_GETTER, STATIC_SETTER
-> throw new IllegalArgumentException(kind.toString());
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/26769#discussion_r2278557045
More information about the core-libs-dev
mailing list