Skip to content

Commit fca319d

Browse files
authored
[http-client] Copy Annotations to Generated Clients (#216)
* Copy Annotations * handle compound annotations * Update AnnotationUtil.java * Update AnnotationUtil.java * Delete .factorypath * Update AnnotationUtil.java * Update AnnotationUtil.java
1 parent d0c74c8 commit fca319d

File tree

5 files changed

+94
-16
lines changed

5 files changed

+94
-16
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.avaje.http.generator.client;
2+
3+
import java.util.List;
4+
5+
import javax.lang.model.element.AnnotationMirror;
6+
import javax.lang.model.element.AnnotationValue;
7+
import javax.lang.model.element.Element;
8+
import javax.lang.model.element.VariableElement;
9+
10+
import io.avaje.http.generator.core.Append;
11+
import io.avaje.http.generator.core.UType;
12+
13+
final class AnnotationUtil {
14+
private AnnotationUtil() {}
15+
16+
public static void writeAnnotations(Append writer, Element element) {
17+
for (final AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
18+
final var type = UType.parse(annotationMirror.getAnnotationType().asElement().asType());
19+
if (type.mainType().startsWith("io.avaje.http") || type.mainType().startsWith("io.swagger")) {
20+
continue;
21+
}
22+
final String annotationName = annotationMirror.getAnnotationType().toString();
23+
final StringBuilder sb = new StringBuilder("@").append(annotationName).append("(");
24+
boolean first = true;
25+
26+
for (final var entry : annotationMirror.getElementValues().entrySet()) {
27+
if (!first) {
28+
sb.append(", ");
29+
}
30+
sb.append(entry.getKey().getSimpleName()).append("=");
31+
writeVal(sb, entry.getValue());
32+
first = false;
33+
}
34+
35+
sb.append(")");
36+
final String annotationString = sb.toString();
37+
writer.append(annotationString).eol();
38+
}
39+
}
40+
41+
private static void writeVal(final StringBuilder sb, final AnnotationValue annotationValue) {
42+
final var value = annotationValue.getValue();
43+
// handle array values
44+
if (value instanceof List) {
45+
sb.append("{");
46+
boolean first = true;
47+
48+
for (final AnnotationValue listValue : (List<AnnotationValue>) value) {
49+
50+
if (!first) {
51+
sb.append(", ");
52+
}
53+
54+
writeVal(sb, listValue);
55+
first = false;
56+
}
57+
sb.append("}");
58+
// Handle enum values
59+
} else if (value instanceof VariableElement) {
60+
61+
final var element = (VariableElement) value;
62+
63+
final var type = UType.parse(element.asType());
64+
sb.append(type.full() + "." + element.toString());
65+
// handle annotation values
66+
} else if (value instanceof AnnotationMirror) {
67+
68+
final var mirror = (AnnotationMirror) value;
69+
70+
final String annotationName = mirror.getAnnotationType().toString();
71+
sb.append("@").append(annotationName).append("(");
72+
boolean first = true;
73+
74+
for (final var entry : mirror.getElementValues().entrySet()) {
75+
if (!first) {
76+
sb.append(", ");
77+
}
78+
sb.append(entry.getKey().getSimpleName()).append("=");
79+
writeVal(sb, entry.getValue());
80+
first = false;
81+
}
82+
83+
sb.append(")");
84+
} else {
85+
sb.append(annotationValue.toString());
86+
}
87+
}
88+
}

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private void methodStart(Append writer) {
4949
}
5050
writer.append(" // %s %s", webMethod, method.webMethodPath()).eol();
5151
writer.append(" @Override").eol();
52+
AnnotationUtil.writeAnnotations(writer, method.element());
5253
writer.append(" public %s%s %s(", methodGenericParams, returnType.shortType(), method.simpleName());
5354
int count = 0;
5455
for (MethodParam param : method.params()) {

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ private void writeMethods() {
7979

8080
private void writeClassStart() {
8181
writer.append(AT_GENERATED).eol();
82+
AnnotationUtil.writeAnnotations(writer, reader.beanType());
8283
writer.append("public class %s%s implements %s {", shortName, SUFFIX, shortName).eol().eol();
8384

8485
writer.append(" private final HttpClient client;").eol().eol();

http-generator-core/src/main/java/io/avaje/http/generator/core/MethodReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,8 @@ public void writeContext(Append writer, String reqName, String resName) {
423423
}
424424
writer.append("(new ServerContext(%s, %s), () -> ", reqName, resName);
425425
}
426+
427+
public ExecutableElement element() {
428+
return element;
429+
}
426430
}

tests/test-nima-jsonb/.factorypath

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)