Skip to content

Commit 49131ed

Browse files
committed
Core: add Gizmo 2 variant of AnnotationProxyProvider.build()
1 parent ecae8ca commit 49131ed

File tree

1 file changed

+69
-4
lines changed

1 file changed

+69
-4
lines changed

core/deployment/src/main/java/io/quarkus/deployment/recording/AnnotationProxyProvider.java

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package io.quarkus.deployment.recording;
22

3+
import static org.jboss.jandex.gizmo2.Jandex2Gizmo.classDescOf;
34
import static org.objectweb.asm.Opcodes.ACC_FINAL;
45
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
56
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
67

78
import java.io.InputStream;
89
import java.lang.annotation.Annotation;
10+
import java.lang.constant.ClassDesc;
911
import java.lang.reflect.InvocationHandler;
1012
import java.lang.reflect.Method;
1113
import java.lang.reflect.Proxy;
14+
import java.util.ArrayList;
1215
import java.util.HashMap;
1316
import java.util.List;
1417
import java.util.ListIterator;
@@ -32,6 +35,12 @@
3235
import io.quarkus.gizmo.FieldDescriptor;
3336
import io.quarkus.gizmo.MethodCreator;
3437
import io.quarkus.gizmo.MethodDescriptor;
38+
import io.quarkus.gizmo2.GenericType;
39+
import io.quarkus.gizmo2.Gizmo;
40+
import io.quarkus.gizmo2.ParamVar;
41+
import io.quarkus.gizmo2.TypeArgument;
42+
import io.quarkus.gizmo2.desc.ConstructorDesc;
43+
import io.quarkus.gizmo2.desc.FieldDesc;
3544

3645
public class AnnotationProxyProvider {
3746

@@ -70,9 +79,9 @@ public <A extends Annotation> AnnotationProxyBuilder<A> builder(AnnotationInstan
7079
}
7180
return clazz;
7281
});
73-
String annotationLiteral = annotationLiterals.computeIfAbsent(annotationInstance.name(), name ->
74-
// com.foo.MyAnnotation -> com.foo.MyAnnotation_Proxy_AnnotationLiteral
75-
name.toString().replace('.', '/') + "_Proxy_AnnotationLiteral");
82+
String annotationLiteral = annotationLiterals.computeIfAbsent(annotationInstance.name(),
83+
// com.foo.MyAnnotation -> com.foo.MyAnnotation_Proxy_AnnotationLiteral
84+
name -> name + "_Proxy_AnnotationLiteral");
7685

7786
return new AnnotationProxyBuilder<>(annotationInstance, annotationType, annotationLiteral, annotationClass);
7887
}
@@ -134,7 +143,6 @@ public AnnotationProxyBuilder<A> withDefaultValue(String name, Object value) {
134143
return this;
135144
}
136145

137-
@SuppressWarnings("unchecked")
138146
public A build(ClassOutput classOutput) {
139147

140148
// Generate literal class if needed
@@ -178,6 +186,11 @@ public A build(ClassOutput classOutput) {
178186
return Boolean.TRUE;
179187
});
180188

189+
return proxy();
190+
}
191+
192+
@SuppressWarnings("unchecked")
193+
private A proxy() {
181194
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
182195
if (classLoader == null) {
183196
classLoader = AnnotationProxy.class.getClassLoader();
@@ -216,6 +229,58 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
216229
}
217230
});
218231
}
232+
233+
public A build(io.quarkus.gizmo2.ClassOutput classOutput) {
234+
generatedLiterals.computeIfAbsent(annotationLiteral, generatedName -> {
235+
Gizmo.create(classOutput).class_(generatedName, cc -> {
236+
ClassDesc annotationClassDesc = classDescOf(annotationInstance.name());
237+
cc.extends_(GenericType.ofClass(AnnotationLiteral.class, TypeArgument.of(annotationClassDesc)));
238+
cc.implements_(annotationClassDesc);
239+
240+
List<MethodInfo> members = annotationClass.methods()
241+
.stream()
242+
.filter(m -> !m.isStaticInitializer() && !m.isConstructor())
243+
.toList();
244+
245+
List<FieldDesc> fields = new ArrayList<>(members.size());
246+
for (MethodInfo member : members) {
247+
fields.add(cc.field(member.name(), fc -> {
248+
fc.private_();
249+
fc.final_();
250+
fc.setType(classDescOf(member.returnType()));
251+
}));
252+
}
253+
254+
cc.constructor(mc -> {
255+
List<ParamVar> params = new ArrayList<>(members.size());
256+
for (MethodInfo member : members) {
257+
params.add(mc.parameter(member.name(), classDescOf(member.returnType())));
258+
}
259+
260+
mc.body(bc -> {
261+
bc.invokeSpecial(ConstructorDesc.of(AnnotationLiteral.class), cc.this_());
262+
for (int i = 0; i < members.size(); i++) {
263+
bc.set(cc.this_().field(fields.get(i)), params.get(i));
264+
}
265+
bc.return_();
266+
});
267+
});
268+
269+
for (int i = 0; i < members.size(); i++) {
270+
MethodInfo member = members.get(i);
271+
FieldDesc field = fields.get(i);
272+
cc.method(member.name(), mc -> {
273+
mc.returning(classDescOf(member.returnType()));
274+
mc.body(bc -> bc.return_(cc.this_().field(field)));
275+
});
276+
}
277+
});
278+
279+
return Boolean.TRUE;
280+
});
281+
282+
return proxy();
283+
}
219284
}
220285

221286
}

0 commit comments

Comments
 (0)