• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 androidx.room.compiler.processing.XAnnotation;
20 import androidx.room.compiler.processing.XProcessingEnv;
21 import com.squareup.javapoet.ClassName;
22 import dagger.internal.codegen.javapoet.TypeNames;
23 
24 /**
25  * Helper methods for getting types of producer annotations.
26  *
27  * <p>Note:These should only be used for cases where the annotations don't exist in the user's code.
28  * For example, all producer components implicitly have {@code @ProductionScope}, but it doesn't
29  * appear in the user's code. We need to get a reference to the scope annotation though to reuse
30  * classes from regular Dagger like the {@code ComponentDescriptor}.
31  */
32 public final class ProducerAnnotations {
33   private static final ClassName ANNOTATION_USAGES =
34       ClassName.get("dagger.producers.internal", "AnnotationUsages");
35   private static final ClassName PRODUCTION_USAGE =
36       ANNOTATION_USAGES.nestedClass("ProductionUsage");
37   private static final ClassName PRODUCTION_IMPLEMENTATION_USAGE =
38       ANNOTATION_USAGES.nestedClass("ProductionImplementationUsage");
39   private static final ClassName PRODUCTION_SCOPE_USAGE =
40       ANNOTATION_USAGES.nestedClass("ProductionScopeUsage");
41 
42   /** Returns a {@link dagger.producers.internal.ProductionImplementation} qualifier. */
43   // TODO(bcorso): We could probably remove the need for this if we define a new type,
44   //  "ProductionImplementationExecutor", rather than binding "@ProductionImplementation Executor".
productionImplementationQualifier(XProcessingEnv processingEnv)45   public static XAnnotation productionImplementationQualifier(XProcessingEnv processingEnv) {
46     return processingEnv.findTypeElement(PRODUCTION_IMPLEMENTATION_USAGE)
47         .getAnnotation(TypeNames.PRODUCTION_IMPLEMENTATION);
48   }
49 
50   /** Returns a {@link dagger.producers.Production} qualifier. */
51   // TODO(bcorso): We could probably remove the need for this. It's currently only used in
52   //  "DependsOnProductionExecutorValidator", but we could implement that without this.
productionQualifier(XProcessingEnv processingEnv)53   public static XAnnotation productionQualifier(XProcessingEnv processingEnv) {
54     return processingEnv.findTypeElement(PRODUCTION_USAGE).getAnnotation(TypeNames.PRODUCTION);
55   }
56 
57   /** Returns a {@link dagger.producers.ProductionScope} scope. */
58   // TODO(bcorso): We could probably remove the need for this, but it would require changing
59   //  Dagger SPI's public API. In particular, Scope should probably only require a ClassName rather
60   //  than an actual annotation type.
productionScope(XProcessingEnv processingEnv)61   public static XAnnotation productionScope(XProcessingEnv processingEnv) {
62     return processingEnv.findTypeElement(PRODUCTION_SCOPE_USAGE)
63         .getAnnotation(TypeNames.PRODUCTION_SCOPE);
64   }
65 
ProducerAnnotations()66   private ProducerAnnotations() {}
67 }
68