Skip to content

Commit f40c43b

Browse files
bcorsoDagger Team
authored andcommitted
Internal changes
RELNOTES=N/A PiperOrigin-RevId: 752370724
1 parent 837dbaa commit f40c43b

47 files changed

Lines changed: 944 additions & 182 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/binding/FrameworkField.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static androidx.room.compiler.processing.XElementKt.isTypeElement;
2323
import static com.google.common.collect.Iterables.getLast;
2424
import static dagger.internal.codegen.model.BindingKind.MEMBERS_INJECTOR;
25+
import static dagger.internal.codegen.xprocessing.NullableTypeNames.asNullableTypeName;
2526
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
2627

2728
import androidx.room.compiler.codegen.XClassName;
@@ -30,8 +31,8 @@
3031
import androidx.room.compiler.processing.XType;
3132
import com.google.common.base.CaseFormat;
3233
import dagger.internal.codegen.base.MapType;
34+
import dagger.internal.codegen.compileroption.CompilerOptions;
3335
import dagger.internal.codegen.xprocessing.Nullability;
34-
import dagger.internal.codegen.xprocessing.XTypeNames;
3536
import java.util.Optional;
3637

3738
/**
@@ -55,9 +56,17 @@ public final class FrameworkField {
5556
* @param frameworkClassName the framework class that wraps the type (e.g., {@code Provider}).
5657
* @param type the base type of the field (e.g., {@code Foo}).
5758
*/
58-
public static FrameworkField create(String fieldName, XClassName frameworkClassName, XType type) {
59+
public static FrameworkField create(
60+
String fieldName,
61+
XClassName frameworkClassName,
62+
XType type,
63+
CompilerOptions compilerOptions) {
5964
return createInternal(
60-
fieldName, frameworkClassName, Optional.of(type), Nullability.NOT_NULLABLE);
65+
fieldName,
66+
frameworkClassName,
67+
Optional.of(type),
68+
Nullability.NOT_NULLABLE,
69+
compilerOptions);
6170
}
6271

6372
/**
@@ -67,12 +76,15 @@ public static FrameworkField create(String fieldName, XClassName frameworkClassN
6776
* one for the binding's type.
6877
*/
6978
public static FrameworkField forBinding(
70-
ContributionBinding binding, Optional<XClassName> frameworkClassName) {
79+
ContributionBinding binding,
80+
Optional<XClassName> frameworkClassName,
81+
CompilerOptions compilerOptions) {
7182
return createInternal(
7283
bindingName(binding),
7384
frameworkClassName.orElse(binding.frameworkType().frameworkClassName()),
7485
bindingType(binding),
75-
binding.nullability());
86+
binding.nullability(),
87+
compilerOptions);
7688
}
7789

7890
private static String bindingName(ContributionBinding binding) {
@@ -104,11 +116,11 @@ private static FrameworkField createInternal(
104116
String fieldName,
105117
XClassName frameworkClassName,
106118
Optional<XType> type,
107-
Nullability nullability) {
108-
119+
Nullability nullability,
120+
CompilerOptions compilerOptions) {
109121
XTypeName fieldType =
110122
type.map(XType::asTypeName)
111-
.map(typeName -> XTypeNames.withTypeNullability(typeName, nullability))
123+
.map(typeName -> asNullableTypeName(typeName, nullability, compilerOptions))
112124
.map(frameworkClassName::parametrizedBy)
113125
.orElse(frameworkClassName);
114126

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import dagger.internal.codegen.base.MapType;
4848
import dagger.internal.codegen.base.SetType;
4949
import dagger.internal.codegen.binding.MembersInjectionBinding.InjectionSite;
50+
import dagger.internal.codegen.compileroption.CompilerOptions;
5051
import dagger.internal.codegen.model.DependencyRequest;
5152
import dagger.internal.codegen.model.RequestKind;
5253
import dagger.internal.codegen.xprocessing.XTypeNames;
@@ -73,7 +74,7 @@ public final class SourceFiles {
7374
* @param binding must be an unresolved binding (type parameters must match its type element's)
7475
*/
7576
public static ImmutableMap<DependencyRequest, FrameworkField>
76-
generateBindingFieldsForDependencies(Binding binding) {
77+
generateBindingFieldsForDependencies(Binding binding, CompilerOptions compilerOptions) {
7778
checkArgument(!binding.unresolved().isPresent(), "binding must be unresolved: %s", binding);
7879

7980
FrameworkTypeMapper frameworkTypeMapper =
@@ -87,7 +88,8 @@ public final class SourceFiles {
8788
return FrameworkField.create(
8889
DependencyVariableNamer.name(dependency),
8990
frameworkClassName,
90-
dependency.key().type().xprocessing());
91+
dependency.key().type().xprocessing(),
92+
compilerOptions);
9193
});
9294
}
9395

dagger-compiler/main/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ && componentRequirements(componentDescriptor)
167167
}
168168

169169
private XFunSpec emptyComponentMethod(XTypeElement typeElement, XMethodElement baseMethod) {
170-
return XFunSpecs.overriding(baseMethod, typeElement.getType()).build();
170+
return XFunSpecs.overriding(baseMethod, typeElement.getType(), compilerOptions).build();
171171
}
172172

173173
private static XFunSpec privateConstructor() {

dagger-compiler/main/java/dagger/internal/codegen/processingstep/AssistedFactoryProcessingStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public ImmutableList<XTypeSpec> topLevelTypes(AssistedFactoryBinding binding) {
306306
delegateFactoryParam.getName()) // SUPPRESS_GET_NAME_CHECK
307307
.build())
308308
.addFunction(
309-
overriding(metadata.factoryMethod(), metadata.factoryType())
309+
overriding(metadata.factoryMethod(), metadata.factoryType(), compilerOptions)
310310
.addStatement(
311311
"return %N.get(%L)",
312312
delegateFactoryParam.getName(), // SUPPRESS_GET_NAME_CHECK

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import dagger.internal.codegen.binding.AssistedInjectionBinding;
3737
import dagger.internal.codegen.binding.Binding;
3838
import dagger.internal.codegen.binding.BindingGraph;
39+
import dagger.internal.codegen.compileroption.CompilerOptions;
3940
import dagger.internal.codegen.xprocessing.XExpression;
4041
import dagger.internal.codegen.xprocessing.XTypeSpecs;
4142
import java.util.Optional;
@@ -49,17 +50,20 @@ final class AssistedFactoryRequestRepresentation extends RequestRepresentation {
4950
private final BindingGraph graph;
5051
private final SimpleMethodRequestRepresentation.Factory simpleMethodRequestRepresentationFactory;
5152
private final ComponentImplementation componentImplementation;
53+
private final CompilerOptions compilerOptions;
5254

5355
@AssistedInject
5456
AssistedFactoryRequestRepresentation(
5557
@Assisted AssistedFactoryBinding binding,
5658
BindingGraph graph,
5759
ComponentImplementation componentImplementation,
58-
SimpleMethodRequestRepresentation.Factory simpleMethodRequestRepresentationFactory) {
60+
SimpleMethodRequestRepresentation.Factory simpleMethodRequestRepresentationFactory,
61+
CompilerOptions compilerOptions) {
5962
this.binding = checkNotNull(binding);
6063
this.graph = graph;
6164
this.componentImplementation = componentImplementation;
6265
this.simpleMethodRequestRepresentationFactory = simpleMethodRequestRepresentationFactory;
66+
this.compilerOptions = compilerOptions;
6367
}
6468

6569
@Override
@@ -87,7 +91,7 @@ private XTypeSpec anonymousfactoryImpl(
8791
XTypeSpecs.Builder builder =
8892
XTypeSpecs.anonymousClassBuilder()
8993
.addFunction(
90-
overridingWithoutParameters(factoryMethod, factoryType)
94+
overridingWithoutParameters(factoryMethod, factoryType, compilerOptions)
9195
.addParameters(
9296
assistedFactoryParameterSpecs(
9397
binding, componentImplementation.shardImplementation(assistedBinding)))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ private XType creatorType() {
425425

426426
@Override
427427
protected XFunSpecs.Builder factoryMethodBuilder() {
428-
return overriding(creatorDescriptor.factoryMethod(), creatorType());
428+
return overriding(creatorDescriptor.factoryMethod(), creatorType(), compilerOptions);
429429
}
430430

431431
private RequirementStatus requirementStatus(ComponentRequirement requirement) {
@@ -457,7 +457,7 @@ private boolean isOwnedModule(ComponentRequirement requirement) {
457457
@Override
458458
protected XFunSpecs.Builder setterMethodBuilder(ComponentRequirement requirement) {
459459
XMethodElement supertypeMethod = creatorDescriptor.setterMethods().get(requirement);
460-
XFunSpecs.Builder method = overriding(supertypeMethod, creatorType());
460+
XFunSpecs.Builder method = overriding(supertypeMethod, creatorType(), compilerOptions);
461461
if (!isVoid(supertypeMethod.getReturnType())) {
462462
// Take advantage of covariant returns so that we don't have to worry about type variables
463463
method.returns(componentImplementation.getCreatorName());

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ private ShardImplementation(XClassName name) {
509509
XParameterSpecs.of(
510510
getUniqueFieldName(requirement.variableName() + "Param"),
511511
requirement.type().asTypeName(),
512-
requirement.getNullability())));
512+
requirement.getNullability(),
513+
compilerOptions)));
513514
}
514515

515516
private ShardImplementation createShard() {
@@ -881,7 +882,7 @@ private boolean canInstantiateAllRequirements() {
881882
private void createSubcomponentFactoryMethod(XMethodElement factoryMethod) {
882883
checkState(parent.isPresent());
883884
XType parentType = parent.get().graph().componentTypeElement().getType();
884-
XFunSpecs.Builder method = overriding(factoryMethod, parentType);
885+
XFunSpecs.Builder method = overriding(factoryMethod, parentType, compilerOptions);
885886
// Use the parameter names from the overriding method, which may be different from the
886887
// parameter names at the declaration site if it is pulled in as a class dependency from a
887888
// separate build unit (see https://github.com/google/dagger/issues/3401).

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import dagger.internal.codegen.binding.FrameworkType;
4545
import dagger.internal.codegen.binding.FrameworkTypeMapper;
4646
import dagger.internal.codegen.binding.MembersInjectionBinding;
47+
import dagger.internal.codegen.compileroption.CompilerOptions;
4748
import dagger.internal.codegen.model.DependencyRequest;
4849
import dagger.internal.codegen.model.RequestKind;
4950
import dagger.internal.codegen.xprocessing.XCodeBlocks;
@@ -70,6 +71,7 @@ public final class ComponentRequestRepresentations {
7071
private final ProductionBindingRepresentation.Factory productionBindingRepresentationFactory;
7172
private final Map<Binding, BindingRepresentation> representations = new HashMap<>();
7273
private final XProcessingEnv processingEnv;
74+
private final CompilerOptions compilerOptions;
7375

7476
@Inject
7577
ComponentRequestRepresentations(
@@ -80,7 +82,8 @@ public final class ComponentRequestRepresentations {
8082
MembersInjectionBindingRepresentation.Factory membersInjectionBindingRepresentationFactory,
8183
ProvisionBindingRepresentation.Factory provisionBindingRepresentationFactory,
8284
ProductionBindingRepresentation.Factory productionBindingRepresentationFactory,
83-
XProcessingEnv processingEnv) {
85+
XProcessingEnv processingEnv,
86+
CompilerOptions compilerOptions) {
8487
this.parent = parent;
8588
this.graph = graph;
8689
this.componentImplementation = componentImplementation;
@@ -90,6 +93,7 @@ public final class ComponentRequestRepresentations {
9093
this.productionBindingRepresentationFactory = productionBindingRepresentationFactory;
9194
this.componentRequirementExpressions = checkNotNull(componentRequirementExpressions);
9295
this.processingEnv = processingEnv;
96+
this.compilerOptions = compilerOptions;
9397
}
9498

9599
/**
@@ -183,7 +187,10 @@ && isRawTypeAccessible(dependencyType, requestingClass.getPackageName())) {
183187

184188
/** Returns the implementation of a component method. */
185189
public XFunSpec getComponentMethod(ComponentMethodDescriptor componentMethod) {
186-
return overriding(componentMethod.methodElement(), graph.componentTypeElement().getType())
190+
return overriding(
191+
componentMethod.methodElement(),
192+
graph.componentTypeElement().getType(),
193+
compilerOptions)
187194
.addCode(getComponentMethodCodeBlock(componentMethod))
188195
.build();
189196
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.common.base.Suppliers.memoize;
2222
import static dagger.internal.codegen.writing.ComponentImplementation.FieldSpecKind.COMPONENT_REQUIREMENT_FIELD;
23+
import static dagger.internal.codegen.xprocessing.NullableTypeNames.asNullableTypeName;
2324
import static javax.lang.model.element.Modifier.FINAL;
2425
import static javax.lang.model.element.Modifier.PRIVATE;
2526

@@ -31,6 +32,7 @@
3132
import com.google.common.base.Supplier;
3233
import dagger.internal.codegen.binding.BindingGraph;
3334
import dagger.internal.codegen.binding.ComponentRequirement;
35+
import dagger.internal.codegen.compileroption.CompilerOptions;
3436
import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation;
3537
import dagger.internal.codegen.xprocessing.Nullability;
3638
import dagger.internal.codegen.xprocessing.XPropertySpecs;
@@ -55,16 +57,19 @@ public final class ComponentRequirementExpressions {
5557
componentRequirementExpressions = new HashMap<>();
5658
private final BindingGraph graph;
5759
private final ShardImplementation componentShard;
60+
private final CompilerOptions compilerOptions;
5861

5962
@Inject
6063
ComponentRequirementExpressions(
6164
@ParentComponent Optional<ComponentRequirementExpressions> parent,
6265
BindingGraph graph,
63-
ComponentImplementation componentImplementation) {
66+
ComponentImplementation componentImplementation,
67+
CompilerOptions compilerOptions) {
6468
this.parent = parent;
6569
this.graph = graph;
6670
// All component requirements go in the componentShard.
6771
this.componentShard = componentImplementation.getComponentShard();
72+
this.compilerOptions = compilerOptions;
6873
}
6974

7075
/**
@@ -130,9 +135,14 @@ public XCodeBlock getExpression(XClassName requestingClass) {
130135

131136
private MemberSelect createField() {
132137
String fieldName = componentShard.getUniqueFieldName(componentRequirement.variableName());
133-
XTypeName fieldType = componentRequirement.type().asTypeName();
134138
Nullability nullability = componentRequirement.getNullability();
135-
XPropertySpec field = XPropertySpecs.of(fieldName, fieldType, nullability, PRIVATE, FINAL);
139+
XTypeName fieldType =
140+
asNullableTypeName(
141+
componentRequirement.type().asTypeName(), nullability, compilerOptions);
142+
XPropertySpec field =
143+
XPropertySpecs.builder(fieldName, fieldType, PRIVATE, FINAL)
144+
.addAnnotationNames(nullability.nonTypeUseNullableAnnotations())
145+
.build();
136146
componentShard.addField(COMPONENT_REQUIREMENT_FIELD, field);
137147
componentShard.addComponentRequirementInitialization(fieldInitialization(field));
138148
return MemberSelect.localField(componentShard, fieldName);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static com.google.common.base.Preconditions.checkArgument;
2424
import static com.google.common.base.Preconditions.checkNotNull;
2525
import static dagger.internal.codegen.writing.ComponentImplementation.TypeSpecKind.COMPONENT_PROVISION_FACTORY;
26+
import static dagger.internal.codegen.xprocessing.NullableTypeNames.asNullableTypeName;
2627
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
2728
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
2829
import static dagger.internal.codegen.xprocessing.XFunSpecs.constructorBuilder;
@@ -47,7 +48,6 @@
4748
import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation;
4849
import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression;
4950
import dagger.internal.codegen.xprocessing.XFunSpecs;
50-
import dagger.internal.codegen.xprocessing.XTypeNames;
5151
import dagger.internal.codegen.xprocessing.XTypeSpecs;
5252

5353
/**
@@ -97,12 +97,16 @@ public XCodeBlock creationExpression() {
9797
compilerOptions,
9898
XCodeBlock.of("%N.%N()", dependency().variableName(), provisionMethod.getJvmName()));
9999
XClassName dependencyClassName = dependency().typeElement().asClassName();
100-
XTypeName keyType = binding.key().type().xprocessing().asTypeName();
100+
XTypeName returnType =
101+
asNullableTypeName(
102+
binding.key().type().xprocessing().asTypeName(),
103+
binding.nullability(),
104+
compilerOptions);
101105
XFunSpecs.Builder getMethod =
102106
methodBuilder("get")
103107
.addAnnotation(Override.class)
104108
.addModifiers(PUBLIC)
105-
.returns(XTypeNames.withTypeNullability(keyType, binding.nullability()))
109+
.returns(returnType)
106110
.addStatement("return %L", invocation)
107111
.addAnnotationNames(binding.nullability().nonTypeUseNullableAnnotations());
108112

@@ -119,8 +123,7 @@ public XCodeBlock creationExpression() {
119123
componentShard.addType(
120124
COMPONENT_PROVISION_FACTORY,
121125
XTypeSpecs.classBuilder(factoryClassName)
122-
.addSuperinterface(
123-
daggerProviderOf(XTypeNames.withTypeNullability(keyType, binding.nullability())))
126+
.addSuperinterface(daggerProviderOf(returnType))
124127
.addModifiers(PRIVATE, STATIC, FINAL)
125128
.addField(toJavaPoet(dependencyClassName), dependency().variableName(), PRIVATE, FINAL)
126129
.addFunction(

0 commit comments

Comments
 (0)