gnychis
gnychis

Reputation: 7565

method not applicable for the arguments, but not sure why

I have the following method which takes a list of classes as a parameter:

public List<Interface> getInterfacesOfTypes(List<Class<? extends InternalRadio>> types) {
    List<Interface> interfaces = new ArrayList<Interface>();

    for(Interface iface : _nodes)
        if(types.contains(iface._type))
            interfaces.add(iface);

    return interfaces;
}

What I want to do is create a wrapper for it where only a single class is specified, which calls the above method with a list of only that one class:

public List<Interface> getInterfacesOfType(Class<? extends InternalRadio> type) {       
    return getInterfacesOfTypes(Arrays.asList(type));
}

However, I am getting an error:

The method getInterfacesOfTypes(List<Class<? extends InternalRadio>>) in the type InterfaceConnectivityGraph is not applicable for the arguments (List<Class<capture#3-of ? extends InternalRadio>>)  

I can't figure out why this is or what the capture #3-of even means. I'd greatly appreciate any help!

Upvotes: 0

Views: 429

Answers (2)

Arnout Engelen
Arnout Engelen

Reputation: 6927

Solution

Change the interface to the following:

public List<Interface> getInterfacesOfTypes(List<? extends Class<? extends InternalRadio>> types)

To be quite honest, I cannot really explain why. Broadening the range of allowed generic collections (by adding '? extends') just makes it easier for the compiler to see this is valid...

Aside

  • Instead of Arrays.asList(type) I would write Collections.singletonList(type).
  • Prefixing class members with '_' is uncommon in Java
  • I think Interface is not a great name as 'interface' is also a Java concept (and it seems Interface is not such an interface :) )
  • I'd probably use an 'getType()' function on Interface instead of directly referring to its '_type' field - this makes for easier refactoring later.
  • You can probably accept any Collection rather than requiring a List

Upvotes: 1

Illyr
Illyr

Reputation: 155

If you are sure of you object types:

public List<Interface> getInterfacesOfType(final Class<? extends InternalRadio> type)
    {
        final List list = Arrays.asList(type);
        @SuppressWarnings("unchecked")
        final List<Class<? extends Interface>> adapters = list;

        return getInterfacesOfTypes(adapters);
    }

Upvotes: 0

Related Questions