Skip to content

AutoFactory: missing @Provided leads to StackOverflowError #697

Open

Description

@wideberg

I usually generate factories that implement an interface:

@AutoFactory(implementing = Foo.Factory.class)
class Foo {

    interface Factory {
        Foo create(String runtimeDep);
    }

    @Inject
    Foo(
            @Provided String dep1,
            String dep2,      // <-- missing annotation
            String runtimeDep
    ){}
}

however, if one forgets to annotate all dependencies with @provided (has happened to me a few times now and also to my colleague developers), then the generated class looks like this:

@Generated(
  value = "com.google.auto.factory.processor.AutoFactoryProcessor",
  comments = "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/google/auto/tree/master/factory"
)
final class FooFactory implements Foo.Factory {
  private final Provider<String> dep1Provider;

  @Inject
  FooFactory(Provider<String> dep1Provider) {
    this.dep1Provider = checkNotNull(dep1Provider, 1);
  }

  Foo create(String dep2, String runtimeDep) {
    return new Foo(
        checkNotNull(dep1Provider.get(), 1), checkNotNull(dep2, 2), checkNotNull(runtimeDep, 3));
  }

  @Override
  public Foo create(String runtimeDep) {
    return create(runtimeDep);         // <-- recursive
  }

  private static <T> T checkNotNull(T reference, int argumentIndex) {
    if (reference == null) {
      throw new NullPointerException(
          "@AutoFactory method argument is null but is not marked @Nullable. Argument index: "
              + argumentIndex);
    }
    return reference;
  }
}

Using the method declared in the interface leads to a StackOverflowError at runtime.

Is it possible to change this? I really don't see what's good about recursively calling the factory method.
Also, would it be a good idea to not allow implementing an interface unless the non-annotated parameter list corresponds to the interface?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions