Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ClassLoader issue in test mode with Multipart in multi-module project #19068

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import io.quarkus.gizmo.Gizmo;

class MultipartTransformer implements BiFunction<String, ClassVisitor, ClassVisitor> {

private static final String THREAD_BINARY_NAME = "java/lang/Thread";
private static final String CLASS_BINARY_NAME = "java/lang/Class";
private static final String INJECTION_TARGET_BINARY_NAME = ResteasyReactiveInjectionTarget.class.getName()
.replace('.', '/');
private static final String INJECTION_CONTEXT_BINARY_NAME = ResteasyReactiveInjectionContext.class.getName()
Expand Down Expand Up @@ -79,11 +82,45 @@ public void visitEnd() {
// ctx param
injectMethod.visitIntInsn(Opcodes.ALOAD, 1);

// call the populator
injectMethod.visitMethodInsn(Opcodes.INVOKESTATIC, populatorName.replace('.', '/'),
DotNames.POPULATE_METHOD_NAME,
String.format("(%s%s)V", thisDescriptor, INJECTION_CONTEXT_DESCRIPTOR), false);

Comment on lines -82 to -86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I debated whether to keep this around for launch modes that are not test, but I think there isn't too much benefit in doing so...

// Call the populator using reflection. This is needed to ensure that the populator is loaded from the proper classloader
// See: https://github.com/quarkusio/quarkus/issues/19037
// All this ASM essentially generates:
//Thread.currentThread().getContextClassLoader().loadClass(populatorName).getMethod("populate", this.getClass(), ResteasyReactiveInjectionContext.class).invoke((Object)null, this, ctx);

injectMethod.visitMethodInsn(Opcodes.INVOKESTATIC, THREAD_BINARY_NAME, "currentThread", "()Ljava/lang/Thread;",
false);
injectMethod.visitMethodInsn(Opcodes.INVOKEVIRTUAL, THREAD_BINARY_NAME, "getContextClassLoader",
"()Ljava/lang/ClassLoader;", false);
injectMethod.visitLdcInsn(populatorName);
injectMethod.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ClassLoader", "loadClass",
"(Ljava/lang/String;)Ljava/lang/Class;", false);
injectMethod.visitLdcInsn("populate");
injectMethod.visitInsn(Opcodes.ICONST_2);
injectMethod.visitTypeInsn(Opcodes.ANEWARRAY, CLASS_BINARY_NAME);
injectMethod.visitInsn(Opcodes.DUP);
injectMethod.visitInsn(Opcodes.ICONST_0);
injectMethod.visitVarInsn(Opcodes.ALOAD, 0);
injectMethod.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
injectMethod.visitInsn(Opcodes.AASTORE);
injectMethod.visitInsn(Opcodes.DUP);
injectMethod.visitInsn(Opcodes.ICONST_1);
injectMethod.visitLdcInsn(Type.getType(INJECTION_CONTEXT_DESCRIPTOR));
injectMethod.visitInsn(Opcodes.AASTORE);
injectMethod.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CLASS_BINARY_NAME, "getMethod",
"(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
injectMethod.visitInsn(Opcodes.ACONST_NULL);
injectMethod.visitInsn(Opcodes.ICONST_2);
injectMethod.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
injectMethod.visitInsn(Opcodes.DUP);
injectMethod.visitInsn(Opcodes.ICONST_0);
injectMethod.visitVarInsn(Opcodes.ALOAD, 0);
injectMethod.visitInsn(Opcodes.AASTORE);
injectMethod.visitInsn(Opcodes.DUP);
injectMethod.visitInsn(Opcodes.ICONST_1);
injectMethod.visitVarInsn(Opcodes.ALOAD, 1);
injectMethod.visitInsn(Opcodes.AASTORE);
injectMethod.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke",
"(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
Comment on lines +90 to +123
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I obviously didn't come up with all this myself... I used the Bytecode Viewer plugin (on a sample class I wrote) in IntelliJ which has the capability to "ASM-ify" bytecode

injectMethod.visitInsn(Opcodes.RETURN);
injectMethod.visitEnd();
injectMethod.visitMaxs(0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ protected void handleMultipart(ClassInfo multipartClassInfo) {
String populatorClassName = MultipartPopulatorGenerator.generate(multipartClassInfo,
new GeneratedClassGizmoAdaptor(generatedClassBuildItemBuildProducer, true), index);
multipartGeneratedPopulators.put(className, populatorClassName);
reflectiveClassProducer.produce(new ReflectiveClassBuildItem(true, false, populatorClassName));

// transform the multipart pojo (and any super-classes) so we can access its fields no matter what
ClassInfo currentClassInHierarchy = multipartClassInfo;
Expand Down