Skip to content

Commit

Permalink
8273360: [lworld] Invoking a reflection-generated constructor for pri…
Browse files Browse the repository at this point in the history
…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 1303571
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -80,6 +81,7 @@ public MethodAccessor generateMethod(Class<?> declaringClass,
modifiers,
false,
false,
false,
null);
}

Expand All @@ -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);
}
Expand All @@ -116,6 +120,7 @@ public ConstructorAccessor generateConstructor(Class<?> declaringClass,
checkedExceptions,
modifiers,
true,
false,
true,
targetConstructorClass);
}
Expand All @@ -129,6 +134,7 @@ private MagicAccessorImpl generate(final Class<?> declaringClass,
Class<?>[] checkedExceptions,
int modifiers,
boolean isConstructor,
boolean isStaticFactory,
boolean forSerialization,
Class<?> serializationTargetClass)
{
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions test/jdk/valhalla/valuetypes/StaticFactoryTest.java
Expand Up @@ -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;
Expand Down

0 comments on commit 1303571

Please sign in to comment.