• 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.spi.model;
18 
19 import static com.google.auto.common.MoreElements.isAnnotationPresent;
20 import static com.google.common.base.Preconditions.checkArgument;
21 
22 import com.google.auto.value.AutoValue;
23 import com.squareup.javapoet.ClassName;
24 
25 /** A representation of a {@link javax.inject.Scope}. */
26 @AutoValue
27 public abstract class Scope {
28   /**
29    * Creates a {@link Scope} object from the {@link javax.inject.Scope}-annotated annotation type.
30    */
scope(DaggerAnnotation scopeAnnotation)31   public static Scope scope(DaggerAnnotation scopeAnnotation) {
32     checkArgument(isScope(scopeAnnotation));
33     return new AutoValue_Scope(scopeAnnotation);
34   }
35 
36   /**
37    * Returns {@code true} if {@link #scopeAnnotation()} is a {@link javax.inject.Scope} annotation.
38    */
isScope(DaggerAnnotation scopeAnnotation)39   public static boolean isScope(DaggerAnnotation scopeAnnotation) {
40     return isScope(scopeAnnotation.annotationTypeElement());
41   }
42 
43   /**
44    * Returns {@code true} if {@code scopeAnnotationType} is a {@link javax.inject.Scope} annotation.
45    */
isScope(DaggerTypeElement scopeAnnotationType)46   public static boolean isScope(DaggerTypeElement scopeAnnotationType) {
47     return isAnnotationPresent(scopeAnnotationType.java(), SCOPE.canonicalName())
48         || isAnnotationPresent(scopeAnnotationType.java(), SCOPE_JAVAX.canonicalName());
49   }
50 
51   private static final ClassName PRODUCTION_SCOPE =
52       ClassName.get("dagger.producers", "ProductionScope");
53   private static final ClassName SINGLETON = ClassName.get("jakarta.inject", "Singleton");
54   private static final ClassName SINGLETON_JAVAX = ClassName.get("javax.inject", "Singleton");
55   private static final ClassName REUSABLE = ClassName.get("dagger", "Reusable");
56   private static final ClassName SCOPE = ClassName.get("jakarta.inject", "Scope");
57   private static final ClassName SCOPE_JAVAX = ClassName.get("javax.inject", "Scope");
58 
59 
60   /** The {@link DaggerAnnotation} that represents the scope annotation. */
scopeAnnotation()61   public abstract DaggerAnnotation scopeAnnotation();
62 
className()63   public final ClassName className() {
64     return scopeAnnotation().className();
65   }
66 
67   /** Returns {@code true} if this scope is the {@link javax.inject.Singleton @Singleton} scope. */
isSingleton()68   public final boolean isSingleton() {
69     return isScope(SINGLETON) || isScope(SINGLETON_JAVAX);
70   }
71 
72   /** Returns {@code true} if this scope is the {@link dagger.Reusable @Reusable} scope. */
isReusable()73   public final boolean isReusable() {
74     return isScope(REUSABLE);
75   }
76 
77   /**
78    * Returns {@code true} if this scope is the {@link
79    * dagger.producers.ProductionScope @ProductionScope} scope.
80    */
isProductionScope()81   public final boolean isProductionScope() {
82     return isScope(PRODUCTION_SCOPE);
83   }
84 
isScope(ClassName annotation)85   private boolean isScope(ClassName annotation) {
86     return scopeAnnotation().className().equals(annotation);
87   }
88 
89   /** Returns a debug representation of the scope. */
90   @Override
toString()91   public final String toString() {
92     return scopeAnnotation().toString();
93   }
94 }
95