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

AutoFactory: missing @Provided leads to StackOverflowError #697

Open
wideberg opened this issue Jan 23, 2019 · 0 comments
Open

AutoFactory: missing @Provided leads to StackOverflowError #697

wideberg opened this issue Jan 23, 2019 · 0 comments

Comments

@wideberg
Copy link

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 = "https://github.com/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
Projects
None yet
Development

No branches or pull requests

3 participants