Skip to content

Commit 65fe716

Browse files
bcorsoDagger Team
authored andcommitted
[XPoet Migration]: Replace TypeName usages in AutoValue with Equivalence.Wrapper<XType>.
As part of the XPoet migration, I've been replacing `TypeName` with `XTypeName`. However, that doesn't apply to cases where we currently rely on the equals/hashcode of `TypeName`, e.g. AutoValue classes. In these cases, I'm replacing `TypeName` with an `Equivalence.Wrapper<XType>` that has the same equals/hashcode as `TypeName`. This change should be a noop. RELNOTES=N/A PiperOrigin-RevId: 750346029
1 parent e55205e commit 65fe716

4 files changed

Lines changed: 25 additions & 26 deletions

File tree

dagger-compiler/main/java/dagger/internal/codegen/binding/AssistedInjectionAnnotations.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
import androidx.room.compiler.processing.XVariableElement;
3737
import com.google.auto.value.AutoValue;
3838
import com.google.auto.value.extension.memoized.Memoized;
39+
import com.google.common.base.Equivalence;
3940
import com.google.common.collect.ImmutableList;
4041
import com.google.common.collect.ImmutableMap;
4142
import com.google.common.collect.ImmutableSet;
42-
import com.squareup.javapoet.TypeName;
4343
import dagger.assisted.Assisted;
4444
import dagger.assisted.AssistedFactory;
4545
import dagger.internal.codegen.model.BindingKind;
@@ -222,24 +222,22 @@ public static AssistedParameter create(
222222
Optional.ofNullable(parameter.getAnnotation(XTypeNames.ASSISTED))
223223
.map(assisted -> assisted.getAsString("value"))
224224
.orElse(""),
225-
parameterType.getTypeName());
225+
XTypes.equivalence().wrap(parameterType));
226226
assistedParameter.parameterElement = parameter;
227-
assistedParameter.parameterType = parameterType;
228227
return assistedParameter;
229228
}
230229

231230
private XExecutableParameterElement parameterElement;
232-
private XType parameterType;
233231

234232
/** Returns the string qualifier from the {@link Assisted#value()}. */
235233
public abstract String qualifier();
236234

237-
/** Returns the type annotated with {@link Assisted}. */
238-
abstract TypeName typeName();
235+
/** Returns the equivalence wrapper for the type annotated with {@link Assisted}. */
236+
abstract Equivalence.Wrapper<XType> wrappedType();
239237

240238
/** Returns the type annotated with {@link Assisted}. */
241239
public final XType type() {
242-
return parameterType;
240+
return wrappedType().get();
243241
}
244242

245243
public final XExecutableParameterElement element() {

dagger-compiler/main/java/dagger/internal/codegen/binding/ComponentRequirement.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
import androidx.room.compiler.processing.XType;
3131
import androidx.room.compiler.processing.XTypeElement;
3232
import com.google.auto.value.AutoValue;
33-
import com.squareup.javapoet.TypeName;
33+
import com.google.common.base.Equivalence;
3434
import dagger.internal.codegen.model.BindingKind;
3535
import dagger.internal.codegen.model.Key;
3636
import dagger.internal.codegen.xprocessing.Nullability;
3737
import dagger.internal.codegen.xprocessing.XParameterSpecs;
3838
import dagger.internal.codegen.xprocessing.XTypeElements;
3939
import dagger.internal.codegen.xprocessing.XTypeNames;
40+
import dagger.internal.codegen.xprocessing.XTypes;
4041
import java.util.Optional;
4142

4243
/** A type that a component needs an instance of. */
@@ -65,8 +66,6 @@ public boolean isModule() {
6566
}
6667
}
6768

68-
private XType type;
69-
7069
/** The kind of requirement. */
7170
public abstract Kind kind();
7271

@@ -76,17 +75,17 @@ final boolean isBoundInstance() {
7675
return kind().isBoundInstance();
7776
}
7877

79-
/** The type of the instance the component must have. */
80-
abstract TypeName typeName();
78+
/** Returns the equivalence wrapper for the component requirement type. */
79+
abstract Equivalence.Wrapper<XType> wrappedType();
8180

8281
/** The type of the instance the component must have. */
8382
public XType type() {
84-
return type;
83+
return wrappedType().get();
8584
}
8685

8786
/** The element associated with the type of this requirement. */
8887
public XTypeElement typeElement() {
89-
return type.getTypeElement();
88+
return type().getTypeElement();
9089
}
9190

9291
/** The action a component builder should take if it {@code null} is passed. */
@@ -241,9 +240,8 @@ private static ComponentRequirement create(
241240
String variableName) {
242241
ComponentRequirement requirement =
243242
new AutoValue_ComponentRequirement(
244-
kind, type.getTypeName(), overrideNullPolicy, key, variableName);
243+
kind, XTypes.equivalence().wrap(type), overrideNullPolicy, key, variableName);
245244
requirement.nullability = nullability;
246-
requirement.type = type;
247245
return requirement;
248246
}
249247

dagger-compiler/main/java/dagger/internal/codegen/binding/MethodSignature.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@
2323
import androidx.room.compiler.processing.XProcessingEnv;
2424
import androidx.room.compiler.processing.XType;
2525
import com.google.auto.value.AutoValue;
26+
import com.google.common.base.Equivalence;
2627
import com.google.common.collect.ImmutableList;
27-
import com.squareup.javapoet.TypeName;
2828
import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor;
29+
import dagger.internal.codegen.xprocessing.XTypes;
2930

3031
/** A class that defines proper {@code equals} and {@code hashcode} for a method signature. */
3132
@AutoValue
3233
public abstract class MethodSignature {
3334

3435
abstract String name();
3536

36-
abstract ImmutableList<TypeName> parameterTypes();
37+
abstract ImmutableList<Equivalence.Wrapper<XType>> parameterTypes();
3738

38-
abstract ImmutableList<TypeName> thrownTypes();
39+
abstract ImmutableList<Equivalence.Wrapper<XType>> thrownTypes();
3940

4041
public static MethodSignature forComponentMethod(
4142
ComponentMethodDescriptor componentMethod,
@@ -44,12 +45,14 @@ public static MethodSignature forComponentMethod(
4445
XMethodType methodType = componentMethod.methodElement().asMemberOf(componentType);
4546
return new AutoValue_MethodSignature(
4647
getSimpleName(componentMethod.methodElement()),
47-
methodType.getParameterTypes().stream().map(XType::getTypeName).collect(toImmutableList()),
48+
methodType.getParameterTypes().stream()
49+
.map(XTypes.equivalence()::wrap)
50+
.collect(toImmutableList()),
4851
// Using the thrown types of the method element, which should be the same as the method type
4952
// since thrown types can't use type variables.
5053
// TODO(bcorso): Support getting thrown types from XExecutableType in XProcessing.
5154
componentMethod.methodElement().getThrownTypes().stream()
52-
.map(XType::getTypeName)
55+
.map(XTypes.equivalence()::wrap)
5356
.collect(toImmutableList()));
5457
}
5558
}

dagger-compiler/main/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
import androidx.room.compiler.processing.XMethodType;
4747
import androidx.room.compiler.processing.XType;
4848
import androidx.room.compiler.processing.XTypeElement;
49+
import com.google.common.base.Equivalence;
4950
import com.google.common.collect.ImmutableSet;
5051
import com.google.common.collect.ImmutableSetMultimap;
5152
import com.google.common.collect.Multimaps;
5253
import com.google.common.collect.Sets;
53-
import com.squareup.javapoet.TypeName;
5454
import dagger.internal.codegen.base.DaggerSuperficialValidation;
5555
import dagger.internal.codegen.binding.ComponentCreatorDescriptor;
5656
import dagger.internal.codegen.binding.ComponentDescriptor;
@@ -338,19 +338,19 @@ private void validateCreators(ComponentDescriptor component) {
338338
}
339339

340340
// Validate that declared creator requirements (modules, dependencies) have unique types.
341-
ImmutableSetMultimap<TypeName, XElement> declaredRequirementsByType =
341+
ImmutableSetMultimap<Equivalence.Wrapper<XType>, XElement> declaredRequirementsByType =
342342
Multimaps.filterKeys(
343343
creator.unvalidatedRequirementElements(),
344344
creatorModuleAndDependencyRequirements::contains)
345345
.entries()
346346
.stream()
347347
.collect(
348348
toImmutableSetMultimap(
349-
entry -> entry.getKey().type().getTypeName(), Entry::getValue));
349+
entry -> XTypes.equivalence().wrap(entry.getKey().type()), Entry::getValue));
350350
declaredRequirementsByType
351351
.asMap()
352352
.forEach(
353-
(type, elementsForType) -> {
353+
(wrappedType, elementsForType) -> {
354354
if (elementsForType.size() > 1) {
355355
// TODO(cgdecker): Attach this error message to the factory method rather than
356356
// the component type if the elements are factory method parameters AND the
@@ -359,7 +359,7 @@ private void validateCreators(ComponentDescriptor component) {
359359
.addError(
360360
String.format(
361361
messages.multipleSettersForModuleOrDependencyType(),
362-
type,
362+
XTypes.toStableString(wrappedType.get()),
363363
transform(
364364
elementsForType, element -> formatElement(element, container))),
365365
creator.typeElement());

0 commit comments

Comments
 (0)