Skip to content

Commit

Permalink
Inject "annotations" and "genericType" in ParamConverterProvider
Browse files Browse the repository at this point in the history
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
Sgitario authored and gsmet committed Feb 7, 2022
1 parent 2ca4148 commit deea31c
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 199 deletions.
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;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.gizmo.AssignableResultHandle;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.FieldDescriptor;
import io.quarkus.gizmo.MethodCreator;

/**
Expand Down Expand Up @@ -42,13 +43,15 @@ void forClass(MethodCreator ctor, AssignableResultHandle globalTarget,
* @param index jandex index
* @param generatedClasses build producer used to generate classes. Used e.g. to generate classes for header filling
* @param methodIndex 0-based index of the method in the interface. Used to assure there is no clash in generating classes
* @param javaMethodField method reference in a static class field
*/
void forMethod(ClassCreator classCreator, MethodCreator constructor,
MethodCreator clinit,
MethodCreator methodCreator,
ClassInfo interfaceClass,
MethodInfo method, AssignableResultHandle invocationBuilder,
IndexView index, BuildProducer<GeneratedClassBuildItem> generatedClasses, int methodIndex);
IndexView index, BuildProducer<GeneratedClassBuildItem> generatedClasses, int methodIndex,
FieldDescriptor javaMethodField);

/**
* Method-level alterations for methods of sub-resources
Expand All @@ -65,11 +68,12 @@ void forMethod(ClassCreator classCreator, MethodCreator constructor,
* @param generatedClasses build producer used to generate classes
* @param methodIndex 0-based index of method in the root interface
* @param subMethodIndex index of the method in the sub-resource interface
* @param javaMethodField method reference in a static class field
*/
void forSubResourceMethod(ClassCreator subClassCreator, MethodCreator subConstructor,
MethodCreator subClinit,
MethodCreator subMethodCreator, ClassInfo rootInterfaceClass, ClassInfo subInterfaceClass,
MethodInfo subMethod, MethodInfo rootMethod, AssignableResultHandle invocationBuilder, // sub-level
IndexView index, BuildProducer<GeneratedClassBuildItem> generatedClasses,
int methodIndex, int subMethodIndex);
int methodIndex, int subMethodIndex, FieldDescriptor javaMethodField);
}
Loading

0 comments on commit deea31c

Please sign in to comment.