|
| 1 | +package io.avaje.http.generator.core; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +import javax.annotation.processing.Generated; |
| 8 | +import javax.lang.model.element.AnnotationMirror; |
| 9 | +import javax.lang.model.element.AnnotationValue; |
| 10 | +import javax.lang.model.element.Element; |
| 11 | +import javax.lang.model.element.ExecutableElement; |
| 12 | +import javax.lang.model.element.TypeElement; |
| 13 | +import javax.lang.model.element.VariableElement; |
| 14 | +import javax.lang.model.util.ElementFilter; |
| 15 | + |
| 16 | +/** A Prism representing a {@link io.jstach.jstache.JStacheConfig @JStacheConfig} annotation. */ |
| 17 | +@Generated("avaje-prism-generator") |
| 18 | +final class JStacheConfigPrism { |
| 19 | + |
| 20 | + /** store prism value of type */ |
| 21 | + private final String _type; |
| 22 | + |
| 23 | + public static final String PRISM_TYPE = "io.jstach.jstache.JStacheConfig"; |
| 24 | + |
| 25 | + /** |
| 26 | + * An instance of the Values inner class whose methods return the AnnotationValues used to build |
| 27 | + * this prism. Primarily intended to support using Messager. |
| 28 | + */ |
| 29 | + final Values values; |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns true if the mirror is an instance of {@link |
| 33 | + * io.jstach.jstache.JStacheConfig @JStacheConfig} is present on the element, else false. |
| 34 | + * |
| 35 | + * @param mirror mirror. |
| 36 | + * @return true if prism is present. |
| 37 | + */ |
| 38 | + static boolean isInstance(AnnotationMirror mirror) { |
| 39 | + return getInstance(mirror) != null; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns true if {@link io.jstach.jstache.JStacheConfig @JStacheConfig} is present on the |
| 44 | + * element, else false. |
| 45 | + * |
| 46 | + * @param element element. |
| 47 | + * @return true if annotation is present on the element. |
| 48 | + */ |
| 49 | + static boolean isPresent(Element element) { |
| 50 | + return getInstanceOn(element) != null; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Return a prism representing the {@link io.jstach.jstache.JStacheConfig @JStacheConfig} |
| 55 | + * annotation present on the given element. similar to {@code |
| 56 | + * element.getAnnotation(JStacheConfig.class)} except that an instance of this class rather than |
| 57 | + * an instance of {@link io.jstach.jstache.JStacheConfig @JStacheConfig} is returned. |
| 58 | + * |
| 59 | + * @param element element. |
| 60 | + * @return prism on element or null if no annotation is found. |
| 61 | + */ |
| 62 | + static JStacheConfigPrism getInstanceOn(Element element) { |
| 63 | + final var mirror = getMirror(element); |
| 64 | + if (mirror == null) return null; |
| 65 | + return getInstance(mirror); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Return a Optional representing a nullable {@link |
| 70 | + * io.jstach.jstache.JStacheConfig @JStacheConfig} annotation on the given element. similar to |
| 71 | + * {@code element.getAnnotation(io.jstach.jstache.JStacheConfig.class)} except that an Optional of |
| 72 | + * this class rather than an instance of {@link io.jstach.jstache.JStacheConfig} is returned. |
| 73 | + * |
| 74 | + * @param element element. |
| 75 | + * @return prism optional for element. |
| 76 | + */ |
| 77 | + static Optional<JStacheConfigPrism> getOptionalOn(Element element) { |
| 78 | + final var mirror = getMirror(element); |
| 79 | + if (mirror == null) return Optional.empty(); |
| 80 | + return getOptional(mirror); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Return a prism of the {@link io.jstach.jstache.JStacheConfig @JStacheConfig} annotation from an |
| 85 | + * annotation mirror. |
| 86 | + * |
| 87 | + * @param mirror mirror. |
| 88 | + * @return prism for mirror or null if mirror is an incorrect type. |
| 89 | + */ |
| 90 | + static JStacheConfigPrism getInstance(AnnotationMirror mirror) { |
| 91 | + if (mirror == null || !PRISM_TYPE.equals(mirror.getAnnotationType().toString())) return null; |
| 92 | + |
| 93 | + return new JStacheConfigPrism(mirror); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Return an Optional representing a nullable {@link JStacheConfigPrism @JStacheConfigPrism} from |
| 98 | + * an annotation mirror. similar to {@code e.getAnnotation(io.jstach.jstache.JStacheConfig.class)} |
| 99 | + * except that an Optional of this class rather than an instance of {@link |
| 100 | + * io.jstach.jstache.JStacheConfig @JStacheConfig} is returned. |
| 101 | + * |
| 102 | + * @param mirror mirror. |
| 103 | + * @return prism optional for mirror. |
| 104 | + */ |
| 105 | + static Optional<JStacheConfigPrism> getOptional(AnnotationMirror mirror) { |
| 106 | + if (mirror == null || !PRISM_TYPE.equals(mirror.getAnnotationType().toString())) |
| 107 | + return Optional.empty(); |
| 108 | + |
| 109 | + return Optional.of(new JStacheConfigPrism(mirror)); |
| 110 | + } |
| 111 | + |
| 112 | + private JStacheConfigPrism(AnnotationMirror mirror) { |
| 113 | + for (final ExecutableElement key : mirror.getElementValues().keySet()) { |
| 114 | + memberValues.put(key.getSimpleName().toString(), mirror.getElementValues().get(key)); |
| 115 | + } |
| 116 | + for (final ExecutableElement member : |
| 117 | + ElementFilter.methodsIn(mirror.getAnnotationType().asElement().getEnclosedElements())) { |
| 118 | + defaults.put(member.getSimpleName().toString(), member.getDefaultValue()); |
| 119 | + } |
| 120 | + VariableElement typeMirror = getValue("type", VariableElement.class); |
| 121 | + valid = valid && typeMirror != null; |
| 122 | + _type = typeMirror == null ? null : typeMirror.getSimpleName().toString(); |
| 123 | + this.values = new Values(memberValues); |
| 124 | + this.mirror = mirror; |
| 125 | + this.isValid = valid; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns a String representing the value of the {@code io.jstach.jstache.JStacheType type()} |
| 130 | + * member of the Annotation. |
| 131 | + * |
| 132 | + * @see io.jstach.jstache.JStacheConfig#type() |
| 133 | + */ |
| 134 | + public String type() { |
| 135 | + return _type; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Determine whether the underlying AnnotationMirror has no errors. True if the underlying |
| 140 | + * AnnotationMirror has no errors. When true is returned, none of the methods will return null. |
| 141 | + * When false is returned, a least one member will either return null, or another prism that is |
| 142 | + * not valid. |
| 143 | + */ |
| 144 | + final boolean isValid; |
| 145 | + |
| 146 | + /** |
| 147 | + * The underlying AnnotationMirror of the annotation represented by this Prism. Primarily intended |
| 148 | + * to support using Messager. |
| 149 | + */ |
| 150 | + final AnnotationMirror mirror; |
| 151 | + |
| 152 | + /** |
| 153 | + * A class whose members correspond to those of {@link |
| 154 | + * io.jstach.jstache.JStacheConfig @JStacheConfig} but which each return the AnnotationValue |
| 155 | + * corresponding to that member in the model of the annotations. Returns null for defaulted |
| 156 | + * members. Used for Messager, so default values are not useful. |
| 157 | + */ |
| 158 | + static final class Values { |
| 159 | + private final Map<String, AnnotationValue> values; |
| 160 | + |
| 161 | + private Values(Map<String, AnnotationValue> values) { |
| 162 | + this.values = values; |
| 163 | + } |
| 164 | + |
| 165 | + AnnotationValue type() { |
| 166 | + return values.get("type"); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private final Map<String, AnnotationValue> defaults = new HashMap<>(10); |
| 171 | + private final Map<String, AnnotationValue> memberValues = |
| 172 | + new HashMap<>(10); |
| 173 | + private boolean valid = true; |
| 174 | + |
| 175 | + private <T> T getValue(String name, Class<T> clazz) { |
| 176 | + final T result = JStacheConfigPrism.getValue(memberValues, defaults, name, clazz); |
| 177 | + if (result == null) valid = false; |
| 178 | + return result; |
| 179 | + } |
| 180 | + |
| 181 | + private static AnnotationMirror getMirror(Element target) { |
| 182 | + for (final var m : target.getAnnotationMirrors()) { |
| 183 | + final CharSequence mfqn = |
| 184 | + ((TypeElement) m.getAnnotationType().asElement()).getQualifiedName(); |
| 185 | + if (PRISM_TYPE.contentEquals(mfqn)) return m; |
| 186 | + } |
| 187 | + return null; |
| 188 | + } |
| 189 | + |
| 190 | + private static <T> T getValue( |
| 191 | + Map<String, AnnotationValue> memberValues, |
| 192 | + Map<String, AnnotationValue> defaults, |
| 193 | + String name, |
| 194 | + Class<T> clazz) { |
| 195 | + AnnotationValue av = memberValues.get(name); |
| 196 | + if (av == null) av = defaults.get(name); |
| 197 | + if (av == null) { |
| 198 | + return null; |
| 199 | + } |
| 200 | + if (clazz.isInstance(av.getValue())) return clazz.cast(av.getValue()); |
| 201 | + return null; |
| 202 | + } |
| 203 | +} |
0 commit comments