From 07d8f883f1f1e1fe6787228b05741d8dd8633bd4 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Thu, 19 Dec 2024 17:42:12 -0800 Subject: [PATCH] Remove private from generated Factory's INSTANCE field. Fixes https://github.com/google/dagger/issues/4544 Background: Dagger's generated factory has an optimization to lazy load a singleton instance of the factory when there are no dependencies: ``` public final class Foo_Factory implements Factory { public static Foo_Factory create() { return InstanceHolder.INSTANCE; } private static final class InstanceHolder { private static final Foo_Factory INSTANCE = new Foo_Factory(); } } ``` The `private` modifier on the `INSTANCE` field is not needed and can add extra overhead as described in https://github.com/google/dagger/issues/4544. Removing the `private` modifier is also needed once we start generating Kotlin sources since accessing a `private` field on a nested class in kotlin source is a kotlinc error. RELNOTES=Fixes #4544: Removes private from InstanceHolder field. PiperOrigin-RevId: 708101532 --- .../codegen/base/SourceFileHjarGenerator.java | 17 +++++++++++------ .../codegen/writing/FactoryGenerator.java | 4 ++-- ...MODE_test.TestModule_PrimitiveIntegerFactory | 2 +- ...MODE_test.TestModule_PrimitiveIntegerFactory | 2 +- ...ODE_test.TestModule_NonNullableStringFactory | 2 +- ...ODE_test.TestModule_NonNullableStringFactory | 2 +- ...WithNoDependencies_test.GenericClass_Factory | 2 +- ...GeneratorTest_noDeps_test.SimpleType_Factory | 2 +- ...omponentWithNesting_test.OuterType_A_Factory | 2 +- ...taWithCustomScope_test.ScopedBinding_Factory | 2 +- ...estScopedMetadata_test.ScopedBinding_Factory | 2 +- ...eterizedModule_ProvideMapStringNumberFactory | 2 +- ...meterizedModule_ProvideNonGenericTypeFactory | 2 +- ...FactoryMethods_test.TestModule_CreateFactory | 2 +- ...herFactoryMethods_test.TestModule_GetFactory | 2 +- ...stomScope_test.MyModule_ProvideStringFactory | 2 +- ...cProvides_test.MyModule_ProvideStringFactory | 2 +- 17 files changed, 28 insertions(+), 23 deletions(-) diff --git a/java/dagger/internal/codegen/base/SourceFileHjarGenerator.java b/java/dagger/internal/codegen/base/SourceFileHjarGenerator.java index 6857c366fa5..8e93a615a50 100644 --- a/java/dagger/internal/codegen/base/SourceFileHjarGenerator.java +++ b/java/dagger/internal/codegen/base/SourceFileHjarGenerator.java @@ -201,11 +201,16 @@ private static CodeBlock getDefaultValueCodeBlock(TypeName typeName) { } private FieldSpec skeletonField(FieldSpec completeField) { - return FieldSpec.builder( - completeField.type, - completeField.name, - completeField.modifiers.toArray(new Modifier[0])) - .addAnnotations(completeField.annotations) - .build(); + FieldSpec.Builder skeleton = + FieldSpec.builder( + completeField.type, + completeField.name, + completeField.modifiers.toArray(new Modifier[0])) + .addAnnotations(completeField.annotations); + if (completeField.modifiers.contains(Modifier.FINAL)) { + // Final fields must be initialized so use the default value. + skeleton.initializer(getDefaultValueCodeBlock(completeField.type)); + } + return skeleton.build(); } } diff --git a/java/dagger/internal/codegen/writing/FactoryGenerator.java b/java/dagger/internal/codegen/writing/FactoryGenerator.java index a2c2b4fec4f..818283a42d3 100644 --- a/java/dagger/internal/codegen/writing/FactoryGenerator.java +++ b/java/dagger/internal/codegen/writing/FactoryGenerator.java @@ -159,13 +159,13 @@ private TypeSpec.Builder factoryBuilder(ContributionBinding binding) { } // private static final class InstanceHolder { - // private static final FooModule_ProvidesFooFactory INSTANCE = + // static final FooModule_ProvidesFooFactory INSTANCE = // new FooModule_ProvidesFooFactory(); // } private TypeSpec staticInstanceHolderType(ContributionBinding binding) { ClassName generatedClassName = generatedClassNameForBinding(binding); FieldSpec.Builder instanceHolderFieldBuilder = - FieldSpec.builder(generatedClassName, "INSTANCE", PRIVATE, STATIC, FINAL) + FieldSpec.builder(generatedClassName, "INSTANCE", STATIC, FINAL) .initializer("new $T()", generatedClassName); if (!bindingTypeElementTypeVariableNames(binding).isEmpty()) { // If the factory has type parameters, ignore them in the field declaration & initializer diff --git a/javatests/dagger/internal/codegen/goldens/ComponentProcessorTest_nullCheckingIgnoredWhenProviderReturnsPrimitive_DEFAULT_MODE_test.TestModule_PrimitiveIntegerFactory b/javatests/dagger/internal/codegen/goldens/ComponentProcessorTest_nullCheckingIgnoredWhenProviderReturnsPrimitive_DEFAULT_MODE_test.TestModule_PrimitiveIntegerFactory index 81629f31a31..501fd25bb6a 100644 --- a/javatests/dagger/internal/codegen/goldens/ComponentProcessorTest_nullCheckingIgnoredWhenProviderReturnsPrimitive_DEFAULT_MODE_test.TestModule_PrimitiveIntegerFactory +++ b/javatests/dagger/internal/codegen/goldens/ComponentProcessorTest_nullCheckingIgnoredWhenProviderReturnsPrimitive_DEFAULT_MODE_test.TestModule_PrimitiveIntegerFactory @@ -37,7 +37,7 @@ public final class TestModule_PrimitiveIntegerFactory implements Factory implements Factory> { private static final class InstanceHolder { @SuppressWarnings("rawtypes") - private static final GenericClass_Factory INSTANCE = new GenericClass_Factory(); + static final GenericClass_Factory INSTANCE = new GenericClass_Factory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_noDeps_test.SimpleType_Factory b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_noDeps_test.SimpleType_Factory index 580554d2d55..c8d172b52cf 100644 --- a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_noDeps_test.SimpleType_Factory +++ b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_noDeps_test.SimpleType_Factory @@ -37,7 +37,7 @@ public final class SimpleType_Factory implements Factory { } private static final class InstanceHolder { - private static final SimpleType_Factory INSTANCE = new SimpleType_Factory(); + static final SimpleType_Factory INSTANCE = new SimpleType_Factory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_simpleComponentWithNesting_test.OuterType_A_Factory b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_simpleComponentWithNesting_test.OuterType_A_Factory index 4b8675fb43b..b0c7a56cbff 100644 --- a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_simpleComponentWithNesting_test.OuterType_A_Factory +++ b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_simpleComponentWithNesting_test.OuterType_A_Factory @@ -37,7 +37,7 @@ public final class OuterType_A_Factory implements Factory { } private static final class InstanceHolder { - private static final OuterType_A_Factory INSTANCE = new OuterType_A_Factory(); + static final OuterType_A_Factory INSTANCE = new OuterType_A_Factory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_testScopedMetadataWithCustomScope_test.ScopedBinding_Factory b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_testScopedMetadataWithCustomScope_test.ScopedBinding_Factory index 0ea59518414..d0312324040 100644 --- a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_testScopedMetadataWithCustomScope_test.ScopedBinding_Factory +++ b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_testScopedMetadataWithCustomScope_test.ScopedBinding_Factory @@ -37,7 +37,7 @@ public final class ScopedBinding_Factory implements Factory { } private static final class InstanceHolder { - private static final ScopedBinding_Factory INSTANCE = new ScopedBinding_Factory(); + static final ScopedBinding_Factory INSTANCE = new ScopedBinding_Factory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_testScopedMetadata_test.ScopedBinding_Factory b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_testScopedMetadata_test.ScopedBinding_Factory index 88894ca2da7..3368c1a23a7 100644 --- a/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_testScopedMetadata_test.ScopedBinding_Factory +++ b/javatests/dagger/internal/codegen/goldens/InjectConstructorFactoryGeneratorTest_testScopedMetadata_test.ScopedBinding_Factory @@ -37,7 +37,7 @@ public final class ScopedBinding_Factory implements Factory { } private static final class InstanceHolder { - private static final ScopedBinding_Factory INSTANCE = new ScopedBinding_Factory(); + static final ScopedBinding_Factory INSTANCE = new ScopedBinding_Factory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_parameterizedModuleWithStaticProvidesMethodOfGenericType_test.ParameterizedModule_ProvideMapStringNumberFactory b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_parameterizedModuleWithStaticProvidesMethodOfGenericType_test.ParameterizedModule_ProvideMapStringNumberFactory index bd4fe27bce3..e57b3a27feb 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_parameterizedModuleWithStaticProvidesMethodOfGenericType_test.ParameterizedModule_ProvideMapStringNumberFactory +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_parameterizedModuleWithStaticProvidesMethodOfGenericType_test.ParameterizedModule_ProvideMapStringNumberFactory @@ -39,7 +39,7 @@ public final class ParameterizedModule_ProvideMapStringNumberFactory implements } private static final class InstanceHolder { - private static final ParameterizedModule_ProvideMapStringNumberFactory INSTANCE = new ParameterizedModule_ProvideMapStringNumberFactory(); + static final ParameterizedModule_ProvideMapStringNumberFactory INSTANCE = new ParameterizedModule_ProvideMapStringNumberFactory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_parameterizedModuleWithStaticProvidesMethodOfGenericType_test.ParameterizedModule_ProvideNonGenericTypeFactory b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_parameterizedModuleWithStaticProvidesMethodOfGenericType_test.ParameterizedModule_ProvideNonGenericTypeFactory index 109054902dc..4c8f2aeab31 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_parameterizedModuleWithStaticProvidesMethodOfGenericType_test.ParameterizedModule_ProvideNonGenericTypeFactory +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_parameterizedModuleWithStaticProvidesMethodOfGenericType_test.ParameterizedModule_ProvideNonGenericTypeFactory @@ -38,7 +38,7 @@ public final class ParameterizedModule_ProvideNonGenericTypeFactory implements F } private static final class InstanceHolder { - private static final ParameterizedModule_ProvideNonGenericTypeFactory INSTANCE = new ParameterizedModule_ProvideNonGenericTypeFactory(); + static final ParameterizedModule_ProvideNonGenericTypeFactory INSTANCE = new ParameterizedModule_ProvideNonGenericTypeFactory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_proxyMethodsConflictWithOtherFactoryMethods_test.TestModule_CreateFactory b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_proxyMethodsConflictWithOtherFactoryMethods_test.TestModule_CreateFactory index 2d1b5d1f7c1..a21af1c8676 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_proxyMethodsConflictWithOtherFactoryMethods_test.TestModule_CreateFactory +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_proxyMethodsConflictWithOtherFactoryMethods_test.TestModule_CreateFactory @@ -37,7 +37,7 @@ public final class TestModule_CreateFactory implements Factory { } private static final class InstanceHolder { - private static final TestModule_CreateFactory INSTANCE = new TestModule_CreateFactory(); + static final TestModule_CreateFactory INSTANCE = new TestModule_CreateFactory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_proxyMethodsConflictWithOtherFactoryMethods_test.TestModule_GetFactory b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_proxyMethodsConflictWithOtherFactoryMethods_test.TestModule_GetFactory index 1662eacaedb..244868aadac 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_proxyMethodsConflictWithOtherFactoryMethods_test.TestModule_GetFactory +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_proxyMethodsConflictWithOtherFactoryMethods_test.TestModule_GetFactory @@ -37,7 +37,7 @@ public final class TestModule_GetFactory implements Factory { } private static final class InstanceHolder { - private static final TestModule_GetFactory INSTANCE = new TestModule_GetFactory(); + static final TestModule_GetFactory INSTANCE = new TestModule_GetFactory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_testScopeMetadataWithCustomScope_test.MyModule_ProvideStringFactory b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_testScopeMetadataWithCustomScope_test.MyModule_ProvideStringFactory index f262b78f689..689e523ac46 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_testScopeMetadataWithCustomScope_test.MyModule_ProvideStringFactory +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_testScopeMetadataWithCustomScope_test.MyModule_ProvideStringFactory @@ -38,7 +38,7 @@ public final class MyModule_ProvideStringFactory implements Factory { } private static final class InstanceHolder { - private static final MyModule_ProvideStringFactory INSTANCE = new MyModule_ProvideStringFactory(); + static final MyModule_ProvideStringFactory INSTANCE = new MyModule_ProvideStringFactory(); } } diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_testScopedMetadataOnStaticProvides_test.MyModule_ProvideStringFactory b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_testScopedMetadataOnStaticProvides_test.MyModule_ProvideStringFactory index b839d37b2aa..cf4287890ef 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_testScopedMetadataOnStaticProvides_test.MyModule_ProvideStringFactory +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_testScopedMetadataOnStaticProvides_test.MyModule_ProvideStringFactory @@ -38,7 +38,7 @@ public final class MyModule_ProvideStringFactory implements Factory { } private static final class InstanceHolder { - private static final MyModule_ProvideStringFactory INSTANCE = new MyModule_ProvideStringFactory(); + static final MyModule_ProvideStringFactory INSTANCE = new MyModule_ProvideStringFactory(); } }