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

Ensure ParameterConverter is loaded from the TCCL #39823

Merged
merged 1 commit into from
Apr 2, 2024
Merged
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 @@ -36,6 +36,7 @@
import org.jboss.resteasy.reactive.server.core.parameters.converters.DelegatingParameterConverterSupplier;
import org.jboss.resteasy.reactive.server.core.parameters.converters.ParameterConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.ParameterConverterSupplier;
import org.jboss.resteasy.reactive.server.core.parameters.converters.ParameterConverterSupport;
import org.jboss.resteasy.reactive.server.core.parameters.converters.RuntimeResolvedConverter;
import org.jboss.resteasy.reactive.server.injection.ResteasyReactiveInjectionContext;
import org.jboss.resteasy.reactive.server.injection.ResteasyReactiveInjectionTarget;
Expand Down Expand Up @@ -77,6 +78,9 @@ public class ClassInjectorTransformer implements BiFunction<String, ClassVisitor
private static final String INIT_CONVERTER_FIELD_NAME = "__quarkus_converter__";
private static final String INIT_CONVERTER_METHOD_DESCRIPTOR = "(" + QUARKUS_REST_DEPLOYMENT_DESCRIPTOR + ")V";

private static final String PARAMETER_CONVERTER_SUPPORT_BINARY_NAME = ParameterConverterSupport.class.getName().replace('.',
'/');

private static final String MULTIPART_SUPPORT_BINARY_NAME = MultipartSupport.class.getName().replace('.', '/');

private static final String OBJECT_BINARY_NAME = Object.class.getName().replace('.', '/');
Expand Down Expand Up @@ -457,18 +461,14 @@ private void generateConverterInitMethod(FieldInfo fieldInfo, ParameterConverter
initConverterMethod.visitJumpInsn(Opcodes.IFNONNULL, notNull);
// stack: [converter]
initConverterMethod.visitInsn(Opcodes.POP);
// stack: []
// let's instantiate our delegate
initConverterMethod.visitTypeInsn(Opcodes.NEW, delegateBinaryName);
// stack: [converter]
initConverterMethod.visitInsn(Opcodes.DUP);
// stack: [converter, converter]
initConverterMethod.visitMethodInsn(Opcodes.INVOKESPECIAL, delegateBinaryName, "<init>",
"()V", false);
// stack: [converter]
// If we don't cast this to ParameterConverter, ASM in computeFrames will call getCommonSuperType
// and try to load our generated class before we can load it, so we insert this cast to avoid that
initConverterMethod.visitTypeInsn(Opcodes.CHECKCAST, PARAMETER_CONVERTER_BINARY_NAME);

// className param
initConverterMethod.visitLdcInsn(delegateBinaryName.replace('/', '.'));
initConverterMethod.visitMethodInsn(Opcodes.INVOKESTATIC, PARAMETER_CONVERTER_SUPPORT_BINARY_NAME,
"create",
"(" + STRING_DESCRIPTOR + ")" + PARAMETER_CONVERTER_DESCRIPTOR,
false);

// end default delegate
initConverterMethod.visitLabel(notNull);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jboss.resteasy.reactive.server.core.parameters.converters;

/**
* This class isn't used directly, it is however used by generated code meant to deal with {@link ParameterConverter}.
*/
@SuppressWarnings("unused")
public final class ParameterConverterSupport {

private ParameterConverterSupport() {
}

/**
* Normally the reflective instantiation would not be needed, and we could just instantiate normally,
* however that could break dev-mode when the converters are in a different module and non-standard Maven
* configuration is used (see <a href="https://github.com/quarkusio/quarkus/issues/39773#issuecomment-2030493539">this</a>)
*/
public static ParameterConverter create(String className) {
try {
Class<?> clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
return (ParameterConverter) clazz.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Unable to create instance of " + className, e);
}
}
}
Loading