dimanche 16 juin 2019

Set value to java class by reflection?

I have this model :

public class Base_RegisterPlack {


    private String RegisterPlackTypeValue;

    public void setRegisterPlackTypeValue(String registerPlackTypeValue) {
        RegisterPlackTypeValue = registerPlackTypeValue;
    }
}

in this method i pass this object:

gridView2.setAdapter(GridHelper.getRegisterPlackAdapter(getContext(), getCookie(),
        result_getClsBase_Info.getBase_RegisterPlack()), Base_RegisterPlack.class);

body of setAdapter method is:

public void setAdapter (DataGridAdapter adapter, Class cls) {
    this.baseClass = cls;
    super.setAdapter(adapter);
    setGridAdapter(adapter);
}

Now i want to call set method setRegisterPlackTypeValue so i write this reflection :

try {
    Field field = baseClass.getDeclaredField("RegisterPlackTypeValue");
    field.setAccessible(true);
    field.set(baseClass,"test");
} catch (NoSuchFieldException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}

I got this error :

java.lang.IllegalArgumentException: Expected receiver of type com.module.contracts.Base_RegisterPlack, but got java.lang.Class<com.module.contracts.Base_RegisterPlack>

How can i call set method and convert again this baseClass to the Base_RegisterPlack after set value to it?





Aucun commentaire:

Enregistrer un commentaire