|
| 1 | +package io.avaje.http.generator.core; |
| 2 | + |
| 3 | +import static java.util.stream.Collectors.toList; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map.Entry; |
| 7 | +import java.util.regex.Pattern; |
| 8 | + |
| 9 | +import javax.lang.model.element.AnnotationMirror; |
| 10 | +import javax.lang.model.element.AnnotationValue; |
| 11 | +import javax.lang.model.element.Element; |
| 12 | +import javax.lang.model.element.ExecutableElement; |
| 13 | +import javax.lang.model.element.VariableElement; |
| 14 | + |
| 15 | +final class AnnotationCopier { |
| 16 | + private AnnotationCopier() {} |
| 17 | + |
| 18 | + private static final Pattern ANNOTATION_TYPE_PATTERN = Pattern.compile("@([\\w.]+)\\."); |
| 19 | + |
| 20 | + static String trimAnnotationString(String input) { |
| 21 | + return ANNOTATION_TYPE_PATTERN.matcher(input).replaceAll("@"); |
| 22 | + } |
| 23 | + |
| 24 | + static void copyAnnotations(Append writer, Element element, boolean newLines) { |
| 25 | + copyAnnotations(writer, element, "", newLines); |
| 26 | + } |
| 27 | + |
| 28 | + static void copyAnnotations(Append writer, Element element, String indent, boolean newLines) { |
| 29 | + for (final AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { |
| 30 | + final var type = annotationMirror.getAnnotationType().asElement().asType().toString(); |
| 31 | + if (!type.contains("io.avaje.http.api.") |
| 32 | + || type.contains("Produces") |
| 33 | + || type.contains("Consumes")) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + String annotationString = toAnnotationString(indent, annotationMirror, false); |
| 38 | + |
| 39 | + annotationString = |
| 40 | + annotationString |
| 41 | + .replace("io.avaje.http.api.", "") |
| 42 | + .replace("value=", "") |
| 43 | + .replace("(\"\")", ""); |
| 44 | + |
| 45 | + writer.append(annotationString); |
| 46 | + |
| 47 | + if (newLines) { |
| 48 | + writer.eol(); |
| 49 | + } else { |
| 50 | + writer.append(" "); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + static String toSimpleAnnotationString(AnnotationMirror annotationMirror) { |
| 56 | + return trimAnnotationString(toAnnotationString("", annotationMirror, true)).substring(1); |
| 57 | + } |
| 58 | + |
| 59 | + static String toAnnotationString( |
| 60 | + String indent, AnnotationMirror annotationMirror, boolean simpleEnums) { |
| 61 | + final String annotationName = annotationMirror.getAnnotationType().toString(); |
| 62 | + |
| 63 | + final StringBuilder sb = |
| 64 | + new StringBuilder(indent).append("@").append(annotationName).append("("); |
| 65 | + boolean first = true; |
| 66 | + |
| 67 | + for (final var entry : sortedValues(annotationMirror)) { |
| 68 | + if (!first) { |
| 69 | + sb.append(", "); |
| 70 | + } |
| 71 | + sb.append(entry.getKey().getSimpleName()).append("="); |
| 72 | + writeVal(sb, entry.getValue(), simpleEnums); |
| 73 | + first = false; |
| 74 | + } |
| 75 | + |
| 76 | + return sb.append(")").toString().replace("()", ""); |
| 77 | + } |
| 78 | + |
| 79 | + private static List<Entry<? extends ExecutableElement, ? extends AnnotationValue>> sortedValues( |
| 80 | + AnnotationMirror annotationMirror) { |
| 81 | + return APContext.elements().getElementValuesWithDefaults(annotationMirror).entrySet().stream() |
| 82 | + .sorted(AnnotationCopier::compareBySimpleName) |
| 83 | + .collect(toList()); |
| 84 | + } |
| 85 | + |
| 86 | + private static int compareBySimpleName( |
| 87 | + Entry<? extends ExecutableElement, ? extends AnnotationValue> entry1, |
| 88 | + Entry<? extends ExecutableElement, ? extends AnnotationValue> entry2) { |
| 89 | + return entry1 |
| 90 | + .getKey() |
| 91 | + .getSimpleName() |
| 92 | + .toString() |
| 93 | + .compareTo(entry2.getKey().getSimpleName().toString()); |
| 94 | + } |
| 95 | + |
| 96 | + @SuppressWarnings("unchecked") |
| 97 | + private static void writeVal( |
| 98 | + final StringBuilder sb, final AnnotationValue annotationValue, boolean simpleEnums) { |
| 99 | + final var value = annotationValue.getValue(); |
| 100 | + if (value instanceof List) { |
| 101 | + // handle array values |
| 102 | + sb.append("{"); |
| 103 | + boolean first = true; |
| 104 | + for (final AnnotationValue listValue : (List<AnnotationValue>) value) { |
| 105 | + if (!first) { |
| 106 | + sb.append(", "); |
| 107 | + } |
| 108 | + writeVal(sb, listValue, simpleEnums); |
| 109 | + first = false; |
| 110 | + } |
| 111 | + sb.append("}"); |
| 112 | + |
| 113 | + } else if (value instanceof VariableElement) { |
| 114 | + // Handle enum values |
| 115 | + final var element = (VariableElement) value; |
| 116 | + final var type = element.asType(); |
| 117 | + final var str = simpleEnums ? element : type.toString() + "." + element; |
| 118 | + sb.append(str); |
| 119 | + |
| 120 | + } else if (value instanceof AnnotationMirror) { |
| 121 | + // handle annotation values |
| 122 | + final var mirror = (AnnotationMirror) value; |
| 123 | + final String annotationName = mirror.getAnnotationType().toString(); |
| 124 | + sb.append("@").append(annotationName).append("("); |
| 125 | + boolean first = true; |
| 126 | + |
| 127 | + for (final var entry : sortedValues(mirror)) { |
| 128 | + if (!first) { |
| 129 | + sb.append(", "); |
| 130 | + } |
| 131 | + sb.append(entry.getKey().getSimpleName()).append("="); |
| 132 | + writeVal(sb, entry.getValue(), simpleEnums); |
| 133 | + first = false; |
| 134 | + } |
| 135 | + sb.append(")"); |
| 136 | + |
| 137 | + } else { |
| 138 | + sb.append(annotationValue); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments