AutoFactory: missing @Provided leads to StackOverflowError
- Dominant language
- Java
- Stars
- 10.6k
- Forks
- 1.2k
- Avg merge
- 6h 32m
- Merged PRs (30d)
- 13
Description
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 dep1Provider;
@Inject
FooFactory(Provider 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 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?
Contributor guide
Assessment
This issue has not been assessed yet.