1 /* 2 * Copyright (C) 2017 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.base; 18 19 import static com.google.common.collect.Iterables.getOnlyElement; 20 import static dagger.internal.codegen.base.DiagnosticFormatting.stripCommonTypePrefixes; 21 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 22 23 import com.google.auto.common.AnnotationMirrors; 24 import com.google.common.collect.ImmutableSet; 25 import dagger.internal.codegen.langmodel.DaggerElements; 26 import dagger.model.Scope; 27 import dagger.producers.ProductionScope; 28 import java.lang.annotation.Annotation; 29 import java.util.Optional; 30 import javax.inject.Singleton; 31 import javax.lang.model.element.Element; 32 33 /** Common names and convenience methods for {@link Scope}s. */ 34 public final class Scopes { 35 36 /** Returns a representation for {@link ProductionScope @ProductionScope} scope. */ productionScope(DaggerElements elements)37 public static Scope productionScope(DaggerElements elements) { 38 return scope(elements, ProductionScope.class); 39 } 40 41 /** Returns a representation for {@link Singleton @Singleton} scope. */ singletonScope(DaggerElements elements)42 public static Scope singletonScope(DaggerElements elements) { 43 return scope(elements, Singleton.class); 44 } 45 46 /** 47 * Creates a {@link Scope} object from the {@link javax.inject.Scope}-annotated annotation type. 48 */ scope( DaggerElements elements, Class<? extends Annotation> scopeAnnotationClass)49 private static Scope scope( 50 DaggerElements elements, Class<? extends Annotation> scopeAnnotationClass) { 51 return Scope.scope(SimpleAnnotationMirror.of(elements.getTypeElement(scopeAnnotationClass))); 52 } 53 54 /** 55 * Returns at most one associated scoped annotation from the source code element, throwing an 56 * exception if there are more than one. 57 */ uniqueScopeOf(Element element)58 public static Optional<Scope> uniqueScopeOf(Element element) { 59 // TODO(ronshapiro): Use MoreCollectors.toOptional() once we can use guava-jre 60 return Optional.ofNullable(getOnlyElement(scopesOf(element), null)); 61 } 62 63 /** 64 * Returns the readable source representation (name with @ prefix) of the scope's annotation type. 65 * 66 * <p>It's readable source because it has had common package prefixes removed, e.g. 67 * {@code @javax.inject.Singleton} is returned as {@code @Singleton}. 68 */ getReadableSource(Scope scope)69 public static String getReadableSource(Scope scope) { 70 return stripCommonTypePrefixes(scope.toString()); 71 } 72 73 /** Returns all of the associated scopes for a source code element. */ scopesOf(Element element)74 public static ImmutableSet<Scope> scopesOf(Element element) { 75 return AnnotationMirrors.getAnnotatedAnnotations(element, javax.inject.Scope.class) 76 .stream() 77 .map(Scope::scope) 78 .collect(toImmutableSet()); 79 } 80 } 81