[code-reflection] RFR: Update ReflectMethods.BodyScanner.coerce to handle null targetType

Mourad Abbay mabbay at openjdk.org
Mon Sep 1 15:17:57 UTC 2025


On Mon, 1 Sep 2025 10:34:08 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> `ReflectMethods.BodyScanner.coerce` wasn't handling `null` targetType. For example, a `null` targetType may propagate to reach the method `coerce` when visiting case body of switch statement.
>
> src/jdk.incubator.code/share/classes/jdk/incubator/code/internal/ReflectMethods.java line 739:
> 
>> 737: 
>> 738:         Value coerce(Value sourceValue, Type sourceType, Type targetType) {
>> 739:             if (targetType == null || targetType.hasTag(TypeTag.VOID)) {
> 
> Not sure about this -- as I'm not sure about `VOID`. E.g. this method should be called when we do have a target type. If there's no target type then there's nothing to coerce in the first place, and we shouldn't call this method. Can you describe more precisely why we end up with a `null` here?

For case body of a switch statement and body has a return. Because `bodyTarget` will be set to `yieldType` which is null, `bodyTarget` will then be used when we scan the return, this cause null to passed to `coerce`.

-------------

PR Review Comment: https://git.openjdk.org/babylon/pull/536#discussion_r2314209482


More information about the babylon-dev mailing list