1 /* 2 * Copyright (C) 2015 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.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.common.AnnotationMirrors; 23 import com.google.auto.common.MoreElements; 24 import com.google.auto.common.MoreTypes; 25 import com.google.auto.value.AutoValue; 26 import com.google.common.base.Equivalence; 27 import com.squareup.javapoet.ClassName; 28 import dagger.Reusable; 29 import javax.inject.Singleton; 30 import javax.lang.model.element.AnnotationMirror; 31 import javax.lang.model.element.TypeElement; 32 33 /** A representation of a {@link javax.inject.Scope}. */ 34 @AutoValue 35 // TODO(ronshapiro): point to SimpleAnnotationMirror 36 public abstract class Scope { wrappedScopeAnnotation()37 abstract Equivalence.Wrapper<AnnotationMirror> wrappedScopeAnnotation(); 38 39 /** The {@link AnnotationMirror} that represents the scope annotation. */ scopeAnnotation()40 public final AnnotationMirror scopeAnnotation() { 41 return wrappedScopeAnnotation().get(); 42 } 43 44 /** The scope annotation element. */ scopeAnnotationElement()45 public final TypeElement scopeAnnotationElement() { 46 return MoreTypes.asTypeElement(scopeAnnotation().getAnnotationType()); 47 } 48 49 /** 50 * Creates a {@link Scope} object from the {@link javax.inject.Scope}-annotated annotation type. 51 */ scope(AnnotationMirror scopeAnnotation)52 public static Scope scope(AnnotationMirror scopeAnnotation) { 53 checkArgument(isScope(scopeAnnotation)); 54 return new AutoValue_Scope(AnnotationMirrors.equivalence().wrap(scopeAnnotation)); 55 } 56 57 /** 58 * Returns {@code true} if {@link #scopeAnnotation()} is a {@link javax.inject.Scope} annotation. 59 */ isScope(AnnotationMirror scopeAnnotation)60 public static boolean isScope(AnnotationMirror scopeAnnotation) { 61 return isScope(MoreElements.asType(scopeAnnotation.getAnnotationType().asElement())); 62 } 63 64 /** 65 * Returns {@code true} if {@code scopeAnnotationType} is a {@link javax.inject.Scope} annotation. 66 */ isScope(TypeElement scopeAnnotationType)67 public static boolean isScope(TypeElement scopeAnnotationType) { 68 return isAnnotationPresent(scopeAnnotationType, SCOPE.canonicalName()) 69 || isAnnotationPresent(scopeAnnotationType, SCOPE_JAVAX.canonicalName()); 70 } 71 72 private static final ClassName PRODUCTION_SCOPE = 73 ClassName.get("dagger.producers", "ProductionScope"); 74 private static final ClassName SINGLETON = ClassName.get("jakarta.inject", "Singleton"); 75 private static final ClassName SINGLETON_JAVAX = ClassName.get("javax.inject", "Singleton"); 76 private static final ClassName REUSABLE = ClassName.get("dagger", "Reusable"); 77 private static final ClassName SCOPE = ClassName.get("jakarta.inject", "Scope"); 78 private static final ClassName SCOPE_JAVAX = ClassName.get("javax.inject", "Scope"); 79 80 /** Returns {@code true} if this scope is the {@link Singleton @Singleton} scope. */ isSingleton()81 public final boolean isSingleton() { 82 return isScope(SINGLETON) || isScope(SINGLETON_JAVAX); 83 } 84 85 /** Returns {@code true} if this scope is the {@link Reusable @Reusable} scope. */ isReusable()86 public final boolean isReusable() { 87 return isScope(REUSABLE); 88 } 89 90 /** Returns {@code true} if this scope is the {@code @ProductionScope} scope. */ isProductionScope()91 public final boolean isProductionScope() { 92 return isScope(PRODUCTION_SCOPE); 93 } 94 isScope(ClassName className)95 private boolean isScope(ClassName className) { 96 return scopeAnnotationElement().getQualifiedName().contentEquals(className.canonicalName()); 97 } 98 99 /** Returns a debug representation of the scope. */ 100 @Override toString()101 public final String toString() { 102 return scopeAnnotation().toString(); 103 } 104 } 105