forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject "annotations" and "genericType" in ParamConverterProvider
Note that this PR will introduce a performance penalty at load time (first time, when the rest client instance is loaded AND if and only if the rest client methods use param annotations - which is most of the times tho). What I did was to always generate the `javaMethodX` static fields (before, it was also generated always, but it was done in the implementation MicroProfileRestClientEnricher). Plus, apart of the method information, we will also load the annotations and the generic types. This is done at load class time (static init). Moreover, as I had to move some code from MicroProfileRestClientEnricher to JaxrsClientReactiveProcessor, in order to not increase the length of this class JaxrsClientReactiveProcessor, I started moving some code to ClassRestClientContext (which is protected - not available for users) Fix quarkusio#22870 (cherry picked from commit 7f453ca)
- Loading branch information
Showing
12 changed files
with
516 additions
and
199 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
...ent/src/main/java/io/quarkus/jaxrs/client/reactive/deployment/ClassRestClientContext.java
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package io.quarkus.jaxrs.client.reactive.deployment; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.MethodInfo; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
import io.quarkus.deployment.GeneratedClassGizmoAdaptor; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.builditem.GeneratedClassBuildItem; | ||
import io.quarkus.gizmo.ClassCreator; | ||
import io.quarkus.gizmo.FieldDescriptor; | ||
import io.quarkus.gizmo.MethodCreator; | ||
import io.quarkus.gizmo.MethodDescriptor; | ||
import io.quarkus.gizmo.ResultHandle; | ||
|
||
class ClassRestClientContext implements AutoCloseable { | ||
|
||
public final ClassCreator classCreator; | ||
public final MethodCreator constructor; | ||
public final MethodCreator clinit; | ||
|
||
public final Map<Integer, FieldDescriptor> methodStaticFields = new HashMap<>(); | ||
public final Map<Integer, FieldDescriptor> methodParamAnnotationsStaticFields = new HashMap<>(); | ||
public final Map<Integer, FieldDescriptor> methodGenericParametersStaticFields = new HashMap<>(); | ||
|
||
public ClassRestClientContext(String name, BuildProducer<GeneratedClassBuildItem> generatedClasses, | ||
String... interfaces) { | ||
this(name, MethodDescriptor.ofConstructor(name), generatedClasses, Object.class, interfaces); | ||
} | ||
|
||
public ClassRestClientContext(String name, MethodDescriptor constructorDesc, | ||
BuildProducer<GeneratedClassBuildItem> generatedClasses, | ||
Class<?> superClass, String... interfaces) { | ||
|
||
this.classCreator = new ClassCreator(new GeneratedClassGizmoAdaptor(generatedClasses, true), | ||
name, null, superClass.getName(), interfaces); | ||
this.constructor = classCreator.getMethodCreator(constructorDesc); | ||
this.clinit = classCreator.getMethodCreator(MethodDescriptor.ofMethod(name, "<clinit>", void.class)); | ||
this.clinit.setModifiers(Opcodes.ACC_STATIC); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
classCreator.close(); | ||
} | ||
|
||
protected FieldDescriptor createJavaMethodField(ClassInfo interfaceClass, MethodInfo method, int methodIndex) { | ||
ResultHandle interfaceClassHandle = clinit.loadClassFromTCCL(interfaceClass.toString()); | ||
|
||
ResultHandle parameterArray = clinit.newArray(Class.class, method.parameters().size()); | ||
for (int i = 0; i < method.parameters().size(); i++) { | ||
String parameterClass = method.parameters().get(i).name().toString(); | ||
clinit.writeArrayValue(parameterArray, i, clinit.loadClassFromTCCL(parameterClass)); | ||
} | ||
|
||
ResultHandle javaMethodHandle = clinit.invokeVirtualMethod( | ||
MethodDescriptor.ofMethod(Class.class, "getMethod", Method.class, String.class, Class[].class), | ||
interfaceClassHandle, clinit.load(method.name()), parameterArray); | ||
FieldDescriptor javaMethodField = FieldDescriptor.of(classCreator.getClassName(), "javaMethod" + methodIndex, | ||
Method.class); | ||
classCreator.getFieldCreator(javaMethodField).setModifiers(Modifier.PRIVATE | Modifier.FINAL | Modifier.STATIC); | ||
clinit.writeStaticField(javaMethodField, javaMethodHandle); | ||
|
||
methodStaticFields.put(methodIndex, javaMethodField); | ||
|
||
return javaMethodField; | ||
} | ||
|
||
/** | ||
* Generates "method.getParameterAnnotations()" and it will only be created if and only if the supplier is used | ||
* in order to not have a penalty performance. | ||
*/ | ||
protected Supplier<FieldDescriptor> getLazyJavaMethodParamAnnotationsField(int methodIndex) { | ||
return () -> { | ||
FieldDescriptor methodParamAnnotationsField = methodParamAnnotationsStaticFields.get(methodIndex); | ||
if (methodParamAnnotationsField != null) { | ||
return methodParamAnnotationsField; | ||
} | ||
|
||
ResultHandle javaMethodParamAnnotationsHandle = clinit.invokeVirtualMethod( | ||
MethodDescriptor.ofMethod(Method.class, "getParameterAnnotations", Annotation[][].class), | ||
clinit.readStaticField(methodStaticFields.get(methodIndex))); | ||
FieldDescriptor javaMethodParamAnnotationsField = FieldDescriptor.of(classCreator.getClassName(), | ||
"javaMethodParameterAnnotations" + methodIndex, Annotation[][].class); | ||
classCreator.getFieldCreator(javaMethodParamAnnotationsField) | ||
.setModifiers(Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC); | ||
clinit.writeStaticField(javaMethodParamAnnotationsField, javaMethodParamAnnotationsHandle); | ||
|
||
methodParamAnnotationsStaticFields.put(methodIndex, javaMethodParamAnnotationsField); | ||
|
||
return javaMethodParamAnnotationsField; | ||
}; | ||
} | ||
|
||
/** | ||
* Generates "method.getGenericParameterTypes()" and it will only be created if and only if the supplier is used | ||
* in order to not have a penalty performance. | ||
*/ | ||
protected Supplier<FieldDescriptor> getLazyJavaMethodGenericParametersField(int methodIndex) { | ||
return () -> { | ||
FieldDescriptor methodGenericTypeField = methodGenericParametersStaticFields.get(methodIndex); | ||
if (methodGenericTypeField != null) { | ||
return methodGenericTypeField; | ||
} | ||
|
||
ResultHandle javaMethodGenericParametersHandle = clinit.invokeVirtualMethod( | ||
MethodDescriptor.ofMethod(Method.class, "getGenericParameterTypes", java.lang.reflect.Type[].class), | ||
clinit.readStaticField(methodStaticFields.get(methodIndex))); | ||
FieldDescriptor javaMethodGenericParametersField = FieldDescriptor.of(classCreator.getClassName(), | ||
"javaMethodGenericParameters" + methodIndex, java.lang.reflect.Type[].class); | ||
classCreator.getFieldCreator(javaMethodGenericParametersField) | ||
.setModifiers(Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC); | ||
clinit.writeStaticField(javaMethodGenericParametersField, javaMethodGenericParametersHandle); | ||
|
||
methodGenericParametersStaticFields.put(methodIndex, javaMethodGenericParametersField); | ||
|
||
return javaMethodGenericParametersField; | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.