Skip to content

Commit

Permalink
Migrate FrameworkType and FrameworkField to XPoet.
Browse files Browse the repository at this point in the history
This CL:

  * Migrates `FrameworkField` to XPoet
  * Migrates `FrameworkType` to XPoet
  * Adds `XTypeNames` -- a version of `TypeNames` compatible with XPoet.
  * Adds legacy `toJavaPoet()` calls where needed. These will be cleaned up in future CLs as we migrate the corresponding usages to use XPoet.

#dagger-xpoet-migration

RELNOTES=N/A
PiperOrigin-RevId: 707570301
  • Loading branch information
bcorso authored and Dagger Team committed Dec 18, 2024
1 parent 972c8d3 commit 6ee6092
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 41 deletions.
20 changes: 10 additions & 10 deletions java/dagger/internal/codegen/binding/FrameworkField.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
import static androidx.room.compiler.processing.XElementKt.isMethod;
import static androidx.room.compiler.processing.XElementKt.isMethodParameter;
import static androidx.room.compiler.processing.XElementKt.isTypeElement;
import static com.google.common.collect.Iterables.getLast;
import static dagger.internal.codegen.model.BindingKind.MEMBERS_INJECTOR;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;

import androidx.room.compiler.codegen.XClassName;
import androidx.room.compiler.codegen.XTypeName;
import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XType;
import com.google.auto.value.AutoValue;
import com.google.common.base.CaseFormat;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import dagger.internal.codegen.base.MapType;
import java.util.Optional;

Expand All @@ -55,7 +55,7 @@ public abstract class FrameworkField {
* @param frameworkClassName the framework class that wraps the type (e.g., {@code Provider}).
* @param type the base type of the field (e.g., {@code Foo}).
*/
public static FrameworkField create(String fieldName, ClassName frameworkClassName, XType type) {
public static FrameworkField create(String fieldName, XClassName frameworkClassName, XType type) {
return createInternal(fieldName, frameworkClassName, Optional.of(type));
}

Expand All @@ -66,7 +66,7 @@ public static FrameworkField create(String fieldName, ClassName frameworkClassNa
* one for the binding's type.
*/
public static FrameworkField forBinding(
ContributionBinding binding, Optional<ClassName> frameworkClassName) {
ContributionBinding binding, Optional<XClassName> frameworkClassName) {
return createInternal(
bindingName(binding),
frameworkClassName.orElse(binding.frameworkType().frameworkClassName()),
Expand Down Expand Up @@ -99,17 +99,17 @@ private static Optional<XType> bindingType(ContributionBinding binding) {
}

private static FrameworkField createInternal(
String fieldName, ClassName frameworkClassName, Optional<XType> type) {
String fieldName, XClassName frameworkClassName, Optional<XType> type) {
return new AutoValue_FrameworkField(
frameworkFieldName(fieldName, frameworkClassName),
type.isPresent()
? ParameterizedTypeName.get(frameworkClassName, type.get().getTypeName())
? frameworkClassName.parametrizedBy(type.get().asTypeName())
// Use a raw framework classname, e.g. Provider
: frameworkClassName);
}

private static String frameworkFieldName(String fieldName, ClassName frameworkClassName) {
String suffix = frameworkClassName.simpleName();
private static String frameworkFieldName(String fieldName, XClassName frameworkClassName) {
String suffix = getLast(frameworkClassName.getSimpleNames());
return fieldName.endsWith(suffix) ? fieldName : fieldName + suffix;
}

Expand All @@ -129,5 +129,5 @@ private static String bindingElementName(XElement bindingElement) {

public abstract String name();

public abstract TypeName type();
public abstract XTypeName type();
}
12 changes: 7 additions & 5 deletions java/dagger/internal/codegen/binding/FrameworkType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package dagger.internal.codegen.binding;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;

import androidx.room.compiler.codegen.XClassName;
import androidx.room.compiler.processing.XProcessingEnv;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
Expand All @@ -29,6 +30,7 @@
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.model.DependencyRequest;
import dagger.internal.codegen.model.RequestKind;
import dagger.internal.codegen.xprocessing.XTypeNames;
import java.util.Optional;

/** One of the core types initialized as fields in a generated component. */
Expand Down Expand Up @@ -176,22 +178,22 @@ public static Optional<FrameworkType> forRequestKind(RequestKind requestKind) {
}

/** The class of fields of this type. */
public ClassName frameworkClassName() {
public XClassName frameworkClassName() {
switch (this) {
case PROVIDER:
return TypeNames.DAGGER_PROVIDER;
return XTypeNames.DAGGER_PROVIDER;
case PRODUCER_NODE:
// TODO(cgdecker): Replace this with new class for representing internal producer nodes.
// Currently the new class is CancellableProducer, but it may be changed to ProducerNode and
// made to not implement Producer.
return TypeNames.PRODUCER;
return XTypeNames.PRODUCER;
}
throw new AssertionError("Unknown value: " + this.name());
}

/** Returns the {@link #frameworkClassName()} parameterized with a type. */
public ParameterizedTypeName frameworkClassOf(TypeName valueType) {
return ParameterizedTypeName.get(frameworkClassName(), valueType);
return ParameterizedTypeName.get(toJavaPoet(frameworkClassName()), valueType);
}

/** The request kind that an instance of this framework type can satisfy directly, if any. */
Expand Down
8 changes: 5 additions & 3 deletions java/dagger/internal/codegen/binding/SourceFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import static dagger.internal.codegen.xprocessing.XTypeElements.typeVariableNames;
import static javax.lang.model.SourceVersion.isName;

import androidx.room.compiler.codegen.XClassName;
import androidx.room.compiler.processing.XExecutableElement;
import androidx.room.compiler.processing.XFieldElement;
import androidx.room.compiler.processing.XMethodElement;
Expand All @@ -64,6 +65,7 @@
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.model.DependencyRequest;
import dagger.internal.codegen.model.RequestKind;
import dagger.internal.codegen.xprocessing.XTypeNames;
import javax.inject.Inject;
import javax.lang.model.SourceVersion;

Expand Down Expand Up @@ -96,15 +98,15 @@ public final class SourceFiles {
return Maps.toMap(
binding.dependencies(),
dependency -> {
ClassName frameworkClassName =
XClassName frameworkClassName =
frameworkTypeMapper.getFrameworkType(dependency.kind()).frameworkClassName();
// Remap factory fields back to javax.inject.Provider to maintain backwards compatibility
// for now. In a future release, we should change this to Dagger Provider. This will still
// be a breaking change, but keeping compatibility for a while should reduce the
// likelihood of breakages as it would require components built at much older versions
// using factories built at newer versions to break.
if (frameworkClassName.equals(TypeNames.DAGGER_PROVIDER)) {
frameworkClassName = TypeNames.PROVIDER;
if (frameworkClassName.equals(XTypeNames.DAGGER_PROVIDER)) {
frameworkClassName = XTypeNames.PROVIDER;
}
return FrameworkField.create(
DependencyVariableNamer.name(dependency),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.writing;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.binding.BindingRequest.bindingRequest;
Expand Down Expand Up @@ -58,7 +59,7 @@ public CodeBlock creationExpression() {
bindingRequest(dependency.key(), binding.frameworkType()),
componentImplementation.shardImplementation(binding).name())
.codeBlock(),
binding.frameworkType().frameworkClassName());
toJavaPoet(binding.frameworkType().frameworkClassName()));
}

@AssistedFactory
Expand Down
4 changes: 3 additions & 1 deletion java/dagger/internal/codegen/writing/FactoryGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.writing;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.Preconditions.checkArgument;
import static com.squareup.javapoet.MethodSpec.constructorBuilder;
import static com.squareup.javapoet.MethodSpec.methodBuilder;
Expand Down Expand Up @@ -494,7 +495,8 @@ static FactoryFields create(ContributionBinding binding) {
generateBindingFieldsForDependencies(binding).forEach(
(dependency, field) ->
frameworkFields.put(
dependency, createField(field.type(), nameSet.getUniqueName(field.name()))));
dependency,
createField(toJavaPoet(field.type()), nameSet.getUniqueName(field.name()))));

return new FactoryFields(moduleField, frameworkFields.buildOrThrow());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

package dagger.internal.codegen.writing;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.Preconditions.checkNotNull;
import static dagger.internal.codegen.binding.SourceFiles.generatedClassNameForBinding;
import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.RAWTYPES;
import static dagger.internal.codegen.writing.ComponentImplementation.FieldSpecKind.FRAMEWORK_FIELD;
import static javax.lang.model.element.Modifier.PRIVATE;

import androidx.room.compiler.codegen.XClassName;
import androidx.room.compiler.processing.XType;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
Expand All @@ -36,6 +38,7 @@
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.model.BindingKind;
import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation;
import dagger.internal.codegen.xprocessing.XTypeNames;
import java.util.Optional;

/**
Expand All @@ -56,7 +59,7 @@ interface FrameworkInstanceCreationExpression {
* Returns the framework class to use for the field, if different from the one implied by the
* binding. This implementation returns {@link Optional#empty()}.
*/
default Optional<ClassName> alternativeFrameworkClass() {
default Optional<XClassName> alternativeFrameworkClass() {
return Optional.empty();
}
}
Expand Down Expand Up @@ -143,8 +146,8 @@ private FieldSpec getOrCreateField() {
binding, frameworkInstanceCreationExpression.alternativeFrameworkClass());

TypeName fieldType = useRawType
? TypeNames.rawTypeName(contributionBindingField.type())
: contributionBindingField.type();
? toJavaPoet(contributionBindingField.type().getRawTypeName())
: toJavaPoet(contributionBindingField.type());

if (binding.kind() == BindingKind.ASSISTED_INJECTION) {
// An assisted injection factory doesn't extend Provider, so we reference the generated
Expand Down Expand Up @@ -181,7 +184,7 @@ private boolean isProvider() {
return binding.bindingType().equals(BindingType.PROVISION)
&& frameworkInstanceCreationExpression
.alternativeFrameworkClass()
.map(TypeNames.PROVIDER::equals)
.map(XTypeNames.PROVIDER::equals)
.orElse(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.writing;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.Preconditions.checkNotNull;
import static dagger.internal.codegen.langmodel.Accessibility.isTypeAccessibleFrom;
import static dagger.internal.codegen.xprocessing.XProcessingEnvs.wrapType;
Expand Down Expand Up @@ -51,7 +52,10 @@ abstract class FrameworkInstanceRequestRepresentation extends RequestRepresentat
Expression getDependencyExpression(ClassName requestingClass) {
MemberSelect memberSelect = frameworkInstanceSupplier.memberSelect();
XType expressionType =
wrapType(frameworkType().frameworkClassName(), binding.contributedType(), processingEnv);
wrapType(
toJavaPoet(frameworkType().frameworkClassName()),
binding.contributedType(),
processingEnv);
return Expression.create(
isTypeAccessibleFrom(binding.contributedType(), requestingClass.packageName())
|| isInlinedFactoryCreation(memberSelect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.writing;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.squareup.javapoet.MethodSpec.constructorBuilder;
Expand All @@ -34,7 +35,6 @@
import static dagger.internal.codegen.javapoet.CodeBlocks.parameterNames;
import static dagger.internal.codegen.javapoet.CodeBlocks.toConcatenatedCodeBlock;
import static dagger.internal.codegen.javapoet.TypeNames.membersInjectorOf;
import static dagger.internal.codegen.javapoet.TypeNames.rawTypeName;
import static dagger.internal.codegen.langmodel.Accessibility.isRawTypePubliclyAccessible;
import static dagger.internal.codegen.langmodel.Accessibility.isTypeAccessibleFrom;
import static dagger.internal.codegen.writing.GwtCompatibility.gwtIncompatibleAnnotation;
Expand Down Expand Up @@ -340,7 +340,9 @@ private static ImmutableMap<DependencyRequest, FieldSpec> frameworkFields(
request.key().type().xprocessing(),
membersInjectorTypeName.packageName());
TypeName fieldType =
useRawFrameworkType ? rawTypeName(bindingField.type()) : bindingField.type();
useRawFrameworkType
? toJavaPoet(bindingField.type().getRawTypeName())
: toJavaPoet(bindingField.type());
String fieldName = fieldNames.getUniqueName(bindingField.name());
FieldSpec field =
useRawFrameworkType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.writing;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.Preconditions.checkNotNull;

import com.squareup.javapoet.CodeBlock;
Expand Down Expand Up @@ -52,7 +53,7 @@ protected final CodeBlock multibindingDependencyExpression(DependencyRequest dep
.codeBlock();

return useRawType()
? CodeBlocks.cast(expression, binding.frameworkType().frameworkClassName())
? CodeBlocks.cast(expression, toJavaPoet(binding.frameworkType().frameworkClassName()))
: expression;
}

Expand Down
3 changes: 2 additions & 1 deletion java/dagger/internal/codegen/writing/OptionalFactories.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.writing;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
import static com.google.common.base.Verify.verify;
Expand Down Expand Up @@ -267,7 +268,7 @@ String factoryClassName() {
return new StringBuilder("Present")
.append(UPPER_UNDERSCORE.to(UPPER_CAMEL, optionalKind().name()))
.append(UPPER_UNDERSCORE.to(UPPER_CAMEL, valueKind().toString()))
.append(frameworkType().frameworkClassName().simpleName())
.append(toJavaPoet(frameworkType().frameworkClassName()).simpleName())
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.writing;

import static androidx.room.compiler.codegen.XTypeNameKt.toJavaPoet;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.squareup.javapoet.ClassName.OBJECT;
Expand Down Expand Up @@ -425,7 +426,8 @@ static FactoryFields create(ProductionBinding binding) {
generateBindingFieldsForDependencies(binding).forEach(
(dependency, field) ->
builder.put(
dependency, createField(field.type(), nameSet.getUniqueName(field.name()))));
dependency,
createField(toJavaPoet(field.type()), nameSet.getUniqueName(field.name()))));
return new FactoryFields(binding, moduleField, builder.buildOrThrow());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
package dagger.internal.codegen.writing;


import androidx.room.compiler.codegen.XClassName;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedFactory;
import dagger.assisted.AssistedInject;
import dagger.internal.codegen.binding.FrameworkType;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.model.RequestKind;
import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression;
import dagger.internal.codegen.xprocessing.XTypeNames;
import java.util.Optional;

/** An {@code Producer} creation expression for provision bindings. */
Expand All @@ -49,8 +50,8 @@ public CodeBlock creationExpression() {
}

@Override
public Optional<ClassName> alternativeFrameworkClass() {
return Optional.of(TypeNames.PRODUCER);
public Optional<XClassName> alternativeFrameworkClass() {
return Optional.of(XTypeNames.PRODUCER);
}

@AssistedFactory
Expand Down
Loading

0 comments on commit 6ee6092

Please sign in to comment.