• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Dagger Authors.
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 dagger.internal.codegen.base;
18 
19 import static com.google.auto.common.AnnotationMirrors.getAnnotationValue;
20 import static com.google.auto.common.AnnotationMirrors.getAnnotationValuesWithDefaults;
21 
22 import com.google.common.collect.ImmutableList;
23 import java.util.List;
24 import java.util.Optional;
25 import javax.lang.model.element.AnnotationMirror;
26 import javax.lang.model.element.AnnotationValue;
27 import javax.lang.model.element.AnnotationValueVisitor;
28 import javax.lang.model.util.SimpleAnnotationValueVisitor8;
29 
30 /** Utility methods for working with {@link AnnotationValue} instances. */
31 public final class MoreAnnotationValues {
32   /**
33    * Returns the list of values represented by an array annotation value.
34    *
35    * @throws IllegalArgumentException unless {@code annotationValue} represents an array
36    */
asAnnotationValues(AnnotationValue annotationValue)37   public static ImmutableList<AnnotationValue> asAnnotationValues(AnnotationValue annotationValue) {
38     return annotationValue.accept(AS_ANNOTATION_VALUES, null);
39   }
40 
41   private static final AnnotationValueVisitor<ImmutableList<AnnotationValue>, String>
42       AS_ANNOTATION_VALUES =
43           new SimpleAnnotationValueVisitor8<ImmutableList<AnnotationValue>, String>() {
44             @Override
45             public ImmutableList<AnnotationValue> visitArray(
46                 List<? extends AnnotationValue> vals, String elementName) {
47               return ImmutableList.copyOf(vals);
48             }
49 
50             @Override
51             protected ImmutableList<AnnotationValue> defaultAction(Object o, String elementName) {
52               throw new IllegalArgumentException(elementName + " is not an array: " + o);
53             }
54           };
55 
56   /** Returns the int value of an annotation */
getIntValue(AnnotationMirror annotation, String valueName)57   public static int getIntValue(AnnotationMirror annotation, String valueName) {
58     return (int) getAnnotationValue(annotation, valueName).getValue();
59   }
60 
61   /** Returns an optional int value of an annotation if the value name is present */
getOptionalIntValue( AnnotationMirror annotation, String valueName)62   public static Optional<Integer> getOptionalIntValue(
63       AnnotationMirror annotation, String valueName) {
64     return isValuePresent(annotation, valueName)
65         ? Optional.of(getIntValue(annotation, valueName))
66         : Optional.empty();
67   }
68 
69   /** Returns the String value of an annotation */
getStringValue(AnnotationMirror annotation, String valueName)70   public static String getStringValue(AnnotationMirror annotation, String valueName) {
71     return (String) getAnnotationValue(annotation, valueName).getValue();
72   }
73 
74   /** Returns an optional String value of an annotation if the value name is present */
getOptionalStringValue( AnnotationMirror annotation, String valueName)75   public static Optional<String> getOptionalStringValue(
76       AnnotationMirror annotation, String valueName) {
77     return isValuePresent(annotation, valueName)
78         ? Optional.of(getStringValue(annotation, valueName))
79         : Optional.empty();
80   }
81 
82   /** Returns the int array value of an annotation */
getIntArrayValue(AnnotationMirror annotation, String valueName)83   public static int[] getIntArrayValue(AnnotationMirror annotation, String valueName) {
84     return asAnnotationValues(getAnnotationValue(annotation, valueName)).stream()
85         .mapToInt(it -> (int) it.getValue())
86         .toArray();
87   }
88 
89   /** Returns the String array value of an annotation */
getStringArrayValue(AnnotationMirror annotation, String valueName)90   public static String[] getStringArrayValue(AnnotationMirror annotation, String valueName) {
91     return asAnnotationValues(getAnnotationValue(annotation, valueName)).stream()
92         .map(it -> (String) it.getValue())
93         .toArray(String[]::new);
94   }
95 
isValuePresent(AnnotationMirror annotation, String valueName)96   private static boolean isValuePresent(AnnotationMirror annotation, String valueName) {
97     return getAnnotationValuesWithDefaults(annotation).keySet().stream()
98         .anyMatch(member -> member.getSimpleName().contentEquals(valueName));
99   }
100 
MoreAnnotationValues()101   private MoreAnnotationValues() {}
102 }
103