Skip to content

Commit 013a9a4

Browse files
java-team-github-botDagger Team
authored and
Dagger Team
committed
Add Nullmarking to Components
RELNOTES=Expand Nullmarking PiperOrigin-RevId: 708266599
1 parent 07d8f88 commit 013a9a4

10 files changed

+86
-13
lines changed

java/dagger/internal/codegen/binding/ComponentCreatorDescriptor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import dagger.internal.codegen.base.ComponentCreatorKind;
4242
import dagger.internal.codegen.javapoet.TypeNames;
4343
import dagger.internal.codegen.model.DependencyRequest;
44+
import dagger.internal.codegen.xprocessing.Nullability;
4445
import dagger.internal.codegen.xprocessing.XElements;
4546
import java.util.List;
4647

@@ -217,7 +218,10 @@ private static ComponentRequirement requirement(
217218
DependencyRequest request =
218219
dependencyRequestFactory.forRequiredResolvedVariable(parameter, parameterType);
219220
return ComponentRequirement.forBoundInstance(
220-
request.key(), request.isNullable(), elementForVariableName);
221+
request.key(),
222+
request.isNullable(),
223+
elementForVariableName,
224+
Nullability.of(elementForVariableName));
221225
}
222226

223227
return parameterType.getTypeElement().hasAnyAnnotation(moduleAnnotations())

java/dagger/internal/codegen/binding/ComponentRequirement.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import dagger.internal.codegen.javapoet.TypeNames;
3535
import dagger.internal.codegen.model.BindingKind;
3636
import dagger.internal.codegen.model.Key;
37+
import dagger.internal.codegen.xprocessing.Nullability;
3738
import dagger.internal.codegen.xprocessing.XTypeElements;
3839
import java.util.Optional;
3940

@@ -106,6 +107,16 @@ public enum NullPolicy {
106107
*/
107108
abstract Optional<NullPolicy> overrideNullPolicy();
108109

110+
/**
111+
* The nullability of the requirement. If set, this is used to determine the nullability of the
112+
* requirement's type.
113+
*/
114+
public Nullability getNullability() {
115+
return nullability;
116+
}
117+
118+
private Nullability nullability = Nullability.NOT_NULLABLE;
119+
109120
/** The requirement's null policy. */
110121
public NullPolicy nullPolicy() {
111122
if (overrideNullPolicy().isPresent()) {
@@ -195,25 +206,28 @@ public static ComponentRequirement forModule(XType type) {
195206

196207
public static ComponentRequirement forBoundInstance(BoundInstanceBinding binding) {
197208
checkArgument(binding.kind().equals(BindingKind.BOUND_INSTANCE));
198-
return forBoundInstance(binding.key(), binding.isNullable(), binding.bindingElement().get());
209+
return forBoundInstance(
210+
binding.key(), binding.isNullable(), binding.bindingElement().get(), binding.nullability());
199211
}
200212

201213
static ComponentRequirement forBoundInstance(
202-
Key key, boolean nullable, XElement elementForVariableName) {
214+
Key key, boolean nullable, XElement elementForVariableName, Nullability nullability) {
203215
return create(
204216
Kind.BOUND_INSTANCE,
205217
key.type().xprocessing(),
206218
nullable ? Optional.of(NullPolicy.ALLOW) : Optional.empty(),
207219
Optional.of(key),
220+
nullability,
208221
getSimpleName(elementForVariableName));
209222
}
210223

211224
private static ComponentRequirement create(Kind kind, XType type) {
212225
return create(
213226
kind,
214227
type,
215-
Optional.empty(),
216-
Optional.empty(),
228+
/* overrideNullPolicy= */ Optional.empty(),
229+
/* key= */ Optional.empty(),
230+
Nullability.NOT_NULLABLE,
217231
simpleVariableName(type.getTypeElement().getClassName()));
218232
}
219233

@@ -222,10 +236,12 @@ private static ComponentRequirement create(
222236
XType type,
223237
Optional<NullPolicy> overrideNullPolicy,
224238
Optional<Key> key,
239+
Nullability nullability,
225240
String variableName) {
226241
ComponentRequirement requirement =
227242
new AutoValue_ComponentRequirement(
228243
kind, type.getTypeName(), overrideNullPolicy, key, variableName);
244+
requirement.nullability = nullability;
229245
requirement.type = type;
230246
return requirement;
231247
}

java/dagger/internal/codegen/writing/ComponentImplementation.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.google.common.collect.ListMultimap;
6060
import com.google.common.collect.Lists;
6161
import com.google.common.collect.MultimapBuilder;
62+
import com.squareup.javapoet.AnnotationSpec;
6263
import com.squareup.javapoet.ClassName;
6364
import com.squareup.javapoet.CodeBlock;
6465
import com.squareup.javapoet.FieldSpec;
@@ -491,8 +492,26 @@ private ShardImplementation(ClassName name) {
491492
requirement -> requirement,
492493
requirement ->
493494
ParameterSpec.builder(
494-
requirement.type().getTypeName(),
495+
requirement
496+
.type()
497+
.getTypeName()
498+
.annotated(
499+
requirement
500+
.getNullability()
501+
.typeUseNullableAnnotations()
502+
.stream()
503+
.map(AnnotationSpec::builder)
504+
.map(AnnotationSpec.Builder::build)
505+
.collect(toImmutableList())),
495506
getUniqueFieldName(requirement.variableName() + "Param"))
507+
.addAnnotations(
508+
requirement
509+
.getNullability()
510+
.nonTypeUseNullableAnnotations()
511+
.stream()
512+
.map(AnnotationSpec::builder)
513+
.map(AnnotationSpec.Builder::build)
514+
.collect(toImmutableList()))
496515
.build()));
497516
}
498517

java/dagger/internal/codegen/writing/ComponentRequirementExpressions.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.common.base.Suppliers.memoize;
22+
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
2223
import static dagger.internal.codegen.writing.ComponentImplementation.FieldSpecKind.COMPONENT_REQUIREMENT_FIELD;
2324
import static javax.lang.model.element.Modifier.FINAL;
2425
import static javax.lang.model.element.Modifier.PRIVATE;
2526

2627
import androidx.room.compiler.processing.XTypeElement;
2728
import com.google.common.base.Supplier;
29+
import com.squareup.javapoet.AnnotationSpec;
2830
import com.squareup.javapoet.ClassName;
2931
import com.squareup.javapoet.CodeBlock;
3032
import com.squareup.javapoet.FieldSpec;
@@ -129,7 +131,22 @@ public CodeBlock getExpression(ClassName requestingClass) {
129131
private MemberSelect createField() {
130132
String fieldName = componentShard.getUniqueFieldName(componentRequirement.variableName());
131133
TypeName fieldType = componentRequirement.type().getTypeName();
132-
FieldSpec field = FieldSpec.builder(fieldType, fieldName, PRIVATE, FINAL).build();
134+
FieldSpec field =
135+
FieldSpec.builder(
136+
fieldType.annotated(
137+
componentRequirement.getNullability().typeUseNullableAnnotations().stream()
138+
.map(AnnotationSpec::builder)
139+
.map(AnnotationSpec.Builder::build)
140+
.collect(toImmutableList())),
141+
fieldName,
142+
PRIVATE,
143+
FINAL)
144+
.addAnnotations(
145+
componentRequirement.getNullability().nonTypeUseNullableAnnotations().stream()
146+
.map(AnnotationSpec::builder)
147+
.map(AnnotationSpec.Builder::build)
148+
.collect(toImmutableList()))
149+
.build();
133150
componentShard.addField(COMPONENT_REQUIREMENT_FIELD, field);
134151
componentShard.addComponentRequirementInitialization(fieldInitialization(field));
135152
return MemberSelect.localField(componentShard, fieldName);

java/dagger/internal/codegen/xprocessing/MethodSpecs.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package dagger.internal.codegen.xprocessing;
1818

19+
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
1920
import static dagger.internal.codegen.xprocessing.JavaPoetExt.toParameterSpec;
2021
import static javax.lang.model.element.Modifier.PROTECTED;
2122
import static javax.lang.model.element.Modifier.PUBLIC;
@@ -24,6 +25,7 @@
2425
import androidx.room.compiler.processing.XMethodElement;
2526
import androidx.room.compiler.processing.XMethodType;
2627
import androidx.room.compiler.processing.XType;
28+
import com.squareup.javapoet.AnnotationSpec;
2729
import com.squareup.javapoet.MethodSpec;
2830

2931
// TODO(bcorso): Consider moving these methods into XProcessing library.
@@ -33,13 +35,27 @@ public final class MethodSpecs {
3335
/** Returns a {@link MethodSpec} that overrides the given method. */
3436
public static MethodSpec.Builder overriding(XMethodElement method, XType owner) {
3537
XMethodType methodType = method.asMemberOf(owner);
38+
Nullability nullability = Nullability.of(method);
3639
MethodSpec.Builder builder =
3740
// We're overriding the method so we have to use the jvm name here.
3841
MethodSpec.methodBuilder(method.getJvmName())
3942
.addAnnotation(Override.class)
43+
.addAnnotations(
44+
nullability.nonTypeUseNullableAnnotations().stream()
45+
.map(AnnotationSpec::builder)
46+
.map(AnnotationSpec.Builder::build)
47+
.collect(toImmutableList()))
4048
.addTypeVariables(methodType.getTypeVariableNames())
4149
.varargs(method.isVarArgs())
42-
.returns(methodType.getReturnType().getTypeName());
50+
.returns(
51+
methodType
52+
.getReturnType()
53+
.getTypeName()
54+
.annotated(
55+
nullability.typeUseNullableAnnotations().stream()
56+
.map(AnnotationSpec::builder)
57+
.map(AnnotationSpec.Builder::build)
58+
.collect(toImmutableList())));
4359
if (method.isPublic()) {
4460
builder.addModifiers(PUBLIC);
4561
} else if (method.isProtected()) {

javatests/dagger/functional/factory/FactoryBindsInstanceTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package dagger.functional.factory;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static java.lang.annotation.ElementType.FIELD;
2021
import static java.lang.annotation.ElementType.METHOD;
2122
import static java.lang.annotation.ElementType.PARAMETER;
2223
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@@ -60,7 +61,7 @@ public void nonNullableBindsInstance_failsOnNull() {
6061
}
6162
}
6263

63-
@Target({METHOD, PARAMETER})
64+
@Target({METHOD, PARAMETER, FIELD})
6465
@Retention(RUNTIME)
6566
@interface Nullable {}
6667

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class DaggerTestComponent {
3535
private static final class TestComponentImpl implements TestComponent {
3636
private final TestComponentImpl testComponentImpl = this;
3737

38-
private TestComponentImpl(Bar argParam) {
38+
private TestComponentImpl(@Nullable Bar argParam) {
3939

4040

4141
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class DaggerTestComponent {
3535
private static final class TestComponentImpl implements TestComponent {
3636
private final TestComponentImpl testComponentImpl = this;
3737

38-
private TestComponentImpl(Bar argParam) {
38+
private TestComponentImpl(@Nullable Bar argParam) {
3939

4040

4141
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class DaggerTestComponent {
3535
private static final class TestComponentImpl implements TestComponent {
3636
private final TestComponentImpl testComponentImpl = this;
3737

38-
private TestComponentImpl(Bar argParam) {
38+
private TestComponentImpl(@Nullable Bar argParam) {
3939

4040

4141
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class DaggerTestComponent {
3535
private static final class TestComponentImpl implements TestComponent {
3636
private final TestComponentImpl testComponentImpl = this;
3737

38-
private TestComponentImpl(Bar argParam) {
38+
private TestComponentImpl(@Nullable Bar argParam) {
3939

4040

4141
}

0 commit comments

Comments
 (0)