Skip to content

Commit 5885a1e

Browse files
bcorsoDagger Team
authored andcommitted
[XPoet Migration]: Remove workaround to control @OVERRIDES ordering.
This CL removes the workaround that lets us control the ordering of the `@Overrides` annotation and updates the golden files. This workaround was initially used just to avoid causing changes to our golden files while the migration was in progress, but it's being removed now that the migration is nearing completion. RELNOTES=N/A PiperOrigin-RevId: 752395970
1 parent cb4cb4c commit 5885a1e

62 files changed

Lines changed: 85 additions & 135 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dagger-compiler/main/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private XFunSpec noopSetterMethod(ComponentRequirement requirement) {
250250
XFunSpecs.Builder builder = setterMethodBuilder(requirement);
251251
XParameterSpec parameter = getOnlyElement(builder.getParameters());
252252
builder
253-
.addAnnotation(Deprecated.class)
253+
.addAnnotation(XTypeNames.DEPRECATED)
254254
.addJavadoc(
255255
"@deprecated This module is declared, but an instance is not used in the component. "
256256
+ "This method is a no-op. For more, see https://dagger.dev/unused-modules.\n")

dagger-compiler/main/java/dagger/internal/codegen/writing/ComponentImplementation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,8 @@ private void addConstructorAndInitializationMethods() {
10421042
private void addCancellationListenerImplementation() {
10431043
XFunSpecs.Builder methodBuilder =
10441044
methodBuilder(CANCELLATION_LISTENER_METHOD_NAME)
1045+
.isOverride(true)
10451046
.addModifiers(PUBLIC)
1046-
.addAnnotation(Override.class)
10471047
.addParameter(MAY_INTERRUPT_IF_RUNNING_PARAM);
10481048

10491049
// Reversing should order cancellations starting from entry points and going down to leaves

dagger-compiler/main/java/dagger/internal/codegen/writing/DependencyMethodProviderCreationExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public XCodeBlock creationExpression() {
104104
compilerOptions);
105105
XFunSpecs.Builder getMethod =
106106
methodBuilder("get")
107-
.addAnnotation(Override.class)
107+
.isOverride(true)
108108
.addModifiers(PUBLIC)
109109
.returns(returnType)
110110
.addStatement("return %L", invocation)

dagger-compiler/main/java/dagger/internal/codegen/writing/FactoryGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ private XFunSpec getMethod(ContributionBinding binding, FactoryFields factoryFie
279279
parameter.getType().asTypeName())));
280280
XTypeName providedTypeName = providedTypeName(binding);
281281
XFunSpecs.Builder getMethod =
282-
methodBuilder("get").addModifiers(PUBLIC).addParameters(assistedParameters.values());
282+
methodBuilder("get")
283+
.addModifiers(PUBLIC)
284+
.isOverride(factoryTypeName(binding).isPresent())
285+
.addParameters(assistedParameters.values());
283286

284-
if (factoryTypeName(binding).isPresent()) {
285-
getMethod.addAnnotation(Override.class);
286-
}
287287
XCodeBlock invokeNewInstance =
288288
ProvisionMethod.invoke(
289289
binding,

dagger-compiler/main/java/dagger/internal/codegen/writing/MembersInjectorGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ private XFunSpec injectMembersMethod(
315315
dependencyCodeBlocks::get);
316316
return methodBuilder("injectMembers")
317317
.addModifiers(PUBLIC)
318-
.addAnnotation(Override.class)
318+
.isOverride(true)
319319
.addParameter("instance", instanceType.asTypeName())
320320
.addCode(invokeInjectionSites)
321321
.build();

dagger-compiler/main/java/dagger/internal/codegen/writing/ProducerFactoryGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private XFunSpec staticCreateMethod(ProductionBinding binding, FactoryFields fac
222222
public XFunSpec collectDependenciesMethod(
223223
ProductionBinding binding, FactoryFields factoryFields) {
224224
XFunSpecs.Builder methodBuilder =
225-
methodBuilder("collectDependencies").addAnnotation(Override.class).addModifiers(PROTECTED);
225+
methodBuilder("collectDependencies").isOverride(true).addModifiers(PROTECTED);
226226
ImmutableList<DependencyRequest> asyncDependencies = asyncDependencies(binding);
227227
switch (asyncDependencies.size()) {
228228
case 0:
@@ -292,7 +292,7 @@ private XFunSpec callProducesMethod(ProductionBinding binding, FactoryFields fac
292292
XFunSpecs.Builder methodBuilder =
293293
methodBuilder("callProducesMethod")
294294
.returns(listenableFutureOf(contributedTypeName))
295-
.addAnnotation(Override.class)
295+
.isOverride(true)
296296
.addModifiers(PUBLIC)
297297
.addExceptions(asMethod(binding.bindingElement().get()).getThrownTypes())
298298
.addParameter(parameter);

dagger-compiler/main/java/dagger/internal/codegen/writing/SwitchingProviders.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ private ImmutableList<XFunSpec> getMethods() {
201201
// The case amount does not exceed MAX_CASES_PER_SWITCH, so no need for extra get methods.
202202
return ImmutableList.of(
203203
methodBuilder("get")
204+
.isOverride(true)
204205
.addModifiers(PUBLIC)
205206
.addAnnotation(suppressWarnings(UNCHECKED))
206-
.addAnnotation(Override.class)
207207
.returns(typeVariable)
208208
.addCode(getOnlyElement(switchCodeBlockPartitions))
209209
.build());
@@ -212,8 +212,8 @@ private ImmutableList<XFunSpec> getMethods() {
212212
// This is the main public "get" method that will route to private getter methods.
213213
XFunSpecs.Builder routerMethod =
214214
methodBuilder("get")
215+
.isOverride(true)
215216
.addModifiers(PUBLIC)
216-
.addAnnotation(Override.class)
217217
.returns(typeVariable)
218218
.beginControlFlow("switch (id / %L)", MAX_CASES_PER_SWITCH);
219219

dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XFunSpecs.java

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import androidx.room.compiler.processing.XType;
3939
import com.google.common.collect.ImmutableList;
4040
import com.google.errorprone.annotations.CanIgnoreReturnValue;
41-
import com.squareup.javapoet.AnnotationSpec;
42-
import com.squareup.javapoet.ClassName;
4341
import com.squareup.javapoet.TypeName;
4442
import com.squareup.javapoet.TypeVariableName;
4543
import com.squareup.kotlinpoet.KModifier;
@@ -75,7 +73,7 @@ public static Builder overridingWithoutParameters(
7573
Builder builder =
7674
// We're overriding the method so we have to use the jvm name here.
7775
methodBuilder(method.getJvmName())
78-
.addAnnotation(Override.class)
76+
.isOverride(true)
7977
.addAnnotationNames(nullability.nonTypeUseNullableAnnotations())
8078
.addTypeVariables(methodType.getTypeVariables())
8179
.varargs(method.isVarArgs())
@@ -115,8 +113,8 @@ private static enum Kind {
115113
private XTypeName returnType = null;
116114
private final List<XCodeBlock> javadocs = new ArrayList<>();
117115
private final List<XParameterSpec> parameters = new ArrayList<>();
116+
private final List<XAnnotationSpec> annotations = new ArrayList<>();
118117
// For now, we use a Object to allow for both XPoet and JavaPoet types.
119-
private final List<Object> annotations = new ArrayList<>();
120118
private final List<Object> typeVariableNames = new ArrayList<>();
121119
private final List<Object> exceptionNames = new ArrayList<>();
122120

@@ -280,18 +278,6 @@ public Builder addAnnotations(Collection<XAnnotationSpec> annotations) {
280278
return this;
281279
}
282280

283-
/**
284-
* Adds the given annotations to the method.
285-
*
286-
* @deprecated Use {@link #addAnnotation(XAnnotationSpec)} instead.
287-
*/
288-
@Deprecated
289-
@CanIgnoreReturnValue
290-
public Builder addJavaAnnotations(Collection<AnnotationSpec> annotations) {
291-
annotations.forEach(this::addAnnotation);
292-
return this;
293-
}
294-
295281
/** Adds the given annotation names to the method. */
296282
@CanIgnoreReturnValue
297283
public Builder addAnnotationNames(Collection<XClassName> annotationNames) {
@@ -305,42 +291,15 @@ public Builder addAnnotation(XAnnotation annotation) {
305291
return addAnnotation(XAnnotationSpecs.of(annotation));
306292
}
307293

308-
/** Adds the given annotation to the method. */
309-
@CanIgnoreReturnValue
310-
public Builder addAnnotation(XAnnotationSpec annotation) {
311-
annotations.add(annotation);
312-
return this;
313-
}
314-
315294
/** Adds the given annotation name to the method. */
316295
@CanIgnoreReturnValue
317296
public Builder addAnnotation(XClassName annotationName) {
318297
return addAnnotation(XAnnotationSpec.of(annotationName));
319298
}
320299

321-
/**
322-
* Adds the given annotation to the method.
323-
*
324-
* @deprecated Use {@link #addAnnotation(XClassName)} instead.
325-
*/
326-
@Deprecated
327-
@CanIgnoreReturnValue
328-
public Builder addAnnotation(Class<?> clazz) {
329-
if (clazz.equals(Override.class)) {
330-
isOverride(true);
331-
}
332-
addAnnotation(AnnotationSpec.builder(ClassName.get(clazz)).build());
333-
return this;
334-
}
335-
336-
/**
337-
* Adds the given annotation to the method.
338-
*
339-
* @deprecated Use {@link #addAnnotation(XAnnotationSpec)} instead.
340-
*/
341-
@Deprecated
300+
/** Adds the given annotation to the method. */
342301
@CanIgnoreReturnValue
343-
public Builder addAnnotation(AnnotationSpec annotation) {
302+
public Builder addAnnotation(XAnnotationSpec annotation) {
344303
annotations.add(annotation);
345304
return this;
346305
}
@@ -505,12 +464,6 @@ public XFunSpec build() {
505464
isOpen,
506465
isOverride,
507466
/* addJavaNullabilityAnnotation= */ false);
508-
// If the override annotation exists, remove it from the builder and it will be added
509-
// with the other annotations to avoid unnecessary diffs during the migration.
510-
AnnotationSpec overrideAnnotation = AnnotationSpec.builder(Override.class).build();
511-
if (annotations.contains(overrideAnnotation)) {
512-
toJavaPoet(builder).annotations.remove(overrideAnnotation);
513-
}
514467
break;
515468
case CONSTRUCTOR:
516469
checkState(name == null);
@@ -570,15 +523,7 @@ public XFunSpec build() {
570523
}
571524
}
572525

573-
for (Object annotation : annotations) {
574-
if (annotation instanceof XAnnotationSpec) {
575-
builder.addAnnotation((XAnnotationSpec) annotation);
576-
} else if (annotation instanceof AnnotationSpec) {
577-
toJavaPoet(builder).addAnnotation((AnnotationSpec) annotation);
578-
} else {
579-
throw new AssertionError("Unexpected annotation class: " + annotation.getClass());
580-
}
581-
}
526+
annotations.forEach(builder::addAnnotation);
582527

583528
for (Object exceptionName : exceptionNames) {
584529
if (exceptionName instanceof XTypeName) {

dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeNames.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616

1717
package dagger.internal.codegen.xprocessing
1818

19-
import androidx.room.compiler.codegen.XAnnotationSpec
2019
import androidx.room.compiler.codegen.XClassName
2120
import androidx.room.compiler.codegen.XTypeName
2221
import androidx.room.compiler.codegen.box
23-
import androidx.room.compiler.codegen.compat.XConverters.toJavaPoet
24-
import androidx.room.compiler.codegen.compat.XConverters.toKotlinPoet
2522
import androidx.room.compiler.codegen.compat.XConverters.toXPoet
2623
import androidx.room.compiler.processing.XType
2724
import com.squareup.javapoet.ClassName
@@ -99,6 +96,7 @@ object XTypeNames {
9996
@JvmField val SET_BUILDER = XClassName.get("dagger.internal", "SetBuilder")
10097
@JvmField val SET_FACTORY = XClassName.get("dagger.internal", "SetFactory")
10198
@JvmField val SINGLE_CHECK = XClassName.get("dagger.internal", "SingleCheck")
99+
@JvmField val WEAK = XClassName.get("dagger.internal", "Weak")
102100
@JvmField val LAZY = XClassName.get("dagger", "Lazy")
103101

104102
// Dagger Producers classnames
@@ -169,6 +167,13 @@ object XTypeNames {
169167
@JvmField val RUNTIME_EXCEPTION = XClassName.get("java.lang", "RuntimeException")
170168
@JvmField
171169
val UNSUPPORTED_OPERATION_EXCEPTION = XClassName.get("java.lang", "UnsupportedOperationException")
170+
@JvmField
171+
val DEPRECATED = toXPoet(
172+
ClassName.get("java.lang", "Deprecated"),
173+
com.squareup.kotlinpoet.ClassName("kotlin", "Deprecated")
174+
)
175+
@JvmField val CAN_IGNORE_RETURN_VALUE =
176+
XClassName.get("com.google.errorprone.annotations", "CanIgnoreReturnValue")
172177

173178
@JvmField val KOTLIN_METADATA = XClassName.get("kotlin", "Metadata")
174179
@JvmField val IMMUTABLE_MAP = XClassName.get("com.google.common.collect", "ImmutableMap")

javatests/dagger/internal/codegen/goldens/AssistedFactoryTest_assistedParamConflictsWithComponentFieldName_successfulyDeduped_FAST_INIT_MODE_test.DaggerTestComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ final class DaggerTestComponent {
7474
this.id = id;
7575
}
7676

77-
@SuppressWarnings("unchecked")
7877
@Override
78+
@SuppressWarnings("unchecked")
7979
public T get() {
8080
switch (id) {
8181
case 0: // test.FooFactory

0 commit comments

Comments
 (0)