[lworld] RFR: 8273360: [lworld] Invoking a reflection-generated constructor for primitive class gives InstantiationError

Mandy Chung mchung at openjdk.java.net
Fri Sep 10 18:29:05 UTC 2021

On Sun, 5 Sep 2021 13:33:26 GMT, Jesper Steen Møller <jespersm at openjdk.org> wrote:

> Update constructor accessor code generation to invoke static factory method.

@jespersm Thanks for fixing this.  A couple comments.

src/java.base/share/classes/jdk/internal/reflect/ClassFileAssembler.java line 52:

> 50:         emitInt(0xCAFEBABE);
> 51:         emitShort((short) 0);
> 52:         emitShort((short) 62);

This old bytecode generator does not support stack map and so these reflection classes would need to stay in version 49.   JEP 416 changes the core reflection implementation to build on top of method handles and no longer depends on this old bytecode generator.

src/java.base/share/classes/jdk/internal/reflect/MethodAccessorGenerator.java line 141:

> 139:         if (this.isConstructingPrimitive ) {
> 140:             returnType = declaringClass.asValueType();
> 141:         }

Alternatively, this method can take an additional `isStaticFactory` parameter.  `generateConstructor` will determine if the declaring class is a primitive class and then call the static factory with the return type argument rather than `Void.TYPE`.

test/jdk/valhalla/valuetypes/StaticFactoryTest.java line 74:

> 72:             Object o = ctor.newInstance();
> 73:             assertTrue(o.getClass() == PRIMITIVE_TYPE.asPrimaryType());
> 74:         }

I suggest to replace this change with `@run testng/othervm -Dsun.reflect.noInflation=true StaticFactoryTest` so that other test cases will also verify when it's run with the generated bytecode.


PR: https://git.openjdk.java.net/valhalla/pull/546

More information about the valhalla-dev mailing list