dimanche 26 mars 2017

Trouble with Java Reflection

So I'm new to using reflection and I'm getting inconsistent results.

First I tried converting this line to reflection

userForm.setEquipment(currentForm.getFirstEquipment);

This is what I did and it seems to work.

try{
    Method setObject = userForm.getClass().getMethod("setObject", Shipment.class);
    setObject.invoke(userForm, currentForm.getFirstObject());
}
catch ( Exception e){
    Logger.error(e);
}

But when I tried something similar with a different line it doesn't work.

Here's what I started with.

List<Shipment> list = new ArrayList<Shipment>();
list.add(userForm.getFirstObject);

and this is what I have now, but it doesn't seem to work.

List<Shipment> list = new ArrayList<Shipment>();
try{
    Method add = list.getClass().getMethod("add", Shipment.class);
    add.invoke(list, userForm.getFirstObject());
}
catch ( Exception e){
    Logger.error(e);
}





Aucun commentaire:

Enregistrer un commentaire