samedi 27 février 2016

Alternatives to reflection when accessing an arbitrary field

I was using this code to create some sort of a universal changer class:

    //constructor method
    public Change(Object affdObj, String affdField, float modifier) {
        obj = affdObj;
        //...
        affectedField = affdObj.getClass().getField(affdField);
        //...
        affectedField.setFloat(obj, affectedField.getFloat(obj) + modifier);
    }

But then I was advised to avoid reflection whenever possible since it's very slow. I was suggested to pay attention to interfaces. Unfortunately I can't see how to fit interfaces to my code.

Hence my question: if one needs to access a field which name he doesn't know in advance are there any options other than using reflection?





Aucun commentaire:

Enregistrer un commentaire