[code-reflection] RFR: Clean up support for boxing

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Apr 30 11:16:35 UTC 2024


On Tue, 30 Apr 2024 11:11:42 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> This PR does a couple of things:
> * it sharpens the types of the various `JavaType` constants to the correct sealed sub-interface
> * it consolidates support for boxing/unboxing. More specifically there's now a pair of functions:
> 
> 
> ClassType::unbox() -> Optional<PrimitiveType>
> 
> 
> and
> 
> 
> PrimitiveType::box() -> Optional<ClassType>
> 
> 
> Note that both functions are partial: there are classes that are not wrappers (e.g. String) and there are primitive types that cannot be boxed (e.g. `void`).
> 
> I'm on the fence whether to use `Optional` for this, or just return null. Suggestions welcome.

src/java.base/share/classes/java/lang/reflect/code/op/CoreOps.java line 641:

> 639:         private static boolean isBoxOrUnboxInvocation(CoreOps.InvokeOp iop) {
> 640:             MethodRef mr = iop.invokeDescriptor();
> 641:             return mr.refType() instanceof ClassType ct && ct.unbox().isPresent() &&

In this case, using optional seems ok

src/java.base/share/classes/java/lang/reflect/code/op/ExtendedOps.java line 2860:

> 2858:             for (int i = 0; i < elements.size(); i++) {
> 2859:                 Value ele = elements.get(i);
> 2860:                 if (ele.type() instanceof PrimitiveType pt && pt.box().isPresent()) {

In this case, returning `null` and using another `instanceof` would have led to better results

src/java.base/share/classes/java/lang/reflect/code/type/ClassType.java line 92:

> 90:      */
> 91:     public Optional<PrimitiveType> unbox() {
> 92:         class LazyHolder {

We need a lazy holder here, to avoid initialization cycles (e.g. JavaType initializing a constant of type PrimitiveType which, through the map, points back at JavaType). Same for unbox.

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

PR Review Comment: https://git.openjdk.org/babylon/pull/67#discussion_r1584600093
PR Review Comment: https://git.openjdk.org/babylon/pull/67#discussion_r1584600549
PR Review Comment: https://git.openjdk.org/babylon/pull/67#discussion_r1584602073


More information about the babylon-dev mailing list