Skip to content
Permalink
Browse files
8273360: [lworld] Invoking a reflection-generated constructor for pri…
…mitive class gives InstantiationError

Reviewed-by: mchung
  • Loading branch information
Jesper Steen Møller authored and Mandy Chung committed Sep 23, 2021
1 parent 2688bf6 commit 13035717c97321939a8e152db98ba15f4268a93c
Showing with 12 additions and 3 deletions.
  1. +10 −3 src/java.base/share/classes/jdk/internal/reflect/MethodAccessorGenerator.java
  2. +2 −0 test/jdk/valhalla/valuetypes/StaticFactoryTest.java
@@ -52,6 +52,7 @@ class MethodAccessorGenerator extends AccessorGenerator {
private Class<?>[] parameterTypes;
private Class<?> returnType;
private boolean isConstructor;
private boolean isStaticFactory;
private boolean forSerialization;

private short targetMethodRef;
@@ -80,6 +81,7 @@ public MethodAccessor generateMethod(Class<?> declaringClass,
modifiers,
false,
false,
false,
null);
}

@@ -89,13 +91,15 @@ public ConstructorAccessor generateConstructor(Class<?> declaringClass,
Class<?>[] checkedExceptions,
int modifiers)
{
boolean isStaticFactory = declaringClass.isPrimitiveClass();
return (ConstructorAccessor) generate(declaringClass,
"<init>",
parameterTypes,
Void.TYPE,
isStaticFactory ? declaringClass.asValueType() : Void.TYPE,
checkedExceptions,
modifiers,
true,
isStaticFactory,
false,
null);
}
@@ -116,6 +120,7 @@ public ConstructorAccessor generateConstructor(Class<?> declaringClass,
checkedExceptions,
modifiers,
true,
false,
true,
targetConstructorClass);
}
@@ -129,6 +134,7 @@ private MagicAccessorImpl generate(final Class<?> declaringClass,
Class<?>[] checkedExceptions,
int modifiers,
boolean isConstructor,
boolean isStaticFactory,
boolean forSerialization,
Class<?> serializationTargetClass)
{
@@ -139,6 +145,7 @@ private MagicAccessorImpl generate(final Class<?> declaringClass,
this.returnType = returnType;
this.modifiers = modifiers;
this.isConstructor = isConstructor;
this.isStaticFactory = isStaticFactory;
this.forSerialization = forSerialization;

asm.emitMagicAndVersion();
@@ -431,7 +438,7 @@ private void emitInvoke() {

short illegalArgStartPC = 0;

if (isConstructor) {
if (isConstructor && !isStaticFactory) {
// Instantiate target class before continuing
// new <target class type>
// dup
@@ -621,7 +628,7 @@ private void emitInvoke() {
short invokeStartPC = cb.getLength();

// OK, ready to perform the invocation.
if (isConstructor) {
if (isConstructor && !isStaticFactory) {
cb.opc_invokespecial(targetMethodRef, count, 0);
} else {
if (isStatic()) {
@@ -24,8 +24,10 @@

/*
* @test
* @bug 8273360
* @summary Test reflection of constructors for primitive classes
* @run testng/othervm StaticFactoryTest
* @run testng/othervm -Dsun.reflect.noInflation=true StaticFactoryTest
*/

import java.lang.reflect.Constructor;

0 comments on commit 1303571

Please sign in to comment.