RFR: 8365620: Using enhanced switch in MethodHandleDesc
Shaojin Wen
swen at openjdk.org
Fri Aug 15 20:08:46 UTC 2025
On Fri, 15 Aug 2025 03:08:36 GMT, Chen Liang <liach at openjdk.org> wrote:
>> In MethodHandleDesc, the `ofField` method uses enhanced switch, while the `of` and `ofMethod` methods use traditional switch. The same class should have a unified style.
>
> 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.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/26769#discussion_r2278420230
More information about the core-libs-dev
mailing list