|
1 | 1 | package io.quarkus.deployment.recording; |
2 | 2 |
|
| 3 | +import static org.jboss.jandex.gizmo2.Jandex2Gizmo.classDescOf; |
3 | 4 | import static org.objectweb.asm.Opcodes.ACC_FINAL; |
4 | 5 | import static org.objectweb.asm.Opcodes.ACC_PRIVATE; |
5 | 6 | import static org.objectweb.asm.Opcodes.ACC_PUBLIC; |
6 | 7 |
|
7 | 8 | import java.io.InputStream; |
8 | 9 | import java.lang.annotation.Annotation; |
| 10 | +import java.lang.constant.ClassDesc; |
9 | 11 | import java.lang.reflect.InvocationHandler; |
10 | 12 | import java.lang.reflect.Method; |
11 | 13 | import java.lang.reflect.Proxy; |
| 14 | +import java.util.ArrayList; |
12 | 15 | import java.util.HashMap; |
13 | 16 | import java.util.List; |
14 | 17 | import java.util.ListIterator; |
|
32 | 35 | import io.quarkus.gizmo.FieldDescriptor; |
33 | 36 | import io.quarkus.gizmo.MethodCreator; |
34 | 37 | 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; |
35 | 44 |
|
36 | 45 | public class AnnotationProxyProvider { |
37 | 46 |
|
@@ -70,9 +79,9 @@ public <A extends Annotation> AnnotationProxyBuilder<A> builder(AnnotationInstan |
70 | 79 | } |
71 | 80 | return clazz; |
72 | 81 | }); |
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"); |
76 | 85 |
|
77 | 86 | return new AnnotationProxyBuilder<>(annotationInstance, annotationType, annotationLiteral, annotationClass); |
78 | 87 | } |
@@ -134,7 +143,6 @@ public AnnotationProxyBuilder<A> withDefaultValue(String name, Object value) { |
134 | 143 | return this; |
135 | 144 | } |
136 | 145 |
|
137 | | - @SuppressWarnings("unchecked") |
138 | 146 | public A build(ClassOutput classOutput) { |
139 | 147 |
|
140 | 148 | // Generate literal class if needed |
@@ -178,6 +186,11 @@ public A build(ClassOutput classOutput) { |
178 | 186 | return Boolean.TRUE; |
179 | 187 | }); |
180 | 188 |
|
| 189 | + return proxy(); |
| 190 | + } |
| 191 | + |
| 192 | + @SuppressWarnings("unchecked") |
| 193 | + private A proxy() { |
181 | 194 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
182 | 195 | if (classLoader == null) { |
183 | 196 | classLoader = AnnotationProxy.class.getClassLoader(); |
@@ -216,6 +229,58 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl |
216 | 229 | } |
217 | 230 | }); |
218 | 231 | } |
| 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 | + } |
219 | 284 | } |
220 | 285 |
|
221 | 286 | } |
0 commit comments