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