Skip to content

Commit

Permalink
Fix Enterprise Beans deployment error
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Mar 26, 2023
1 parent 0e19532 commit 0b26475
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation.
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -21,6 +21,9 @@
import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;

import jakarta.inject.Inject;
import org.glassfish.deployment.common.DeploymentException;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
Expand Down Expand Up @@ -89,25 +92,23 @@ public Class generateSerializableSubclass() throws Exception {
// parameters injected by CDI.

Constructor<?>[] ctors = baseClass.getConstructors();
Constructor<?> ctorWithParams = null;
Constructor<?> parentCtor = null;
for(Constructor<?> ctor : ctors) {
if(ctor.getParameterTypes().length == 0) {
ctorWithParams = null; //exists the no-arg ctor, use it
parentCtor = ctor; //exists the no-arg ctor, use it
break;
} else if(ctorWithParams == null) {
ctorWithParams = ctor;
} else if(ctor.isAnnotationPresent(Inject.class)) {
parentCtor = ctor;
}
}

int numArgsToPass = 1; // default is 1 to just handle 'this'
String paramTypeString = "()V";

if (ctorWithParams != null) {
Class<?>[] paramTypes = ctorWithParams.getParameterTypes();
numArgsToPass = paramTypes.length + 1;
paramTypeString = Type.getConstructorDescriptor(ctorWithParams);
if (parentCtor == null) {
throw new DeploymentException("A class " + baseClass.getName() + " doesn't have any appropriate constructor");
}

String paramTypeString = Type.getConstructorDescriptor(parentCtor);
int numArgsToPass = parentCtor.getParameterCount() + 1; // default is 1 to just handle 'this'

MethodVisitor ctorv = tv.visitMethod(ACC_PUBLIC, "<init>", paramTypeString, null, null);

for (int i = 0; i < numArgsToPass; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -33,6 +33,8 @@
import java.util.Map;
import java.util.Set;

import jakarta.inject.Inject;
import org.glassfish.deployment.common.DeploymentException;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
Expand Down Expand Up @@ -195,33 +197,33 @@ public void generateSubclass(Class superClass, String subClassName, Class delega
{

Constructor[] ctors = superClass.getConstructors();
Constructor ctorWithParams = null;
Constructor parentCtor = null;
for(Constructor ctor : ctors) {
if(ctor.getParameterTypes().length == 0) {
ctorWithParams = null; //exists the no-arg ctor, use it
parentCtor = ctor; //exists the no-arg ctor, use it
break;
} else if(ctorWithParams == null) {
ctorWithParams = ctor;
} else if(ctor.isAnnotationPresent(Inject.class)) {
parentCtor = ctor;
}
}

if (parentCtor == null) {
throw new DeploymentException("A class " + superClass.getName() + " doesn't have any appropriate constructor");
}

MethodVisitor cv = tv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
cv.visitVarInsn(ALOAD, 0);
String paramTypeString = "()V";
// if void, only one param (implicit 'this' param)
int maxValue = 1;
if (ctorWithParams != null) {
Class[] paramTypes = ctorWithParams.getParameterTypes();
for (Class paramType : paramTypes) {
cv.visitInsn(ACONST_NULL);
}
paramTypeString = Type.getConstructorDescriptor(ctorWithParams);
// num params + one for 'this' pointer
maxValue = paramTypes.length + 1;

String paramTypeString = Type.getConstructorDescriptor(parentCtor);

int argCount = parentCtor.getParameterCount();
for (int i = 0; i < argCount; i++) {
cv.visitInsn(ACONST_NULL);
}

cv.visitMethodInsn(INVOKESPECIAL, Type.getType(superClass).getInternalName(), "<init>", paramTypeString, false);
cv.visitInsn(RETURN);
cv.visitMaxs(maxValue, 1);
cv.visitMaxs(argCount + 1, 1);
}

generateSetDelegateMethod(tv, delegateClass, subClassName);
Expand Down

0 comments on commit 0b26475

Please sign in to comment.