-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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); | ||
|
||
// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...