annedroiid annedroiid - 4 months ago 9
Java Question

How do I pass in a variable of the form "Class<HostResolver> hostResolver"

I'm using java to do a logging project, and I've come across a method that wants a parameter of the form

Class<HostResolver> hostResolver
, where HostResolver.class is an interface. Can anyone suggest what I'm meant to be passing in here?

If I pass in HostResolver.class it will accept it as a parameter, but then fail at hostResolver.newInstance(). However if I pass in an implementation of HostResolver it says it's not of the right type for the method.

Here's the specific method.

protected static HostResolver createHostResolver(final Class<HostResolver> hostResolver) throws
IllegalAccessException, InstantiationException {
return hostResolver.newInstance();
}

Answer

It turns out the method was meant to have Class, but that the change hadn't been pushed through. Once this was changed I was able to pass it a class that extends HostResolver

protected static HostResolver createHostResolver(final Class<? extends HostResolver> HostResolver) throws
    IllegalAccessException, InstantiationException {
return hostResolver.newInstance();

}

Comments