I have the following generic FunctionalInterface:
@FunctionalInterface
public interface FooInterface<T> {
void bar(T arg);
}
And this ArrayList descendant:
public class FooList<T> extends ArrayList<FooInterface<T>> {
public void doFoo(T arg) {
for(Iterator<FooInterface<T>> i = iterator(); i.hasNext(); ) {
i.next().bar(arg);
}
}
}
Now, I write this code using method references and type erasure:
protected void doFoo(Object arg) { }
private void doStuff() {
FooInterface f = this::doFoo;
List<FooInterface> list = new ArrayList<>();
list.add(f2);
list.add(this::doFoo);
FooList list2 = new FooList();
list2.add(f2);
list2.add(this::doFoo); // <-- Compiler chokes here, complaining that this is not a FunctionalInterface
}
This baffles me. Why would the compiler be fine with me assigning this::doFoo to a FooInterface variable, and calling List.add() in the first part of the code, only to reject calling the same add() method from the class that descends from ArrayList?
Seems like something funky is going on with type erasure in my descendant class, but what? Is this a bug? Have I done something not supported?