Skip to content

Commit

Permalink
Migrate Scopes to XProcessing.
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 407732188
  • Loading branch information
bcorso authored and Dagger Team committed Nov 5, 2021
1 parent da79a12 commit 4329998
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
17 changes: 6 additions & 11 deletions java/dagger/internal/codegen/base/Scopes.java
Expand Up @@ -20,16 +20,17 @@
import static dagger.internal.codegen.base.DiagnosticFormatting.stripCommonTypePrefixes;
import static dagger.internal.codegen.extension.DaggerCollectors.toOptional;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XElements.getAnnotatedAnnotations;
import static dagger.spi.model.DaggerAnnotation.fromJava;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XProcessingEnv;
import com.google.auto.common.AnnotationMirrors;
import androidx.room.compiler.processing.compat.XConverters;
import com.google.common.collect.ImmutableSet;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.spi.model.DaggerAnnotation;
import dagger.spi.model.Scope;
import java.util.Optional;
import javax.lang.model.element.Element;

/** Common names and convenience methods for {@link Scope}s. */
public final class Scopes {
Expand All @@ -43,7 +44,7 @@ public static Scope productionScope(XProcessingEnv processingEnv) {
* Returns at most one associated scoped annotation from the source code element, throwing an
* exception if there are more than one.
*/
public static Optional<Scope> uniqueScopeOf(Element element) {
public static Optional<Scope> uniqueScopeOf(XElement element) {
return scopesOf(element).stream().collect(toOptional());
}

Expand All @@ -59,14 +60,8 @@ public static String getReadableSource(Scope scope) {

/** Returns all of the associated scopes for a source code element. */
public static ImmutableSet<Scope> scopesOf(XElement element) {
return scopesOf(toJavac(element));
}

/** Returns all of the associated scopes for a source code element. */
public static ImmutableSet<Scope> scopesOf(Element element) {
// TODO(bcorso): Replace Scope class reference with class name once auto-common is updated.
return AnnotationMirrors.getAnnotatedAnnotations(element, javax.inject.Scope.class)
.stream()
return getAnnotatedAnnotations(element, TypeNames.SCOPE).stream()
.map(XConverters::toJavac)
.map(DaggerAnnotation::fromJava)
.map(Scope::scope)
.collect(toImmutableSet());
Expand Down
22 changes: 17 additions & 5 deletions java/dagger/internal/codegen/binding/BindingFactory.java
Expand Up @@ -19,6 +19,7 @@
import static androidx.room.compiler.processing.XElementKt.isMethod;
import static androidx.room.compiler.processing.XElementKt.isVariableElement;
import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static androidx.room.compiler.processing.compat.XConverters.toXProcessing;
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static com.google.auto.common.MoreTypes.asDeclared;
import static com.google.auto.common.MoreTypes.asTypeElement;
Expand Down Expand Up @@ -56,6 +57,7 @@
import androidx.room.compiler.processing.XConstructorElement;
import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XType;
import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.XVariableElement;
Expand Down Expand Up @@ -97,6 +99,7 @@

/** A factory for {@link Binding} objects. */
public final class BindingFactory {
private final XProcessingEnv processingEnv;
private final DaggerTypes types;
private final KeyFactory keyFactory;
private final DependencyRequestFactory dependencyRequestFactory;
Expand All @@ -107,13 +110,15 @@ public final class BindingFactory {

@Inject
BindingFactory(
XProcessingEnv processingEnv,
DaggerTypes types,
DaggerElements elements,
KeyFactory keyFactory,
DependencyRequestFactory dependencyRequestFactory,
InjectionSiteFactory injectionSiteFactory,
InjectionAnnotations injectionAnnotations,
KotlinMetadataUtil metadataUtil) {
this.processingEnv = processingEnv;
this.types = types;
this.elements = elements;
this.keyFactory = keyFactory;
Expand Down Expand Up @@ -192,7 +197,9 @@ public ProvisionBinding injectionBinding(
isAnnotationPresent(constructorElement, AssistedInject.class)
? ASSISTED_INJECTION
: INJECTION)
.scope(uniqueScopeOf(constructorElement.getEnclosingElement()));
.scope(
uniqueScopeOf(
toXProcessing(constructorElement.getEnclosingElement(), processingEnv)));

if (hasNonDefaultTypeParameters(key.type().java(), types)) {
builder.unresolved(injectionBinding(constructorElement, Optional.empty()));
Expand Down Expand Up @@ -261,7 +268,7 @@ public ProvisionBinding providesMethodBinding(
keyFactory.forProvidesMethod(providesMethod, contributedBy),
this::providesMethodBinding)
.kind(PROVISION)
.scope(uniqueScopeOf(providesMethod))
.scope(uniqueScopeOf(toXProcessing(providesMethod, processingEnv)))
.nullableType(getNullableType(providesMethod))
.build();
}
Expand Down Expand Up @@ -427,7 +434,7 @@ public ContributionBinding componentDependencyMethodBinding(
.key(keyFactory.forComponentMethod(dependencyMethod))
.nullableType(getNullableType(dependencyMethod))
.kind(COMPONENT_PROVISION)
.scope(uniqueScopeOf(dependencyMethod));
.scope(uniqueScopeOf(toXProcessing(dependencyMethod, processingEnv)));
}
return builder
.contributionType(ContributionType.UNIQUE)
Expand Down Expand Up @@ -510,7 +517,9 @@ ContributionBinding delegateBinding(
case PROVISION:
return buildDelegateBinding(
ProvisionBinding.builder()
.scope(uniqueScopeOf(delegateDeclaration.bindingElement().get()))
.scope(
uniqueScopeOf(
toXProcessing(delegateDeclaration.bindingElement().get(), processingEnv)))
.nullableType(actualBinding.nullableType()),
delegateDeclaration,
TypeNames.PROVIDER);
Expand All @@ -526,7 +535,10 @@ ContributionBinding delegateBinding(
*/
public ContributionBinding unresolvedDelegateBinding(DelegateDeclaration delegateDeclaration) {
return buildDelegateBinding(
ProvisionBinding.builder().scope(uniqueScopeOf(delegateDeclaration.bindingElement().get())),
ProvisionBinding.builder()
.scope(
uniqueScopeOf(
toXProcessing(delegateDeclaration.bindingElement().get(), processingEnv))),
delegateDeclaration,
TypeNames.PROVIDER);
}
Expand Down
Expand Up @@ -269,7 +269,9 @@ private String repeatedModulesWithScopeError(

private ImmutableSet<Scope> moduleScopes(ModuleDescriptor module) {
return FluentIterable.concat(module.allBindingDeclarations())
.transform(declaration -> uniqueScopeOf(declaration.bindingElement().get()))
.transform(
declaration ->
uniqueScopeOf(toXProcessing(declaration.bindingElement().get(), processingEnv)))
.filter(scope -> scope.isPresent() && !scope.get().isReusable())
.transform(scope -> scope.get())
.toSet();
Expand Down

0 comments on commit 4329998

Please sign in to comment.