Skip to content

[http-client] Copy Annotations to Generated Clients #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.avaje.http.generator.client;

import java.util.List;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.VariableElement;

import io.avaje.http.generator.core.Append;
import io.avaje.http.generator.core.UType;

final class AnnotationUtil {
private AnnotationUtil() {}

public static void writeAnnotations(Append writer, Element element) {
for (final AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
final var type = UType.parse(annotationMirror.getAnnotationType().asElement().asType());
if (type.mainType().startsWith("io.avaje.http") || type.mainType().startsWith("io.swagger")) {
continue;
}
final String annotationName = annotationMirror.getAnnotationType().toString();
final StringBuilder sb = new StringBuilder("@").append(annotationName).append("(");
boolean first = true;

for (final var entry : annotationMirror.getElementValues().entrySet()) {
if (!first) {
sb.append(", ");
}
sb.append(entry.getKey().getSimpleName()).append("=");
writeVal(sb, entry.getValue());
first = false;
}

sb.append(")");
final String annotationString = sb.toString();
writer.append(annotationString).eol();
}
}

private static void writeVal(final StringBuilder sb, final AnnotationValue annotationValue) {
final var value = annotationValue.getValue();
// handle array values
if (value instanceof List) {
sb.append("{");
boolean first = true;

for (final AnnotationValue listValue : (List<AnnotationValue>) value) {

if (!first) {
sb.append(", ");
}

writeVal(sb, listValue);
first = false;
}
sb.append("}");
// Handle enum values
} else if (value instanceof VariableElement) {

final var element = (VariableElement) value;

final var type = UType.parse(element.asType());
sb.append(type.full() + "." + element.toString());
// handle annotation values
} else if (value instanceof AnnotationMirror) {

final var mirror = (AnnotationMirror) value;

final String annotationName = mirror.getAnnotationType().toString();
sb.append("@").append(annotationName).append("(");
boolean first = true;

for (final var entry : mirror.getElementValues().entrySet()) {
if (!first) {
sb.append(", ");
}
sb.append(entry.getKey().getSimpleName()).append("=");
writeVal(sb, entry.getValue());
first = false;
}

sb.append(")");
} else {
sb.append(annotationValue.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private void methodStart(Append writer) {
}
writer.append(" // %s %s", webMethod, method.webMethodPath()).eol();
writer.append(" @Override").eol();
AnnotationUtil.writeAnnotations(writer, method.element());
writer.append(" public %s%s %s(", methodGenericParams, returnType.shortType(), method.simpleName());
int count = 0;
for (MethodParam param : method.params()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private void writeMethods() {

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

writer.append(" private final HttpClient client;").eol().eol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,8 @@ public void writeContext(Append writer, String reqName, String resName) {
}
writer.append("(new ServerContext(%s, %s), () -> ", reqName, resName);
}

public ExecutableElement element() {
return element;
}
}
16 changes: 0 additions & 16 deletions tests/test-nima-jsonb/.factorypath

This file was deleted.