Skip to content

Commit

Permalink
Remove private from generated Factory's INSTANCE field.
Browse files Browse the repository at this point in the history
Fixes #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<Foo> {
  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 #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
  • Loading branch information
bcorso authored and Dagger Team committed Dec 20, 2024
1 parent efa421a commit 07d8f88
Show file tree
Hide file tree
Showing 17 changed files with 28 additions and 23 deletions.
17 changes: 11 additions & 6 deletions java/dagger/internal/codegen/base/SourceFileHjarGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions java/dagger/internal/codegen/writing/FactoryGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class TestModule_PrimitiveIntegerFactory implements Factory<Integer
}

private static final class InstanceHolder {
private static final TestModule_PrimitiveIntegerFactory INSTANCE = new TestModule_PrimitiveIntegerFactory();
static final TestModule_PrimitiveIntegerFactory INSTANCE = new TestModule_PrimitiveIntegerFactory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class TestModule_PrimitiveIntegerFactory implements Factory<Integer
}

private static final class InstanceHolder {
private static final TestModule_PrimitiveIntegerFactory INSTANCE = new TestModule_PrimitiveIntegerFactory();
static final TestModule_PrimitiveIntegerFactory INSTANCE = new TestModule_PrimitiveIntegerFactory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class TestModule_NonNullableStringFactory implements Factory<String
}

private static final class InstanceHolder {
private static final TestModule_NonNullableStringFactory INSTANCE = new TestModule_NonNullableStringFactory();
static final TestModule_NonNullableStringFactory INSTANCE = new TestModule_NonNullableStringFactory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class TestModule_NonNullableStringFactory implements Factory<String
}

private static final class InstanceHolder {
private static final TestModule_NonNullableStringFactory INSTANCE = new TestModule_NonNullableStringFactory();
static final TestModule_NonNullableStringFactory INSTANCE = new TestModule_NonNullableStringFactory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class GenericClass_Factory<T> implements Factory<GenericClass<T>> {

private static final class InstanceHolder {
@SuppressWarnings("rawtypes")
private static final GenericClass_Factory INSTANCE = new GenericClass_Factory();
static final GenericClass_Factory INSTANCE = new GenericClass_Factory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class SimpleType_Factory implements Factory<SimpleType> {
}

private static final class InstanceHolder {
private static final SimpleType_Factory INSTANCE = new SimpleType_Factory();
static final SimpleType_Factory INSTANCE = new SimpleType_Factory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class OuterType_A_Factory implements Factory<OuterType.A> {
}

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();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class ScopedBinding_Factory implements Factory<ScopedBinding> {
}

private static final class InstanceHolder {
private static final ScopedBinding_Factory INSTANCE = new ScopedBinding_Factory();
static final ScopedBinding_Factory INSTANCE = new ScopedBinding_Factory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class ScopedBinding_Factory implements Factory<ScopedBinding> {
}

private static final class InstanceHolder {
private static final ScopedBinding_Factory INSTANCE = new ScopedBinding_Factory();
static final ScopedBinding_Factory INSTANCE = new ScopedBinding_Factory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class TestModule_CreateFactory implements Factory<Boolean> {
}

private static final class InstanceHolder {
private static final TestModule_CreateFactory INSTANCE = new TestModule_CreateFactory();
static final TestModule_CreateFactory INSTANCE = new TestModule_CreateFactory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class TestModule_GetFactory implements Factory<Integer> {
}

private static final class InstanceHolder {
private static final TestModule_GetFactory INSTANCE = new TestModule_GetFactory();
static final TestModule_GetFactory INSTANCE = new TestModule_GetFactory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class MyModule_ProvideStringFactory implements Factory<String> {
}

private static final class InstanceHolder {
private static final MyModule_ProvideStringFactory INSTANCE = new MyModule_ProvideStringFactory();
static final MyModule_ProvideStringFactory INSTANCE = new MyModule_ProvideStringFactory();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class MyModule_ProvideStringFactory implements Factory<String> {
}

private static final class InstanceHolder {
private static final MyModule_ProvideStringFactory INSTANCE = new MyModule_ProvideStringFactory();
static final MyModule_ProvideStringFactory INSTANCE = new MyModule_ProvideStringFactory();
}
}

0 comments on commit 07d8f88

Please sign in to comment.