Skip to content

Commit cc1c6c7

Browse files
bcorsoDagger Team
authored and
Dagger Team
committed
Remove private modifier from fields/methods within generated Dagger component's private implementation classes.
This change has a couple benefits: 1. Reduces the amount of synthetic accessor methods needed for JDK<11/Dalvik/ART which don't support nestmates (see #4544). 2. Will likely be needed before we start generating Kotlin source anyway since Kotlin doesn't allow a class to access another class's private members in such a way. RELNOTES=Remove private modifier from fields/methods within private implementation classes. PiperOrigin-RevId: 712627446
1 parent 9daa0ae commit cc1c6c7

File tree

245 files changed

+693
-675
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

245 files changed

+693
-675
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public CodeBlock shardFieldReference() {
557557
// Add the shard if this is the first time it's requested by something.
558558
String shardFieldName =
559559
componentShard.getUniqueFieldName(UPPER_CAMEL.to(LOWER_CAMEL, name.simpleName()));
560-
FieldSpec shardField = FieldSpec.builder(name, shardFieldName, PRIVATE).build();
560+
FieldSpec shardField = FieldSpec.builder(name, shardFieldName).build();
561561

562562
shardFieldsByImplementation.put(this, shardField);
563563
}
@@ -726,6 +726,10 @@ public void claimMethodName(CharSequence name) {
726726
componentMethodNames.claim(name);
727727
}
728728

729+
public boolean isShardClassPrivate() {
730+
return modifiers().contains(PRIVATE);
731+
}
732+
729733
@Override
730734
public TypeSpec generate() {
731735
TypeSpec.Builder builder = classBuilder(name);
@@ -938,7 +942,11 @@ private void addShards() {
938942

939943
/** Creates and adds the constructor and methods needed for initializing the component. */
940944
private void addConstructorAndInitializationMethods() {
941-
MethodSpec.Builder constructor = constructorBuilder().addModifiers(PRIVATE);
945+
MethodSpec.Builder constructor = constructorBuilder();
946+
// TODO(bcorso): remove once dagger.generatedClassExtendsComponent flag is removed.
947+
if (!isShardClassPrivate()) {
948+
constructor.addModifiers(PRIVATE);
949+
}
942950
ImmutableList<ParameterSpec> parameters = constructorParameters.values().asList();
943951

944952
// Add a constructor parameter and initialization for each component field. We initialize

java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ private FieldSpec getOrCreateField() {
165165
FieldSpec.builder(
166166
toJavaPoet(fieldType),
167167
shardImplementation.getUniqueFieldName(contributionBindingField.name()));
168-
contributionField.addModifiers(PRIVATE);
168+
// TODO(bcorso): remove once dagger.generatedClassExtendsComponent flag is removed.
169+
if (!shardImplementation.isShardClassPrivate()) {
170+
contributionField.addModifiers(PRIVATE);
171+
}
169172
if (useRawType) {
170173
contributionField.addAnnotation(AnnotationSpecs.suppressWarnings(RAWTYPES));
171174
}

java/dagger/internal/codegen/writing/PrivateMethodRequestRepresentation.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import androidx.room.compiler.processing.XProcessingEnv;
2828
import androidx.room.compiler.processing.XType;
29+
import com.google.common.collect.ImmutableSet;
2930
import com.squareup.javapoet.CodeBlock;
3031
import com.squareup.javapoet.TypeName;
3132
import dagger.assisted.Assisted;
@@ -99,7 +100,11 @@ private String methodName() {
99100
shardImplementation.addMethod(
100101
PRIVATE_METHOD,
101102
methodBuilder(methodName)
102-
.addModifiers(PRIVATE)
103+
// TODO(bcorso): remove once dagger.generatedClassExtendsComponent flag is removed.
104+
.addModifiers(
105+
!shardImplementation.isShardClassPrivate()
106+
? ImmutableSet.of(PRIVATE)
107+
: ImmutableSet.of())
103108
.returns(returnType().getTypeName())
104109
.addStatement(
105110
"return $L",

java/dagger/internal/codegen/writing/SwitchingProviders.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private CodeBlock createSwitchCaseCodeBlock(
162162

163163
return CodeBlock.builder()
164164
// TODO(bcorso): Is there something else more useful than the key?
165-
.add("case $L: // $L \n", switchIds.get(key), key)
165+
.add("case $L: // $L\n", switchIds.get(key), key)
166166
.addStatement("return ($T) $L", T, instanceCodeBlock)
167167
.build();
168168
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ final class DaggerTestComponent {
4242
private static final class TestComponentImpl implements TestComponent {
4343
private final TestComponentImpl testComponentImpl = this;
4444

45-
private Foo_Factory fooProvider;
45+
Foo_Factory fooProvider;
4646

47-
private Provider<FooFactory> fooFactoryProvider;
47+
Provider<FooFactory> fooFactoryProvider;
4848

49-
private TestComponentImpl() {
49+
TestComponentImpl() {
5050

5151
initialize();
5252

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ final class DaggerTestComponent {
4343
private static final class TestComponentImpl implements TestComponent {
4444
private final TestComponentImpl testComponentImpl = this;
4545

46-
private Provider<Bar> barProvider;
46+
Provider<Bar> barProvider;
4747

48-
private Provider<FooFactory> fooFactoryProvider;
48+
Provider<FooFactory> fooFactoryProvider;
4949

50-
private TestComponentImpl() {
50+
TestComponentImpl() {
5151

5252
initialize();
5353

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ final class DaggerTestComponent {
4343
private static final class TestComponentImpl implements TestComponent {
4444
private final TestComponentImpl testComponentImpl = this;
4545

46-
private Provider<FooFactory> fooFactoryProvider;
46+
Provider<FooFactory> fooFactoryProvider;
4747

48-
private Provider<Bar> barProvider;
48+
Provider<Bar> barProvider;
4949

50-
private Foo_Factory fooProvider;
50+
Foo_Factory fooProvider;
5151

52-
private TestComponentImpl() {
52+
TestComponentImpl() {
5353

5454
initialize();
5555

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ final class DaggerTestComponent {
4444
private static final class TestComponentImpl implements TestComponent {
4545
private final TestComponentImpl testComponentImpl = this;
4646

47-
private Provider<FooFactory> fooFactoryProvider;
47+
Provider<FooFactory> fooFactoryProvider;
4848

49-
private TestComponentImpl() {
49+
TestComponentImpl() {
5050

5151
initialize();
5252

5353
}
5454

55-
private Bar bar() {
55+
Bar bar() {
5656
return new Bar(fooFactoryProvider.get());
5757
}
5858

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ final class DaggerTestComponent {
4242
private static final class TestComponentImpl implements TestComponent {
4343
private final TestComponentImpl testComponentImpl = this;
4444

45-
private Foo_Factory fooProvider;
45+
Foo_Factory fooProvider;
4646

47-
private Provider<FooFactory> fooFactoryProvider;
47+
Provider<FooFactory> fooFactoryProvider;
4848

49-
private TestComponentImpl() {
49+
TestComponentImpl() {
5050

5151
initialize();
5252

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ final class DaggerTestComponent {
4343
private static final class TestComponentImpl implements TestComponent {
4444
private final TestComponentImpl testComponentImpl = this;
4545

46-
private Provider<FooFactory> fooFactoryProvider;
46+
Provider<FooFactory> fooFactoryProvider;
4747

48-
private TestComponentImpl() {
48+
TestComponentImpl() {
4949

5050
initialize();
5151

javatests/dagger/internal/codegen/goldens/AssistedFactoryTest_testMultipleAssistedFactoryInDifferentComponents_DEFAULT_MODE_test.DaggerMyComponent

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ final class DaggerMyComponent {
4444

4545
private final MySubcomponentImpl mySubcomponentImpl = this;
4646

47-
private MyAssistedClass_Factory myAssistedClassProvider;
47+
MyAssistedClass_Factory myAssistedClassProvider;
4848

49-
private Provider<MySubcomponentAssistedFactory> mySubcomponentAssistedFactoryProvider;
49+
Provider<MySubcomponentAssistedFactory> mySubcomponentAssistedFactoryProvider;
5050

51-
private MySubcomponentImpl(MyComponentImpl myComponentImpl) {
51+
MySubcomponentImpl(MyComponentImpl myComponentImpl) {
5252
this.myComponentImpl = myComponentImpl;
5353

5454
initialize();
@@ -70,11 +70,11 @@ final class DaggerMyComponent {
7070
private static final class MyComponentImpl implements MyComponent {
7171
private final MyComponentImpl myComponentImpl = this;
7272

73-
private MyAssistedClass_Factory myAssistedClassProvider;
73+
MyAssistedClass_Factory myAssistedClassProvider;
7474

75-
private Provider<MyComponentAssistedFactory> myComponentAssistedFactoryProvider;
75+
Provider<MyComponentAssistedFactory> myComponentAssistedFactoryProvider;
7676

77-
private MyComponentImpl() {
77+
MyComponentImpl() {
7878

7979
initialize();
8080

javatests/dagger/internal/codegen/goldens/AssistedFactoryTest_testMultipleAssistedFactoryInDifferentComponents_FAST_INIT_MODE_test.DaggerMyComponent

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ final class DaggerMyComponent {
4545

4646
private final MySubcomponentImpl mySubcomponentImpl = this;
4747

48-
private Provider<MySubcomponentAssistedFactory> mySubcomponentAssistedFactoryProvider;
48+
Provider<MySubcomponentAssistedFactory> mySubcomponentAssistedFactoryProvider;
4949

50-
private MySubcomponentImpl(MyComponentImpl myComponentImpl) {
50+
MySubcomponentImpl(MyComponentImpl myComponentImpl) {
5151
this.myComponentImpl = myComponentImpl;
5252

5353
initialize();
@@ -99,9 +99,9 @@ final class DaggerMyComponent {
9999
private static final class MyComponentImpl implements MyComponent {
100100
private final MyComponentImpl myComponentImpl = this;
101101

102-
private Provider<MyComponentAssistedFactory> myComponentAssistedFactoryProvider;
102+
Provider<MyComponentAssistedFactory> myComponentAssistedFactoryProvider;
103103

104-
private MyComponentImpl() {
104+
MyComponentImpl() {
105105

106106
initialize();
107107

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ final class DaggerTestComponent {
4242
private static final class TestComponentImpl implements TestComponent {
4343
private final TestComponentImpl testComponentImpl = this;
4444

45-
private Foo_Factory<String> fooProvider;
45+
Foo_Factory<String> fooProvider;
4646

47-
private Provider<FooFactory<String>> fooFactoryProvider;
47+
Provider<FooFactory<String>> fooFactoryProvider;
4848

49-
private TestComponentImpl() {
49+
TestComponentImpl() {
5050

5151
initialize();
5252

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ final class DaggerTestComponent {
4343
private static final class TestComponentImpl implements TestComponent {
4444
private final TestComponentImpl testComponentImpl = this;
4545

46-
private Provider<FooFactory<String>> fooFactoryProvider;
46+
Provider<FooFactory<String>> fooFactoryProvider;
4747

48-
private TestComponentImpl() {
48+
TestComponentImpl() {
4949

5050
initialize();
5151

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final class DaggerTestComponent {
5353

5454
private final TestComponentImpl testComponentImpl = this;
5555

56-
private TestComponentImpl(TestModule testModuleParam) {
56+
TestComponentImpl(TestModule testModuleParam) {
5757
this.testModule = testModuleParam;
5858

5959
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final class DaggerTestComponent {
5353

5454
private final TestComponentImpl testComponentImpl = this;
5555

56-
private TestComponentImpl(TestModule testModuleParam) {
56+
TestComponentImpl(TestModule testModuleParam) {
5757
this.testModule = testModuleParam;
5858

5959
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCanInstantiateModulesUserCannotSet_compilerMode=DEFAULT_MODE, creatorKind=dagger.Component.Builder_test.DaggerTestComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class DaggerTestComponent {
4141

4242
private final TestComponentImpl testComponentImpl = this;
4343

44-
private TestComponentImpl(TestModule testModuleParam) {
44+
TestComponentImpl(TestModule testModuleParam) {
4545
this.testModule = testModuleParam;
4646

4747
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCanInstantiateModulesUserCannotSet_compilerMode=DEFAULT_MODE, creatorKind=dagger.Component.Factory_test.DaggerTestComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class DaggerTestComponent {
4141

4242
private final TestComponentImpl testComponentImpl = this;
4343

44-
private TestComponentImpl(TestModule testModuleParam) {
44+
TestComponentImpl(TestModule testModuleParam) {
4545
this.testModule = testModuleParam;
4646

4747
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCanInstantiateModulesUserCannotSet_compilerMode=FAST_INIT_MODE, creatorKind=dagger.Component.Builder_test.DaggerTestComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class DaggerTestComponent {
4141

4242
private final TestComponentImpl testComponentImpl = this;
4343

44-
private TestComponentImpl(TestModule testModuleParam) {
44+
TestComponentImpl(TestModule testModuleParam) {
4545
this.testModule = testModuleParam;
4646

4747
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCanInstantiateModulesUserCannotSet_compilerMode=FAST_INIT_MODE, creatorKind=dagger.Component.Factory_test.DaggerTestComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class DaggerTestComponent {
4141

4242
private final TestComponentImpl testComponentImpl = this;
4343

44-
private TestComponentImpl(TestModule testModuleParam) {
44+
TestComponentImpl(TestModule testModuleParam) {
4545
this.testModule = testModuleParam;
4646

4747
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCreatorWithBindsInstanceNoStaticCreateGenerated_compilerMode=DEFAULT_MODE, creatorKind=dagger.Component.Builder_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class DaggerSimpleComponent {
4747

4848
private final SimpleComponentImpl simpleComponentImpl = this;
4949

50-
private SimpleComponentImpl(Object objectParam) {
50+
SimpleComponentImpl(Object objectParam) {
5151
this.object = objectParam;
5252

5353
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCreatorWithBindsInstanceNoStaticCreateGenerated_compilerMode=DEFAULT_MODE, creatorKind=dagger.Component.Factory_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class DaggerSimpleComponent {
3939

4040
private final SimpleComponentImpl simpleComponentImpl = this;
4141

42-
private SimpleComponentImpl(Object objectParam) {
42+
SimpleComponentImpl(Object objectParam) {
4343
this.object = objectParam;
4444

4545
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCreatorWithBindsInstanceNoStaticCreateGenerated_compilerMode=FAST_INIT_MODE, creatorKind=dagger.Component.Builder_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class DaggerSimpleComponent {
4747

4848
private final SimpleComponentImpl simpleComponentImpl = this;
4949

50-
private SimpleComponentImpl(Object objectParam) {
50+
SimpleComponentImpl(Object objectParam) {
5151
this.object = objectParam;
5252

5353
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCreatorWithBindsInstanceNoStaticCreateGenerated_compilerMode=FAST_INIT_MODE, creatorKind=dagger.Component.Factory_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class DaggerSimpleComponent {
3939

4040
private final SimpleComponentImpl simpleComponentImpl = this;
4141

42-
private SimpleComponentImpl(Object objectParam) {
42+
SimpleComponentImpl(Object objectParam) {
4343
this.object = objectParam;
4444

4545
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCreatorWithPrimitiveBindsInstance_compilerMode=DEFAULT_MODE, creatorKind=dagger.Component.Builder_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class DaggerSimpleComponent {
4747

4848
private final SimpleComponentImpl simpleComponentImpl = this;
4949

50-
private SimpleComponentImpl(Integer iParam) {
50+
SimpleComponentImpl(Integer iParam) {
5151
this.i = iParam;
5252

5353
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCreatorWithPrimitiveBindsInstance_compilerMode=DEFAULT_MODE, creatorKind=dagger.Component.Factory_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class DaggerSimpleComponent {
3939

4040
private final SimpleComponentImpl simpleComponentImpl = this;
4141

42-
private SimpleComponentImpl(Integer iParam) {
42+
SimpleComponentImpl(Integer iParam) {
4343
this.i = iParam;
4444

4545
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCreatorWithPrimitiveBindsInstance_compilerMode=FAST_INIT_MODE, creatorKind=dagger.Component.Builder_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class DaggerSimpleComponent {
4747

4848
private final SimpleComponentImpl simpleComponentImpl = this;
4949

50-
private SimpleComponentImpl(Integer iParam) {
50+
SimpleComponentImpl(Integer iParam) {
5151
this.i = iParam;
5252

5353
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testCreatorWithPrimitiveBindsInstance_compilerMode=FAST_INIT_MODE, creatorKind=dagger.Component.Factory_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class DaggerSimpleComponent {
3939

4040
private final SimpleComponentImpl simpleComponentImpl = this;
4141

42-
private SimpleComponentImpl(Integer iParam) {
42+
SimpleComponentImpl(Integer iParam) {
4343
this.i = iParam;
4444

4545
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testEmptyCreator_compilerMode=DEFAULT_MODE, creatorKind=dagger.Component.Builder_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class DaggerSimpleComponent {
3939
private static final class SimpleComponentImpl implements SimpleComponent {
4040
private final SimpleComponentImpl simpleComponentImpl = this;
4141

42-
private SimpleComponentImpl() {
42+
SimpleComponentImpl() {
4343

4444

4545
}

javatests/dagger/internal/codegen/goldens/ComponentCreatorTest_testEmptyCreator_compilerMode=DEFAULT_MODE, creatorKind=dagger.Component.Factory_test.DaggerSimpleComponent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class DaggerSimpleComponent {
3939
private static final class SimpleComponentImpl implements SimpleComponent {
4040
private final SimpleComponentImpl simpleComponentImpl = this;
4141

42-
private SimpleComponentImpl() {
42+
SimpleComponentImpl() {
4343

4444

4545
}

0 commit comments

Comments
 (0)