1 /* 2 * Copyright (C) 2023 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.model; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 21 import com.google.auto.value.AutoValue; 22 import com.squareup.javapoet.ClassName; 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 return scopeAnnotationType.xprocessing().hasAnnotation(SCOPE) 47 || scopeAnnotationType.xprocessing().hasAnnotation(SCOPE_JAVAX); 48 } 49 50 private static final ClassName PRODUCTION_SCOPE = 51 ClassName.get("dagger.producers", "ProductionScope"); 52 private static final ClassName SINGLETON = ClassName.get("jakarta.inject", "Singleton"); 53 private static final ClassName SINGLETON_JAVAX = ClassName.get("javax.inject", "Singleton"); 54 private static final ClassName REUSABLE = ClassName.get("dagger", "Reusable"); 55 private static final ClassName SCOPE = ClassName.get("jakarta.inject", "Scope"); 56 private static final ClassName SCOPE_JAVAX = ClassName.get("javax.inject", "Scope"); 57 58 59 /** The {@link DaggerAnnotation} that represents the scope annotation. */ scopeAnnotation()60 public abstract DaggerAnnotation scopeAnnotation(); 61 className()62 public final ClassName className() { 63 return scopeAnnotation().className(); 64 } 65 66 /** Returns {@code true} if this scope is the {@link javax.inject.Singleton @Singleton} scope. */ isSingleton()67 public final boolean isSingleton() { 68 return isScope(SINGLETON) || isScope(SINGLETON_JAVAX); 69 } 70 71 /** Returns {@code true} if this scope is the {@link dagger.Reusable @Reusable} scope. */ isReusable()72 public final boolean isReusable() { 73 return isScope(REUSABLE); 74 } 75 76 /** 77 * Returns {@code true} if this scope is the {@link 78 * dagger.producers.ProductionScope @ProductionScope} scope. 79 */ isProductionScope()80 public final boolean isProductionScope() { 81 return isScope(PRODUCTION_SCOPE); 82 } 83 isScope(ClassName annotation)84 private boolean isScope(ClassName annotation) { 85 return scopeAnnotation().className().equals(annotation); 86 } 87 88 /** Returns a debug representation of the scope. */ 89 @Override toString()90 public final String toString() { 91 return scopeAnnotation().toString(); 92 } 93 } 94