• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 dagger.Reusable;
28 import dagger.producers.ProductionScope;
29 import java.lang.annotation.Annotation;
30 import javax.inject.Singleton;
31 import javax.lang.model.element.AnnotationMirror;
32 import javax.lang.model.element.TypeElement;
33 
34 /** A representation of a {@link javax.inject.Scope}. */
35 @AutoValue
36 // TODO(ronshapiro): point to SimpleAnnotationMirror
37 public abstract class Scope {
wrappedScopeAnnotation()38   abstract Equivalence.Wrapper<AnnotationMirror> wrappedScopeAnnotation();
39 
40   /** The {@link AnnotationMirror} that represents the scope annotation. */
scopeAnnotation()41   public final AnnotationMirror scopeAnnotation() {
42     return wrappedScopeAnnotation().get();
43   }
44 
45   /** The scope annotation element. */
scopeAnnotationElement()46   public final TypeElement scopeAnnotationElement() {
47     return MoreTypes.asTypeElement(scopeAnnotation().getAnnotationType());
48   }
49 
50   /**
51    * Creates a {@link Scope} object from the {@link javax.inject.Scope}-annotated annotation type.
52    */
scope(AnnotationMirror scopeAnnotation)53   public static Scope scope(AnnotationMirror scopeAnnotation) {
54     checkArgument(isScope(scopeAnnotation));
55     return new AutoValue_Scope(AnnotationMirrors.equivalence().wrap(scopeAnnotation));
56   }
57 
58   /**
59    * Returns {@code true} if {@link #scopeAnnotation()} is a {@link javax.inject.Scope} annotation.
60    */
isScope(AnnotationMirror scopeAnnotation)61   public static boolean isScope(AnnotationMirror scopeAnnotation) {
62     return isScope(MoreElements.asType(scopeAnnotation.getAnnotationType().asElement()));
63   }
64 
65   /**
66    * Returns {@code true} if {@code scopeAnnotationType} is a {@link javax.inject.Scope} annotation.
67    */
isScope(TypeElement scopeAnnotationType)68   public static boolean isScope(TypeElement scopeAnnotationType) {
69     return isAnnotationPresent(scopeAnnotationType, javax.inject.Scope.class);
70   }
71 
72   /** Returns {@code true} if this scope is the {@link Singleton @Singleton} scope. */
isSingleton()73   public final boolean isSingleton() {
74     return isScope(Singleton.class);
75   }
76 
77   /** Returns {@code true} if this scope is the {@link Reusable @Reusable} scope. */
isReusable()78   public final boolean isReusable() {
79     return isScope(Reusable.class);
80   }
81 
82   /** Returns {@code true} if this scope is the {@link ProductionScope @ProductionScope} scope. */
isProductionScope()83   public final boolean isProductionScope() {
84     return isScope(ProductionScope.class);
85   }
86 
isScope(Class<? extends Annotation> annotation)87   private boolean isScope(Class<? extends Annotation> annotation) {
88     return scopeAnnotationElement().getQualifiedName().contentEquals(annotation.getCanonicalName());
89   }
90 
91   /** Returns a debug representation of the scope. */
92   @Override
toString()93   public final String toString() {
94     return scopeAnnotation().toString();
95   }
96 }
97