|
| 1 | +package datadog.apt; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 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.ExecutableElement; |
| 9 | +import javax.lang.model.element.TypeElement; |
| 10 | +import javax.lang.model.element.VariableElement; |
| 11 | +import javax.lang.model.type.TypeMirror; |
| 12 | +import javax.lang.model.util.AbstractAnnotationValueVisitor6; |
| 13 | + |
| 14 | +/** |
| 15 | + * Utility class that works AnnotationMirrors & AnnotationValues |
| 16 | + * |
| 17 | + * <p>By convention, nulls pass through nicely to allow easy composition |
| 18 | + */ |
| 19 | +public final class AnnoUtils { |
| 20 | + private AnnoUtils() {} |
| 21 | + |
| 22 | + /** Finds the AnnotationMirror on Element of type TypeElement */ |
| 23 | + public static final AnnotationMirror findAnnotation(Element element, TypeElement annoType) { |
| 24 | + if (element == null || annoType == null) return null; |
| 25 | + |
| 26 | + for (AnnotationMirror annoMirror : element.getAnnotationMirrors()) { |
| 27 | + if (isA(annoMirror, annoType)) return annoMirror; |
| 28 | + } |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + public static final AnnotationMirror findAnnotation(Element element, Class<?> annoClass) { |
| 33 | + if (element == null || annoClass == null) return null; |
| 34 | + |
| 35 | + for (AnnotationMirror annoMirror : element.getAnnotationMirrors()) { |
| 36 | + if (isA(annoMirror, annoClass)) return annoMirror; |
| 37 | + } |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + public static final boolean isSuppressed(Element element, String checkName) { |
| 42 | + if (element == null) return false; |
| 43 | + if (isSuppressedHelper(element, checkName)) return true; |
| 44 | + |
| 45 | + for (Element enclosingElement = element.getEnclosingElement(); |
| 46 | + enclosingElement != null; |
| 47 | + enclosingElement = enclosingElement.getEnclosingElement()) { |
| 48 | + if (isSuppressedHelper(enclosingElement, checkName)) return true; |
| 49 | + } |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + private static final boolean isSuppressedHelper(Element element, String checkName) { |
| 54 | + AnnotationMirror mirror = findAnnotation(element, SuppressWarnings.class); |
| 55 | + AnnotationValue value = getValue(mirror); |
| 56 | + List<? extends AnnotationValue> suppressionList = asList(value); |
| 57 | + |
| 58 | + return contains(suppressionList, checkName); |
| 59 | + } |
| 60 | + |
| 61 | + public static final boolean contains(List<? extends AnnotationValue> annoList, Object value) { |
| 62 | + if (annoList == null) return false; |
| 63 | + |
| 64 | + for (AnnotationValue annoValue : annoList) { |
| 65 | + if (is(annoValue, value)) return true; |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + public static final boolean isA(AnnotationMirror annoMirror, TypeElement annoType) { |
| 71 | + if (annoMirror == null) return false; |
| 72 | + |
| 73 | + return annoMirror.getAnnotationType().asElement().equals(annoType); |
| 74 | + } |
| 75 | + |
| 76 | + public static final boolean isA(AnnotationMirror annoMirror, Class<?> annoClass) { |
| 77 | + if (annoMirror == null) return false; |
| 78 | + |
| 79 | + return TypeUtils.isClass(annoMirror.getAnnotationType(), annoClass); |
| 80 | + } |
| 81 | + |
| 82 | + public static final boolean is(AnnotationValue annoValue, Object obj) { |
| 83 | + // TODO: Add support for Class objects? |
| 84 | + return (annoValue == null) ? false : annoValue.getValue().equals(obj); |
| 85 | + } |
| 86 | + |
| 87 | + public static final TypeMirror asType(AnnotationValue annoValue) { |
| 88 | + return (annoValue == null) ? null : (TypeMirror) annoValue.getValue(); |
| 89 | + } |
| 90 | + |
| 91 | + public static final List<? extends AnnotationValue> asList(AnnotationValue annoValue) { |
| 92 | + if (annoValue == null) return null; |
| 93 | + |
| 94 | + return annoValue.accept( |
| 95 | + new AnnotationVisitor<List<? extends AnnotationValue>, Void>() { |
| 96 | + @Override |
| 97 | + public List<? extends AnnotationValue> visitArray( |
| 98 | + List<? extends AnnotationValue> vals, Void p) { |
| 99 | + return vals; |
| 100 | + } |
| 101 | + }, |
| 102 | + null); |
| 103 | + } |
| 104 | + |
| 105 | + public static final AnnotationValue getValue(AnnotationMirror annoMirror) { |
| 106 | + return getValue(annoMirror, "value"); |
| 107 | + } |
| 108 | + |
| 109 | + public static final AnnotationValue getValue(AnnotationMirror annoMirror, String simpleName) { |
| 110 | + if (annoMirror == null) return null; |
| 111 | + |
| 112 | + for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> elementEntry : |
| 113 | + annoMirror.getElementValues().entrySet()) { |
| 114 | + ExecutableElement element = elementEntry.getKey(); |
| 115 | + if (!element.getSimpleName().contentEquals(simpleName)) continue; |
| 116 | + |
| 117 | + return elementEntry.getValue(); |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + abstract static class AnnotationVisitor<R, P> extends AbstractAnnotationValueVisitor6<R, P> { |
| 123 | + @Override |
| 124 | + public R visitArray(List<? extends AnnotationValue> vals, P p) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public R visitBoolean(boolean b, P p) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public R visitByte(byte b, P p) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public R visitChar(char c, P p) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public R visitDouble(double d, P p) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public R visitFloat(float f, P p) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public R visitInt(int i, P p) { |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public R visitLong(long i, P p) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public R visitShort(short s, P p) { |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public R visitString(String s, P p) { |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public R visitType(TypeMirror t, P p) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public R visitEnumConstant(VariableElement c, P p) { |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public R visitAnnotation(AnnotationMirror a, P p) { |
| 185 | + return null; |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments