1 /* 2 * Copyright (C) 2014 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.binding; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 import static dagger.internal.codegen.base.ComponentAnnotation.subcomponentAnnotations; 21 import static dagger.internal.codegen.base.ComponentCreatorAnnotation.subcomponentCreatorAnnotations; 22 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 23 import static dagger.internal.codegen.xprocessing.XAnnotations.getClassName; 24 import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation; 25 26 import androidx.room.compiler.processing.XAnnotation; 27 import androidx.room.compiler.processing.XElement; 28 import androidx.room.compiler.processing.XType; 29 import androidx.room.compiler.processing.XTypeElement; 30 import com.google.common.collect.ImmutableSet; 31 import com.squareup.javapoet.ClassName; 32 import dagger.Component; 33 import dagger.Module; 34 import java.util.List; 35 import java.util.Optional; 36 import javax.lang.model.element.AnnotationMirror; 37 import javax.lang.model.element.Element; 38 import javax.lang.model.type.DeclaredType; 39 40 /** 41 * Utility methods related to dagger configuration annotations (e.g.: {@link Component} and {@link 42 * Module}). 43 */ 44 public final class ConfigurationAnnotations { 45 getSubcomponentCreator(XTypeElement subcomponent)46 public static Optional<XTypeElement> getSubcomponentCreator(XTypeElement subcomponent) { 47 checkArgument(subcomponent.hasAnyAnnotation(subcomponentAnnotations())); 48 return subcomponent.getEnclosedTypeElements().stream() 49 .filter(ConfigurationAnnotations::isSubcomponentCreator) 50 // TODO(bcorso): Consider doing toOptional() instead since there should be at most 1. 51 .findFirst(); 52 } 53 isSubcomponentCreator(XElement element)54 static boolean isSubcomponentCreator(XElement element) { 55 return hasAnyAnnotation(element, subcomponentCreatorAnnotations()); 56 } 57 58 /** Returns the first type that specifies this' nullability, or empty if none. */ getNullableAnnotation(XElement element)59 public static Optional<XAnnotation> getNullableAnnotation(XElement element) { 60 return element.getAllAnnotations().stream() 61 .filter(annotation -> getClassName(annotation).simpleName().contentEquals("Nullable")) 62 .findFirst(); 63 } 64 getNullableType(XElement element)65 public static Optional<XType> getNullableType(XElement element) { 66 return getNullableAnnotation(element).map(XAnnotation::getType); 67 } 68 69 /** Returns the first type that specifies this' nullability, or empty if none. */ getNullableType(Element element)70 public static Optional<DeclaredType> getNullableType(Element element) { 71 List<? extends AnnotationMirror> mirrors = element.getAnnotationMirrors(); 72 for (AnnotationMirror mirror : mirrors) { 73 if (mirror.getAnnotationType().asElement().getSimpleName().contentEquals("Nullable")) { 74 return Optional.of(mirror.getAnnotationType()); 75 } 76 } 77 return Optional.empty(); 78 } 79 80 /** Returns the enclosed types annotated with the given annotation. */ enclosedAnnotatedTypes( XTypeElement typeElement, ImmutableSet<ClassName> annotations)81 public static ImmutableSet<XTypeElement> enclosedAnnotatedTypes( 82 XTypeElement typeElement, ImmutableSet<ClassName> annotations) { 83 return typeElement.getEnclosedTypeElements().stream() 84 .filter(enclosedType -> hasAnyAnnotation(enclosedType, annotations)) 85 .collect(toImmutableSet()); 86 } 87 ConfigurationAnnotations()88 private ConfigurationAnnotations() {} 89 } 90