1 /* 2 * Copyright (C) 2016 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 dagger.internal.codegen.base.MoreAnnotationValues.asAnnotationValues; 21 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; 22 23 import com.google.auto.common.AnnotationMirrors; 24 import com.google.common.base.Equivalence; 25 import com.google.common.collect.ImmutableList; 26 import java.util.Optional; 27 import javax.lang.model.element.AnnotationMirror; 28 import javax.lang.model.element.Name; 29 import javax.lang.model.type.TypeMirror; 30 31 /** 32 * A utility class for working with {@link AnnotationMirror} instances, similar to {@link 33 * AnnotationMirrors}. 34 */ 35 public final class MoreAnnotationMirrors { 36 MoreAnnotationMirrors()37 private MoreAnnotationMirrors() {} 38 39 /** 40 * Wraps an {@link Optional} of a type in an {@code Optional} of a {@link Equivalence.Wrapper} for 41 * that type. 42 */ wrapOptionalInEquivalence( Optional<AnnotationMirror> optional)43 public static Optional<Equivalence.Wrapper<AnnotationMirror>> wrapOptionalInEquivalence( 44 Optional<AnnotationMirror> optional) { 45 return optional.map(AnnotationMirrors.equivalence()::wrap); 46 } 47 48 /** 49 * Unwraps an {@link Optional} of a {@link Equivalence.Wrapper} into an {@code Optional} of the 50 * underlying type. 51 */ unwrapOptionalEquivalence( Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedOptional)52 public static Optional<AnnotationMirror> unwrapOptionalEquivalence( 53 Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedOptional) { 54 return wrappedOptional.map(Equivalence.Wrapper::get); 55 } 56 simpleName(AnnotationMirror annotationMirror)57 public static Name simpleName(AnnotationMirror annotationMirror) { 58 return annotationMirror.getAnnotationType().asElement().getSimpleName(); 59 } 60 61 /** 62 * Returns the list of types that is the value named {@code name} from {@code annotationMirror}. 63 * 64 * @throws IllegalArgumentException unless that member represents an array of types 65 */ getTypeListValue( AnnotationMirror annotationMirror, String name)66 public static ImmutableList<TypeMirror> getTypeListValue( 67 AnnotationMirror annotationMirror, String name) { 68 return asAnnotationValues(getAnnotationValue(annotationMirror, name)) 69 .stream() 70 .map(MoreAnnotationValues::asType) 71 .collect(toImmutableList()); 72 } 73 } 74