[code-reflection] RFR: Enable pattern matching for JavaType hierarchy
Maurizio Cimadamore
mcimadamore at openjdk.org
Fri Apr 26 18:13:06 UTC 2024
On Fri, 26 Apr 2024 17:50:43 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> The `JavaType` hierarchy has now several types under it:
>
> * `ClassType`
> * `ArrayType`
> * `PrimitiveType`
> * `TypeVarRef`
> * `WildcardType`
>
> All these types are `public`, except `PrimitiveType`. This limits applicability of pattern matching.
>
> In addition, `JavaType` defines a bunch of predicates:
>
> * `isClass()`
> * `isArray()`
> * `isPrimitive()`
>
> This set of predicates is now incomplete, as it doesn't support all the possible leaves in the type hierarchy.
>
> Overall, it would be much better to just lean into pattern matching more, and drop the predicates.
src/java.base/share/classes/java/lang/reflect/code/op/CoreOps.java line 1664:
> 1662:
> 1663: static TypeElement resultType(Value array, Value v) {
> 1664: if (!(array.type() instanceof ArrayType arrayType)) {
Note how the old code was buggy - as we did the test _after_ casting to `ArrayType`
src/java.base/share/classes/java/lang/reflect/code/type/JavaType.java line 212:
> 210:
> 211: static JavaType type(JavaType t, List<JavaType> typeArguments) {
> 212: return switch (t) {
Again, the old code was not complete - in the `else` branch we assumed the only possible option was a class type, which is no longer true
-------------
PR Review Comment: https://git.openjdk.org/babylon/pull/64#discussion_r1581363009
PR Review Comment: https://git.openjdk.org/babylon/pull/64#discussion_r1581363962
More information about the babylon-dev
mailing list