|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Google, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.auto.common; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 21 | +import static javax.lang.model.util.ElementFilter.methodsIn; |
| 22 | + |
| 23 | +import com.google.common.base.Joiner; |
| 24 | +import com.google.common.collect.ImmutableMap; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.LinkedHashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import javax.lang.model.element.AnnotationMirror; |
| 30 | +import javax.lang.model.element.AnnotationValue; |
| 31 | +import javax.lang.model.element.ElementKind; |
| 32 | +import javax.lang.model.element.ExecutableElement; |
| 33 | +import javax.lang.model.element.TypeElement; |
| 34 | +import javax.lang.model.type.DeclaredType; |
| 35 | + |
| 36 | +/** |
| 37 | + * A simple implementation of the {@link AnnotationMirror} interface. |
| 38 | + * |
| 39 | + * <p>This type implements {@link #equals(Object)} and {@link #hashCode()} using {@link |
| 40 | + * AnnotationMirrors#equivalence} in accordance with the {@link AnnotationMirror} spec. Some {@link |
| 41 | + * AnnotationMirror}s, however, do not correctly implement equals, you should always compare them |
| 42 | + * using {@link AnnotationMirrors#equivalence} anyway. |
| 43 | + */ |
| 44 | +public final class SimpleAnnotationMirror implements AnnotationMirror { |
| 45 | + private final TypeElement annotationType; |
| 46 | + private final ImmutableMap<String, ? extends AnnotationValue> namedValues; |
| 47 | + private final ImmutableMap<ExecutableElement, ? extends AnnotationValue> elementValues; |
| 48 | + |
| 49 | + private SimpleAnnotationMirror( |
| 50 | + TypeElement annotationType, Map<String, ? extends AnnotationValue> namedValues) { |
| 51 | + checkArgument( |
| 52 | + annotationType.getKind().equals(ElementKind.ANNOTATION_TYPE), |
| 53 | + "annotationType must be an annotation: %s", |
| 54 | + annotationType); |
| 55 | + Map<String, AnnotationValue> values = new LinkedHashMap<>(); |
| 56 | + Map<String, AnnotationValue> unusedValues = new LinkedHashMap<>(namedValues); |
| 57 | + List<String> missingMembers = new ArrayList<>(); |
| 58 | + for (ExecutableElement method : methodsIn(annotationType.getEnclosedElements())) { |
| 59 | + String memberName = method.getSimpleName().toString(); |
| 60 | + if (unusedValues.containsKey(memberName)) { |
| 61 | + values.put(memberName, unusedValues.remove(memberName)); |
| 62 | + } else if (method.getDefaultValue() != null) { |
| 63 | + values.put(memberName, method.getDefaultValue()); |
| 64 | + } else { |
| 65 | + missingMembers.add(memberName); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + checkArgument( |
| 70 | + unusedValues.isEmpty(), |
| 71 | + "namedValues has entries for members that are not in %s: %s", |
| 72 | + annotationType, |
| 73 | + unusedValues); |
| 74 | + checkArgument( |
| 75 | + missingMembers.isEmpty(), "namedValues is missing entries for: %s", missingMembers); |
| 76 | + |
| 77 | + this.annotationType = annotationType; |
| 78 | + this.namedValues = ImmutableMap.copyOf(namedValues); |
| 79 | + this.elementValues = |
| 80 | + methodsIn(annotationType.getEnclosedElements()) |
| 81 | + .stream() |
| 82 | + .collect(toImmutableMap(e -> e, e -> values.get(e.getSimpleName().toString()))); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * An object representing an {@linkplain ElementKind#ANNOTATION_TYPE annotation} instance. If |
| 87 | + * {@code annotationType} has any annotation members, they must have default values. |
| 88 | + */ |
| 89 | + public static AnnotationMirror of(TypeElement annotationType) { |
| 90 | + return of(annotationType, ImmutableMap.of()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * An object representing an {@linkplain ElementKind#ANNOTATION_TYPE annotation} instance. If |
| 95 | + * {@code annotationType} has any annotation members, they must either be present in {@code |
| 96 | + * namedValues} or have default values. |
| 97 | + */ |
| 98 | + public static AnnotationMirror of( |
| 99 | + TypeElement annotationType, Map<String, ? extends AnnotationValue> namedValues) { |
| 100 | + return new SimpleAnnotationMirror(annotationType, namedValues); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public DeclaredType getAnnotationType() { |
| 105 | + return MoreTypes.asDeclared(annotationType.asType()); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Map<ExecutableElement, ? extends AnnotationValue> getElementValues() { |
| 110 | + return elementValues; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toString() { |
| 115 | + StringBuilder builder = new StringBuilder("@").append(annotationType.getQualifiedName()); |
| 116 | + if (!namedValues.isEmpty()) { |
| 117 | + builder |
| 118 | + .append('(') |
| 119 | + .append(Joiner.on(", ").withKeyValueSeparator(" = ").join(namedValues)) |
| 120 | + .append(')'); |
| 121 | + } |
| 122 | + return builder.toString(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean equals(Object other) { |
| 127 | + return other instanceof AnnotationMirror |
| 128 | + && AnnotationMirrors.equivalence().equivalent(this, (AnnotationMirror) other); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public int hashCode() { |
| 133 | + return AnnotationMirrors.equivalence().hash(this); |
| 134 | + } |
| 135 | +} |
0 commit comments