Why does this method call fail? (Generics & wildcards)
Asked Answered
C

2

7

I am getting the below error:

'call(ContainsMonitor)' cannot invoke 'call(? extends webscout.Monitor)' in 'WebScoutCallable'

Monitor.java

WebScoutCallable<? extends Monitor> handler;

public setCallable(WebScoutCallable<? extends Monitor> callable) {
     this.handler = callable;
}

WebScoutCallable.java

public interface WebScoutCallable<T extends Monitor> {
     public void call(T caller);
}

ContainsMonitor.java

public class ContainsMonitor extends Monitor {
     public void handleDocument() {
          handler.call(this);
     }
}

I'll freely admit that I'm new to generics and still quite new to Java itself. I find the error message confusing as it looks like it should work (method declaration expects a Monitor or subclass, I'm passing in a subclass). Any help (+explanation) would be greatly appreciated!

Thanks!

Chau answered 18/6, 2015 at 20:53 Comment(0)
V
6

You have a wildcard in the type parameter of your handler variable. The compiler doesn't know what the exact type of this type parameter is, only that it's either Monitor or a subclass.

The call method takes a T, which is matched on the wildcard. But there is no guarantee that the wildcard type is a ContainsMonitor. It could be a Monitor, or it could be MonitorSubtypeThatDoesntExistYet. Because the compiler doesn't know the actual type, it cannot allow you to pass anything except null, because with any non-null argument, it can't guarantee type safety.

You can get around this by removing the wildcard, and replacing that concept with a type parameter on the Monitor class.

class Monitor<T extends Monitor<T>>
{
    WebScoutCallable<T> handler;

    public void setCallable(WebScoutCallable<T> callable) {
         this.handler = callable;
    }
}

The interface WebScoutCallable changes a little in response:

interface WebScoutCallable<T extends Monitor<T>> {
    public void call(T caller);
}

The subclass feeds its own name as the type argument when extending Monitor.

class ContainsMonitor extends Monitor<ContainsMonitor> {
     public void handleDocument() {
          handler.call(this);
     }
}

Now, T will be a known type, and ContainsMonitor defines it to be itself, so it's now legal for it to pass itself to call.

Vey answered 18/6, 2015 at 21:9 Comment(1)
Thank you for the detailed reply. I thought there might be a less verbose solution but this makes sense!Chau
R
3

? extends Monitor means: a specific subclass of Monitor, but we don't know which one. So it may be a ContainsMonitor or not and handler may or may not be able to accept a ContainsMonitor. The compiler can't decide and shows an error.

One way to solve your problem is to use specific types, for example:

class Monitor<T extends Monitor<T>> {
  WebScoutCallable<T> handler;

  public setCallable(WebScoutCallable<T> callable) {
    this.handler = callable;
  }
}

class ContainsMonitor extends Monitor<ContainsMonitor> {
  public void handleDocument() {
    handler.call(this);
  }
}
Rollway answered 18/6, 2015 at 21:7 Comment(1)
Thanks for the quick response. Wish I could mark two solutions as correct!Chau

© 2022 - 2024 — McMap. All rights reserved.