xref: /aosp_15_r20/external/dagger2/java/dagger/spi/model/Scope.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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.common.base.Preconditions.checkArgument;
20 
21 import com.google.auto.common.MoreElements;
22 import com.google.auto.value.AutoValue;
23 
24 /** A representation of a {@link javax.inject.Scope}. */
25 @AutoValue
26 public abstract class Scope {
27   /**
28    * Creates a {@link Scope} object from the {@link javax.inject.Scope}-annotated annotation type.
29    */
scope(DaggerAnnotation scopeAnnotation)30   public static Scope scope(DaggerAnnotation scopeAnnotation) {
31     checkArgument(isScope(scopeAnnotation));
32     return new AutoValue_Scope(scopeAnnotation);
33   }
34 
35   /**
36    * Returns {@code true} if {@link #scopeAnnotation()} is a {@link javax.inject.Scope} annotation.
37    */
isScope(DaggerAnnotation scopeAnnotation)38   public static boolean isScope(DaggerAnnotation scopeAnnotation) {
39     return isScope(scopeAnnotation.annotationTypeElement());
40   }
41 
42   /**
43    * Returns {@code true} if {@code scopeAnnotationType} is a {@link javax.inject.Scope} annotation.
44    */
isScope(DaggerTypeElement scopeAnnotationType)45   public static boolean isScope(DaggerTypeElement scopeAnnotationType) {
46     switch (scopeAnnotationType.backend()) {
47       case JAVAC:
48         return MoreElements.isAnnotationPresent(scopeAnnotationType.javac(), SCOPE)
49             || MoreElements.isAnnotationPresent(scopeAnnotationType.javac(), SCOPE_JAVAX);
50       case KSP:
51         return KspUtilsKt.hasAnnotation(scopeAnnotationType.ksp(), SCOPE)
52             || KspUtilsKt.hasAnnotation(scopeAnnotationType.ksp(), SCOPE_JAVAX);
53     }
54     throw new IllegalStateException(
55         String.format("Backend %s not supported yet.", scopeAnnotationType.backend()));
56   }
57 
isScope(String annotationName)58   private boolean isScope(String annotationName) {
59     return scopeAnnotation().toString().equals(annotationName);
60   }
61 
62   /** The {@link DaggerAnnotation} that represents the scope annotation. */
scopeAnnotation()63   public abstract DaggerAnnotation scopeAnnotation();
64 
65   private static final String PRODUCTION_SCOPE = "dagger.producers.ProductionScope";
66   private static final String SINGLETON = "jakarta.inject.Singleton";
67   private static final String SINGLETON_JAVAX = "javax.inject.Singleton";
68   private static final String REUSABLE = "dagger.Reusable";
69   private static final String SCOPE = "jakarta.inject.Scope";
70   private static final String SCOPE_JAVAX = "javax.inject.Scope";
71 
72   /** Returns {@code true} if this scope is the {@link javax.inject.Singleton @Singleton} scope. */
isSingleton()73   public final boolean isSingleton() {
74     return isScope(SINGLETON) || isScope(SINGLETON_JAVAX);
75   }
76 
77   /** Returns {@code true} if this scope is the {@link dagger.Reusable @Reusable} scope. */
isReusable()78   public final boolean isReusable() {
79     return isScope(REUSABLE);
80   }
81 
82   /**
83    * Returns {@code true} if this scope is the {@code @ProductionScope} scope.
84    */
isProductionScope()85   public final boolean isProductionScope() {
86     return isScope(PRODUCTION_SCOPE);
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