0

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.

4

1 に答える 1

1

getInstance(Class type, String name) return T not Class -> クラスオブジェクトではなく ValidationProvider のインスタンス。

それが例外メッセージの内容です。Class オブジェクトが必要ですが、ValidationProvider オブジェクトを渡しています。

于 2012-10-01T16:35:07.717 に答える