Skip to content

Commit

Permalink
Fixes self-loop when generated implementation of an assisted factory …
Browse files Browse the repository at this point in the history
…method calls a generated component method with the same name.

The problem exists in FastInit mode, where the private method name can conflict with the factory method name:

```
final class DaggerMyComponent implements MyComponent {
  @OverRide
  public FooFactory fooFactory() {
    return new FooFactory() {
      @OverRide
      public Foo foo() {
        return foo();  // This calls factory method rather than component method
      }
    };
  }

  private Foo foo() {
    return new Foo(getDep());
  }
}
```

The fix is to reference outer component when calling the private method within the anonymous class.

Fixes #2359

RELNOTES=Fixes #2359: Fixes self-loop when generated implementation of an assisted factory method calls a generated component method with the same name.
PiperOrigin-RevId: 355887899
  • Loading branch information
bcorso authored and Dagger Team committed Feb 6, 2021
1 parent 6592b06 commit 04b950d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ Expression getDependencyExpression(ClassName requestingClass) {
Expression assistedInjectionExpression =
componentBindingExpressions.getDependencyExpression(
BindingRequest.bindingRequest(assistedInjectionRequest.key(), RequestKind.INSTANCE),
requestingClass);
// This is kind of gross because the anonymous class doesn't really have a name we can
// reference. The requesting class name is really only needed to determine if we need to
// append "OwningClass.this." to the method call or not.
// TODO(bcorso): We should probably use a non-anonymous class here instead so that we
// actually have a proper class name.
requestingClass.peerClass(""));
return Expression.create(
assistedInjectionExpression.type(),
CodeBlock.of("$L", anonymousfactoryImpl(assistedInjectionExpression)));
Expand Down
4 changes: 2 additions & 2 deletions javatests/dagger/internal/codegen/AssistedFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testAssistedFactory() {
" return new FooFactory() {",
" @Override",
" public Foo create(String str) {",
" return foo(str);",
" return DaggerTestComponent.this.foo(str);",
" }",
" };",
" }",
Expand Down Expand Up @@ -210,7 +210,7 @@ public void testAssistedFactoryCycle() {
" return new FooFactory() {",
" @Override",
" public Foo create(String str) {",
" return foo(str);",
" return DaggerTestComponent.this.foo(str);",
" }",
" };",
" }",
Expand Down

0 comments on commit 04b950d

Please sign in to comment.