• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.XElements.hasAnyAnnotation;
24 
25 import androidx.room.compiler.processing.XElement;
26 import androidx.room.compiler.processing.XTypeElement;
27 import com.google.common.collect.ImmutableSet;
28 import com.squareup.javapoet.ClassName;
29 import dagger.Component;
30 import dagger.Module;
31 import java.util.Optional;
32 
33 /**
34  * Utility methods related to dagger configuration annotations (e.g.: {@link Component} and {@link
35  * Module}).
36  */
37 public final class ConfigurationAnnotations {
38 
getSubcomponentCreator(XTypeElement subcomponent)39   public static Optional<XTypeElement> getSubcomponentCreator(XTypeElement subcomponent) {
40     checkArgument(subcomponent.hasAnyAnnotation(subcomponentAnnotations()));
41     return subcomponent.getEnclosedTypeElements().stream()
42         .filter(ConfigurationAnnotations::isSubcomponentCreator)
43         // TODO(bcorso): Consider doing toOptional() instead since there should be at most 1.
44         .findFirst();
45   }
46 
isSubcomponentCreator(XElement element)47   static boolean isSubcomponentCreator(XElement element) {
48     return hasAnyAnnotation(element, subcomponentCreatorAnnotations());
49   }
50 
51   /** Returns the enclosed types annotated with the given annotation. */
enclosedAnnotatedTypes( XTypeElement typeElement, ImmutableSet<ClassName> annotations)52   public static ImmutableSet<XTypeElement> enclosedAnnotatedTypes(
53       XTypeElement typeElement, ImmutableSet<ClassName> annotations) {
54     return typeElement.getEnclosedTypeElements().stream()
55         .filter(enclosedType -> hasAnyAnnotation(enclosedType, annotations))
56         .collect(toImmutableSet());
57   }
58 
ConfigurationAnnotations()59   private ConfigurationAnnotations() {}
60 }
61