user1709652
user1709652

Reputation: 19

Java Generic confusion on wild card

I am learning generic and got struck at some point.here is my class

public class MyValidator{

    private Class providerClass;

    public MyValidator(Container container){
       setProviderClass(container.getInstanc(ValidationProvider.class),"my.providerClass");
   }

    public void setProviderClass(Class<? extends ValidationProvider> providerClass)
    {
        this.providerClass = providerClass;
    }

}

Signature of container is

 <T> T getInstance(Class<T> type, String name)
Gets an instance of the given dependency which was declared in ContainerBuilder.

Container is an inbuild DI mechanism of the platform i am using, but when i call the setter method from inside my constructor i am getting following exception in Eclipse

The method setProviderClass(Class) in the type MyValidator is not applicable for the arguments (ValidationProvider)

I know i am not following the contract properly but not sure how to do this.My intentions are to set the class at run time and it should accept all those implimentation who impliments ValidationProvider which is an interface.

Upvotes: 0

Views: 65

Answers (1)

Puce
Puce

Reputation: 38132

getInstance(Class type, String name) return T not Class -> an instance of ValidationProvider not a class object.

That's what the exception message says: you need a Class object but you're passing a ValidationProvider object.

Upvotes: 1

Related Questions