I am having issues with Java 8 method reference combined with generic types. I have simplified my problem to make it clear where the problem lies. The following code fails:
public static void main(String[] args) {
new Mapper(TestEvent::setId);
}
private static class Mapper<T> {
private BiConsumer<TestEvent, T> setter;
private Mapper(BiConsumer<TestEvent, T> setter) { this.setter = setter; }
}
private static class TestEvent {
public void setId(Long id) { }
}
But if I change the constructor invocation to
BiConsumer<TestEvent, Long> consumer = TestEvent::setId;
new Mapper(consumer);
Everything works. Can someone explain why?
I know that it works if I remove the generic type (T) and use Long instead, but that will not work when solving my real problem.