Skip to content

Commit

Permalink
Cleanup usages of Key.Builder, use KeyFactory instead.
Browse files Browse the repository at this point in the history
In a follow-up CL we will need to use the `CompilerOptions` when creating keys, so this CL replaces usages of `Key.Builder` with `KeyFactory` where we can easily get access to the `CompilerOptions`.

In addition, I've removed some unnecessary parts of `KeyFactoryTest` that was testing more of the implementation details of `KeyFactory` and relied heavily on `Key.Builder`.

RELNOTES=N/A
PiperOrigin-RevId: 535366633
  • Loading branch information
bcorso authored and Dagger Team committed May 25, 2023
1 parent 5926def commit 02176dc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 53 deletions.
5 changes: 2 additions & 3 deletions java/dagger/internal/codegen/binding/BindingFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.model.BindingKind;
import dagger.internal.codegen.model.DaggerAnnotation;
import dagger.internal.codegen.model.DaggerType;
import dagger.internal.codegen.model.DependencyRequest;
import dagger.internal.codegen.model.Key;
import dagger.internal.codegen.model.RequestKind;
Expand Down Expand Up @@ -162,12 +161,12 @@ public ProvisionBinding assistedFactoryBinding(
XMethodType factoryMethodType = factoryMethod.asMemberOf(factoryType);
return ProvisionBinding.builder()
.contributionType(ContributionType.UNIQUE)
.key(Key.builder(DaggerType.from(factoryType)).build())
.key(keyFactory.forType(factoryType))
.bindingElement(factory)
.provisionDependencies(
ImmutableSet.of(
DependencyRequest.builder()
.key(Key.builder(DaggerType.from(factoryMethodType.getReturnType())).build())
.key(keyFactory.forType(factoryMethodType.getReturnType()))
.kind(RequestKind.PROVIDER)
.build()))
.kind(ASSISTED_FACTORY)
Expand Down
13 changes: 5 additions & 8 deletions java/dagger/internal/codegen/binding/KeyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ Key forSubcomponentCreatorMethod(
XMethodElement subcomponentCreatorMethod, XType declaredContainer) {
checkArgument(isDeclared(declaredContainer));
XMethodType resolvedMethod = subcomponentCreatorMethod.asMemberOf(declaredContainer);
return Key.builder(DaggerType.from(resolvedMethod.getReturnType())).build();
return forType(resolvedMethod.getReturnType());
}

public Key forSubcomponentCreator(XType creatorType) {
return Key.builder(DaggerType.from(creatorType)).build();
return forType(creatorType);
}

public Key forProvidesMethod(XMethodElement method, XTypeElement contributingModule) {
Expand Down Expand Up @@ -234,16 +234,15 @@ private Key forMethod(XMethodElement method, XType keyType) {
}

public Key forInjectConstructorWithResolvedType(XType type) {
return Key.builder(DaggerType.from(type)).build();
return forType(type);
}

// TODO(ronshapiro): Remove these conveniences which are simple wrappers around Key.Builder
Key forType(XType type) {
return Key.builder(DaggerType.from(type)).build();
}

public Key forMembersInjectedType(XType type) {
return Key.builder(DaggerType.from(type)).build();
return forType(type);
}

Key forQualifiedType(Optional<XAnnotation> qualifier, XType type) {
Expand All @@ -265,9 +264,7 @@ public Key forProductionImplementationExecutor() {
}

public Key forProductionComponentMonitor() {
return Key.builder(
DaggerType.from(processingEnv.requireType(TypeNames.PRODUCTION_COMPONENT_MONITOR)))
.build();
return forType(processingEnv.requireType(TypeNames.PRODUCTION_COMPONENT_MONITOR));
}

/**
Expand Down
43 changes: 1 addition & 42 deletions javatests/dagger/internal/codegen/KeyFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import static dagger.internal.codegen.xprocessing.XTypes.isPrimitive;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import androidx.room.compiler.processing.XAnnotation;
import androidx.room.compiler.processing.XConstructorElement;
import androidx.room.compiler.processing.XFieldElement;
import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XType;
Expand All @@ -37,10 +35,6 @@
import dagger.Provides;
import dagger.internal.codegen.binding.KeyFactory;
import dagger.internal.codegen.javac.JavacPluginModule;
import dagger.internal.codegen.model.DaggerAnnotation;
import dagger.internal.codegen.model.DaggerExecutableElement;
import dagger.internal.codegen.model.DaggerType;
import dagger.internal.codegen.model.DaggerTypeElement;
import dagger.internal.codegen.model.Key;
import dagger.multibindings.ElementsIntoSet;
import dagger.multibindings.IntoSet;
Expand Down Expand Up @@ -83,7 +77,6 @@ public void forInjectConstructorWithResolvedType() {
Key key =
keyFactory.forInjectConstructorWithResolvedType(
constructor.getEnclosingElement().getType());
assertThat(key).isEqualTo(Key.builder(DaggerType.from(typeElement.getType())).build());
assertThat(key.toString()).isEqualTo("dagger.internal.codegen.KeyFactoryTest.InjectedClass");
}

Expand All @@ -94,12 +87,10 @@ static final class InjectedClass {

@Test
public void forProvidesMethod() {
XType stringType = processingEnv.requireType(String.class.getCanonicalName());
XTypeElement moduleElement =
processingEnv.requireTypeElement(ProvidesMethodModule.class.getCanonicalName());
XMethodElement providesMethod = getOnlyElement(moduleElement.getDeclaredMethods());
Key key = keyFactory.forProvidesMethod(providesMethod, moduleElement);
assertThat(key).isEqualTo(Key.builder(DaggerType.from(stringType)).build());
assertThat(key.toString()).isEqualTo("java.lang.String");
}

Expand Down Expand Up @@ -139,17 +130,7 @@ public void qualifiedKeyEquivalents() {
processingEnv.requireTypeElement(QualifiedProvidesMethodModule.class.getCanonicalName());
XMethodElement providesMethod = getOnlyElement(moduleElement.getDeclaredMethods());
Key provisionKey = keyFactory.forProvidesMethod(providesMethod, moduleElement);

XType type = processingEnv.requireType(String.class.getCanonicalName());
XTypeElement injectableElement =
processingEnv.requireTypeElement(QualifiedFieldHolder.class.getCanonicalName());
XFieldElement injectionField = getOnlyElement(injectableElement.getDeclaredFields());
XAnnotation qualifier = getOnlyElement(injectionField.getAllAnnotations());
Key injectionKey =
Key.builder(DaggerType.from(type)).qualifier(DaggerAnnotation.from(qualifier)).build();

assertThat(provisionKey).isEqualTo(injectionKey);
assertThat(injectionKey.toString())
assertThat(provisionKey.toString())
.isEqualTo(
"@dagger.internal.codegen.KeyFactoryTest.TestQualifier({"
+ "@dagger.internal.codegen.KeyFactoryTest.InnerAnnotation("
Expand Down Expand Up @@ -199,20 +180,10 @@ static final class QualifiedFieldHolder {

@Test
public void forProvidesMethod_sets() {
XTypeElement setElement = processingEnv.requireTypeElement(Set.class.getCanonicalName());
XType stringType = processingEnv.requireType(String.class.getCanonicalName());
XType setOfStringsType = processingEnv.getDeclaredType(setElement, stringType);
XTypeElement moduleElement =
processingEnv.requireTypeElement(SetProvidesMethodsModule.class.getCanonicalName());
for (XMethodElement providesMethod : moduleElement.getDeclaredMethods()) {
Key key = keyFactory.forProvidesMethod(providesMethod, moduleElement);
assertThat(key)
.isEqualTo(
Key.builder(DaggerType.from(setOfStringsType))
.multibindingContributionIdentifier(
DaggerTypeElement.from(moduleElement),
DaggerExecutableElement.from(providesMethod))
.build());
assertThat(key.toString())
.isEqualTo(
String.format(
Expand Down Expand Up @@ -269,12 +240,10 @@ static final class BoxedPrimitiveTypes {
}

@Test public void forProducesMethod() {
XType stringType = processingEnv.requireType(String.class.getCanonicalName());
XTypeElement moduleElement =
processingEnv.requireTypeElement(ProducesMethodsModule.class.getCanonicalName());
for (XMethodElement producesMethod : moduleElement.getDeclaredMethods()) {
Key key = keyFactory.forProducesMethod(producesMethod, moduleElement);
assertThat(key).isEqualTo(Key.builder(DaggerType.from(stringType)).build());
assertThat(key.toString()).isEqualTo("java.lang.String");
}
}
Expand All @@ -291,20 +260,10 @@ static final class ProducesMethodsModule {
}

@Test public void forProducesMethod_sets() {
XTypeElement setElement = processingEnv.requireTypeElement(Set.class.getCanonicalName());
XType stringType = processingEnv.requireType(String.class.getCanonicalName());
XType setOfStringsType = processingEnv.getDeclaredType(setElement, stringType);
XTypeElement moduleElement =
processingEnv.requireTypeElement(SetProducesMethodsModule.class.getCanonicalName());
for (XMethodElement producesMethod : moduleElement.getDeclaredMethods()) {
Key key = keyFactory.forProducesMethod(producesMethod, moduleElement);
assertThat(key)
.isEqualTo(
Key.builder(DaggerType.from(setOfStringsType))
.multibindingContributionIdentifier(
DaggerTypeElement.from(moduleElement),
DaggerExecutableElement.from(producesMethod))
.build());
assertThat(key.toString())
.isEqualTo(
String.format(
Expand Down

0 comments on commit 02176dc

Please sign in to comment.