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.type.TypeMirror; 29 import javax.lang.model.util.SimpleAnnotationValueVisitor8; 30 31 /** Utility methods for working with {@link AnnotationValue} instances. */ 32 public final class MoreAnnotationValues { 33 /** 34 * Returns the list of values represented by an array annotation value. 35 * 36 * @throws IllegalArgumentException unless {@code annotationValue} represents an array 37 */ asAnnotationValues(AnnotationValue annotationValue)38 public static ImmutableList<AnnotationValue> asAnnotationValues(AnnotationValue annotationValue) { 39 return annotationValue.accept(AS_ANNOTATION_VALUES, null); 40 } 41 42 private static final AnnotationValueVisitor<ImmutableList<AnnotationValue>, String> 43 AS_ANNOTATION_VALUES = 44 new SimpleAnnotationValueVisitor8<ImmutableList<AnnotationValue>, String>() { 45 @Override 46 public ImmutableList<AnnotationValue> visitArray( 47 List<? extends AnnotationValue> vals, String elementName) { 48 return ImmutableList.copyOf(vals); 49 } 50 51 @Override 52 protected ImmutableList<AnnotationValue> defaultAction(Object o, String elementName) { 53 throw new IllegalArgumentException(elementName + " is not an array: " + o); 54 } 55 }; 56 57 /** 58 * Returns the type represented by an annotation value. 59 * 60 * @throws IllegalArgumentException unless {@code annotationValue} represents a single type 61 */ asType(AnnotationValue annotationValue)62 public static TypeMirror asType(AnnotationValue annotationValue) { 63 return AS_TYPE.visit(annotationValue); 64 } 65 66 private static final AnnotationValueVisitor<TypeMirror, Void> AS_TYPE = 67 new SimpleAnnotationValueVisitor8<TypeMirror, Void>() { 68 @Override 69 public TypeMirror visitType(TypeMirror t, Void p) { 70 return t; 71 } 72 73 @Override 74 protected TypeMirror defaultAction(Object o, Void p) { 75 throw new TypeNotPresentException(o.toString(), null); 76 } 77 }; 78 79 /** Returns the int value of an annotation */ getIntValue(AnnotationMirror annotation, String valueName)80 public static int getIntValue(AnnotationMirror annotation, String valueName) { 81 return (int) getAnnotationValue(annotation, valueName).getValue(); 82 } 83 84 /** Returns an optional int value of an annotation if the value name is present */ getOptionalIntValue( AnnotationMirror annotation, String valueName)85 public static Optional<Integer> getOptionalIntValue( 86 AnnotationMirror annotation, String valueName) { 87 return isValuePresent(annotation, valueName) 88 ? Optional.of(getIntValue(annotation, valueName)) 89 : Optional.empty(); 90 } 91 92 /** Returns the String value of an annotation */ getStringValue(AnnotationMirror annotation, String valueName)93 public static String getStringValue(AnnotationMirror annotation, String valueName) { 94 return (String) getAnnotationValue(annotation, valueName).getValue(); 95 } 96 97 /** Returns an optional String value of an annotation if the value name is present */ getOptionalStringValue( AnnotationMirror annotation, String valueName)98 public static Optional<String> getOptionalStringValue( 99 AnnotationMirror annotation, String valueName) { 100 return isValuePresent(annotation, valueName) 101 ? Optional.of(getStringValue(annotation, valueName)) 102 : Optional.empty(); 103 } 104 105 /** Returns the int array value of an annotation */ getIntArrayValue(AnnotationMirror annotation, String valueName)106 public static int[] getIntArrayValue(AnnotationMirror annotation, String valueName) { 107 return asAnnotationValues(getAnnotationValue(annotation, valueName)).stream() 108 .mapToInt(it -> (int) it.getValue()) 109 .toArray(); 110 } 111 112 /** Returns the String array value of an annotation */ getStringArrayValue(AnnotationMirror annotation, String valueName)113 public static String[] getStringArrayValue(AnnotationMirror annotation, String valueName) { 114 return asAnnotationValues(getAnnotationValue(annotation, valueName)).stream() 115 .map(it -> (String) it.getValue()) 116 .toArray(String[]::new); 117 } 118 isValuePresent(AnnotationMirror annotation, String valueName)119 private static boolean isValuePresent(AnnotationMirror annotation, String valueName) { 120 return getAnnotationValuesWithDefaults(annotation).keySet().stream() 121 .anyMatch(member -> member.getSimpleName().contentEquals(valueName)); 122 } 123 MoreAnnotationValues()124 private MoreAnnotationValues() {} 125 } 126