Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove private modifier from fields/methods within generated Dagger component's private implementation classes. #4561

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions java/dagger/internal/codegen/writing/ComponentImplementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public CodeBlock shardFieldReference() {
// Add the shard if this is the first time it's requested by something.
String shardFieldName =
componentShard.getUniqueFieldName(UPPER_CAMEL.to(LOWER_CAMEL, name.simpleName()));
FieldSpec shardField = FieldSpec.builder(name, shardFieldName, PRIVATE).build();
FieldSpec shardField = FieldSpec.builder(name, shardFieldName).build();

shardFieldsByImplementation.put(this, shardField);
}
Expand Down Expand Up @@ -726,6 +726,10 @@ public void claimMethodName(CharSequence name) {
componentMethodNames.claim(name);
}

public boolean isShardClassPrivate() {
return modifiers().contains(PRIVATE);
}

@Override
public TypeSpec generate() {
TypeSpec.Builder builder = classBuilder(name);
Expand Down Expand Up @@ -938,7 +942,11 @@ private void addShards() {

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

// Add a constructor parameter and initialization for each component field. We initialize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ private FieldSpec getOrCreateField() {
FieldSpec.builder(
toJavaPoet(fieldType),
shardImplementation.getUniqueFieldName(contributionBindingField.name()));
contributionField.addModifiers(PRIVATE);
// TODO(bcorso): remove once dagger.generatedClassExtendsComponent flag is removed.
if (!shardImplementation.isShardClassPrivate()) {
contributionField.addModifiers(PRIVATE);
}
if (useRawType) {
contributionField.addAnnotation(AnnotationSpecs.suppressWarnings(RAWTYPES));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XType;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.TypeName;
import dagger.assisted.Assisted;
Expand Down Expand Up @@ -99,7 +100,11 @@ private String methodName() {
shardImplementation.addMethod(
PRIVATE_METHOD,
methodBuilder(methodName)
.addModifiers(PRIVATE)
// TODO(bcorso): remove once dagger.generatedClassExtendsComponent flag is removed.
.addModifiers(
!shardImplementation.isShardClassPrivate()
? ImmutableSet.of(PRIVATE)
: ImmutableSet.of())
.returns(returnType().getTypeName())
.addStatement(
"return $L",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private CodeBlock createSwitchCaseCodeBlock(

return CodeBlock.builder()
// TODO(bcorso): Is there something else more useful than the key?
.add("case $L: // $L \n", switchIds.get(key), key)
.add("case $L: // $L\n", switchIds.get(key), key)
.addStatement("return ($T) $L", T, instanceCodeBlock)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ final class DaggerTestComponent {
private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Foo_Factory fooProvider;
Foo_Factory fooProvider;

private Provider<FooFactory> fooFactoryProvider;
Provider<FooFactory> fooFactoryProvider;

private TestComponentImpl() {
TestComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ final class DaggerTestComponent {
private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Provider<Bar> barProvider;
Provider<Bar> barProvider;

private Provider<FooFactory> fooFactoryProvider;
Provider<FooFactory> fooFactoryProvider;

private TestComponentImpl() {
TestComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ final class DaggerTestComponent {
private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Provider<FooFactory> fooFactoryProvider;
Provider<FooFactory> fooFactoryProvider;

private Provider<Bar> barProvider;
Provider<Bar> barProvider;

private Foo_Factory fooProvider;
Foo_Factory fooProvider;

private TestComponentImpl() {
TestComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ final class DaggerTestComponent {
private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Provider<FooFactory> fooFactoryProvider;
Provider<FooFactory> fooFactoryProvider;

private TestComponentImpl() {
TestComponentImpl() {

initialize();

}

private Bar bar() {
Bar bar() {
return new Bar(fooFactoryProvider.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ final class DaggerTestComponent {
private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Foo_Factory fooProvider;
Foo_Factory fooProvider;

private Provider<FooFactory> fooFactoryProvider;
Provider<FooFactory> fooFactoryProvider;

private TestComponentImpl() {
TestComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ final class DaggerTestComponent {
private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Provider<FooFactory> fooFactoryProvider;
Provider<FooFactory> fooFactoryProvider;

private TestComponentImpl() {
TestComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ final class DaggerMyComponent {

private final MySubcomponentImpl mySubcomponentImpl = this;

private MyAssistedClass_Factory myAssistedClassProvider;
MyAssistedClass_Factory myAssistedClassProvider;

private Provider<MySubcomponentAssistedFactory> mySubcomponentAssistedFactoryProvider;
Provider<MySubcomponentAssistedFactory> mySubcomponentAssistedFactoryProvider;

private MySubcomponentImpl(MyComponentImpl myComponentImpl) {
MySubcomponentImpl(MyComponentImpl myComponentImpl) {
this.myComponentImpl = myComponentImpl;

initialize();
Expand All @@ -70,11 +70,11 @@ final class DaggerMyComponent {
private static final class MyComponentImpl implements MyComponent {
private final MyComponentImpl myComponentImpl = this;

private MyAssistedClass_Factory myAssistedClassProvider;
MyAssistedClass_Factory myAssistedClassProvider;

private Provider<MyComponentAssistedFactory> myComponentAssistedFactoryProvider;
Provider<MyComponentAssistedFactory> myComponentAssistedFactoryProvider;

private MyComponentImpl() {
MyComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ final class DaggerMyComponent {

private final MySubcomponentImpl mySubcomponentImpl = this;

private Provider<MySubcomponentAssistedFactory> mySubcomponentAssistedFactoryProvider;
Provider<MySubcomponentAssistedFactory> mySubcomponentAssistedFactoryProvider;

private MySubcomponentImpl(MyComponentImpl myComponentImpl) {
MySubcomponentImpl(MyComponentImpl myComponentImpl) {
this.myComponentImpl = myComponentImpl;

initialize();
Expand Down Expand Up @@ -99,9 +99,9 @@ final class DaggerMyComponent {
private static final class MyComponentImpl implements MyComponent {
private final MyComponentImpl myComponentImpl = this;

private Provider<MyComponentAssistedFactory> myComponentAssistedFactoryProvider;
Provider<MyComponentAssistedFactory> myComponentAssistedFactoryProvider;

private MyComponentImpl() {
MyComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ final class DaggerTestComponent {
private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Foo_Factory<String> fooProvider;
Foo_Factory<String> fooProvider;

private Provider<FooFactory<String>> fooFactoryProvider;
Provider<FooFactory<String>> fooFactoryProvider;

private TestComponentImpl() {
TestComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ final class DaggerTestComponent {
private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Provider<FooFactory<String>> fooFactoryProvider;
Provider<FooFactory<String>> fooFactoryProvider;

private TestComponentImpl() {
TestComponentImpl() {

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class DaggerTestComponent {

private final TestComponentImpl testComponentImpl = this;

private TestComponentImpl(TestModule testModuleParam) {
TestComponentImpl(TestModule testModuleParam) {
this.testModule = testModuleParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class DaggerTestComponent {

private final TestComponentImpl testComponentImpl = this;

private TestComponentImpl(TestModule testModuleParam) {
TestComponentImpl(TestModule testModuleParam) {
this.testModule = testModuleParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class DaggerTestComponent {

private final TestComponentImpl testComponentImpl = this;

private TestComponentImpl(TestModule testModuleParam) {
TestComponentImpl(TestModule testModuleParam) {
this.testModule = testModuleParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class DaggerTestComponent {

private final TestComponentImpl testComponentImpl = this;

private TestComponentImpl(TestModule testModuleParam) {
TestComponentImpl(TestModule testModuleParam) {
this.testModule = testModuleParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class DaggerTestComponent {

private final TestComponentImpl testComponentImpl = this;

private TestComponentImpl(TestModule testModuleParam) {
TestComponentImpl(TestModule testModuleParam) {
this.testModule = testModuleParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class DaggerTestComponent {

private final TestComponentImpl testComponentImpl = this;

private TestComponentImpl(TestModule testModuleParam) {
TestComponentImpl(TestModule testModuleParam) {
this.testModule = testModuleParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class DaggerSimpleComponent {

private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl(Object objectParam) {
SimpleComponentImpl(Object objectParam) {
this.object = objectParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class DaggerSimpleComponent {

private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl(Object objectParam) {
SimpleComponentImpl(Object objectParam) {
this.object = objectParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class DaggerSimpleComponent {

private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl(Object objectParam) {
SimpleComponentImpl(Object objectParam) {
this.object = objectParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class DaggerSimpleComponent {

private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl(Object objectParam) {
SimpleComponentImpl(Object objectParam) {
this.object = objectParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class DaggerSimpleComponent {

private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl(Integer iParam) {
SimpleComponentImpl(Integer iParam) {
this.i = iParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class DaggerSimpleComponent {

private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl(Integer iParam) {
SimpleComponentImpl(Integer iParam) {
this.i = iParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class DaggerSimpleComponent {

private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl(Integer iParam) {
SimpleComponentImpl(Integer iParam) {
this.i = iParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class DaggerSimpleComponent {

private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl(Integer iParam) {
SimpleComponentImpl(Integer iParam) {
this.i = iParam;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class DaggerSimpleComponent {
private static final class SimpleComponentImpl implements SimpleComponent {
private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl() {
SimpleComponentImpl() {


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class DaggerSimpleComponent {
private static final class SimpleComponentImpl implements SimpleComponent {
private final SimpleComponentImpl simpleComponentImpl = this;

private SimpleComponentImpl() {
SimpleComponentImpl() {


}
Expand Down
Loading
Loading