I am using Dagger dependency injection to initiate a list of Foo
elements that will get passed to another object. Here is essentially the code:
@Provides
@Singleton
public List<Foo> provideList(@Named("foo1") Foo foo1, @Named("foo2") Foo foo2) {
List<Foo> list = new ArrayList<>();
list.add(foo1);
list.add(foo2);
return list;
}
As time progresses, we expect to add more and more Foo
arguments to this method. To make the code more durable, I'd like to do something like the following:
@Provides
@Singleton
public List<Foo> provideList(@Named("foo1") Foo foo1, @Named("foo2") Foo foo2) {
List<Foo> list = new ArrayList<>();
args.forEach(arg -> {
if (arg instanceof Foo) list.add((Foo) arg);
});
return list;
}
Is anything like this possible in Java?