| /* |
| * Copyright (C) 2021 The Dagger Authors. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package dagger.android.processor; |
| |
| import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; |
| |
| import com.google.auto.common.MoreElements; |
| import com.google.common.collect.ImmutableSet; |
| import com.squareup.javapoet.ClassName; |
| import java.util.Optional; |
| import javax.lang.model.element.AnnotationMirror; |
| import javax.lang.model.element.Element; |
| import javax.lang.model.element.TypeElement; |
| |
| // TODO(bcorso): Dedupe with dagger/internal/codegen/langmodel/DaggerElements.java? |
| // TODO(bcorso): Contribute upstream to auto common? |
| /** Similar to auto common, but uses {@link ClassName} rather than {@link Class}. */ |
| final class MoreDaggerElements { |
| /** |
| * Returns {@code true} iff the given element has an {@link AnnotationMirror} whose {@linkplain |
| * AnnotationMirror#getAnnotationType() annotation type} has the same canonical name as that of |
| * {@code annotationClass}. This method is a safer alternative to calling {@link |
| * Element#getAnnotation} and checking for {@code null} as it avoids any interaction with |
| * annotation proxies. |
| */ |
| public static boolean isAnnotationPresent(Element element, ClassName annotationName) { |
| return getAnnotationMirror(element, annotationName).isPresent(); |
| } |
| |
| /** |
| * Returns an {@link AnnotationMirror} for the annotation of type {@code annotationClass} on |
| * {@code element}, or {@link Optional#empty()} if no such annotation exists. This method is a |
| * safer alternative to calling {@link Element#getAnnotation} as it avoids any interaction with |
| * annotation proxies. |
| */ |
| public static Optional<AnnotationMirror> getAnnotationMirror( |
| Element element, ClassName annotationName) { |
| String annotationClassName = annotationName.canonicalName(); |
| for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { |
| TypeElement annotationTypeElement = |
| MoreElements.asType(annotationMirror.getAnnotationType().asElement()); |
| if (annotationTypeElement.getQualifiedName().contentEquals(annotationClassName)) { |
| return Optional.of(annotationMirror); |
| } |
| } |
| return Optional.empty(); |
| } |
| |
| public static ImmutableSet<AnnotationMirror> getAnnotatedAnnotations( |
| Element element, ClassName annotationName) { |
| return element.getAnnotationMirrors().stream() |
| .filter(input -> isAnnotationPresent(input.getAnnotationType().asElement(), annotationName)) |
| .collect(toImmutableSet()); |
| } |
| |
| private MoreDaggerElements() {} |
| } |