Skip to content

Commit 3507b0d

Browse files
java-team-github-botDagger Team
authored andcommitted
Support nullability in Switching Providers
RELNOTES=n/a PiperOrigin-RevId: 866132936
1 parent 1900560 commit 3507b0d

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import static javax.lang.model.element.Modifier.PUBLIC;
3333
import static javax.lang.model.element.Modifier.STATIC;
3434

35-
import androidx.room3.compiler.codegen.XAnnotationSpec;
36-
import androidx.room3.compiler.codegen.XClassName;
3735
import androidx.room3.compiler.codegen.XCodeBlock;
3836
import androidx.room3.compiler.codegen.XFunSpec;
3937
import androidx.room3.compiler.codegen.XParameterSpec;
@@ -65,8 +63,6 @@
6563

6664
/** Factory for creating {@link ComponentCreatorImplementation} instances. */
6765
final class ComponentCreatorImplementationFactory {
68-
private static final XAnnotationSpec JSPECIFY_NULLABLE =
69-
XAnnotationSpec.of(XClassName.get("org.jspecify.annotations", "Nullable"));
7066

7167
private final CompilerOptions compilerOptions;
7268
private final ComponentImplementation componentImplementation;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.collect.Iterables.getLast;
2121
import static com.google.common.collect.Iterables.getOnlyElement;
2222
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
23+
import static dagger.internal.codegen.xprocessing.NullableTypeNames.appendTypeUseNullable;
2324
import static dagger.internal.codegen.xprocessing.XAnnotationSpecs.Suppression.UNCHECKED;
2425
import static dagger.internal.codegen.xprocessing.XAnnotationSpecs.suppressWarnings;
2526
import static dagger.internal.codegen.xprocessing.XCodeBlocks.concat;
@@ -32,6 +33,7 @@
3233
import static javax.lang.model.element.Modifier.PUBLIC;
3334
import static javax.lang.model.element.Modifier.STATIC;
3435

36+
import androidx.room3.compiler.codegen.XAnnotationSpec;
3537
import androidx.room3.compiler.codegen.XClassName;
3638
import androidx.room3.compiler.codegen.XCodeBlock;
3739
import androidx.room3.compiler.codegen.XFunSpec;
@@ -75,7 +77,6 @@ final class SwitchingProviders {
7577
private static final int MAX_CASES_PER_SWITCH = 100;
7678

7779
private static final long MAX_CASES_PER_CLASS = MAX_CASES_PER_SWITCH * MAX_CASES_PER_SWITCH;
78-
private static final XTypeName typeVariable = XTypeNames.getTypeVariableName("T");
7980

8081
/**
8182
* Maps a {@link Key} to an instance of a {@link SwitchingProviderBuilder}. Each group of {@code
@@ -87,6 +88,7 @@ final class SwitchingProviders {
8788
private final ShardImplementation shardImplementation;
8889
private final CompilerOptions compilerOptions;
8990
private final XProcessingEnv processingEnv;
91+
private final XTypeName typeVariable;
9092

9193
SwitchingProviders(
9294
ShardImplementation shardImplementation,
@@ -95,6 +97,14 @@ final class SwitchingProviders {
9597
this.shardImplementation = checkNotNull(shardImplementation);
9698
this.compilerOptions = checkNotNull(compilerOptions);
9799
this.processingEnv = checkNotNull(processingEnv);
100+
this.typeVariable =
101+
XTypeName.getTypeVariableName(
102+
"T",
103+
ImmutableList.of(
104+
appendTypeUseNullable(
105+
XTypeName.ANY_OBJECT,
106+
XAnnotationSpec.of(XTypeNames.JSPECIFY_NULLABLE),
107+
compilerOptions)));
98108
}
99109

100110
/** Returns the framework instance creation expression for an inner switching provider class. */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ object XTypeNames {
178178
@JvmField val PRODUCTION_SCOPE = XClassName.get("dagger.producers", "ProductionScope")
179179

180180
// Other classnames
181+
@JvmField val JSPECIFY_NULLABLE = XClassName.get("org.jspecify.annotations", "Nullable")
181182
@JvmField val EXECUTOR = XClassName.get("java.util.concurrent", "Executor")
182183
@JvmField val ASSERTION_ERROR = XClassName.get("java.lang", "AssertionError")
183184
@JvmField val ERROR = XClassName.get("java.lang", "Error")

javatests/dagger/internal/codegen/goldens/TypeUseNullableTest/componentDependencyWithTypeUseNullableType/test.DaggerBComponent_FAST_INIT_MODE_typeUseNullables_ENABLED

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ final class DaggerBComponent {
6767
return new B(nullableStringProvider);
6868
}
6969

70-
private static final class SwitchingProvider<T> implements Provider<T> {
70+
private static final class SwitchingProvider<T extends @org.jspecify.annotations.Nullable Object> implements Provider<T> {
7171
private final BComponentImpl bComponentImpl;
7272

7373
private final int id;

0 commit comments

Comments
 (0)