mercredi 29 mai 2019

java get value of field from array of fields and set field

Wondering how I can get a value of a field from an Array of fields and then depending on that value, set another field in the same array to some value.

For Example:

public void fieldAlter(Field[] fieldArr) {
    for (int i = 0; i < fieldArr.length; i++) {
        Field fieldObj = fieldArr[i];

        if (fieldObj.getName().equals("exampleFieldOne") &&
          fieldObj.getDeclaringClass().getName().equals("some.directory.path.SomeClass")) {
              //Find the index of where field named "exampleFieldTwo" is in 
              //the `fieldArr` array and then set the value for it.
        }
    }
}

Currently, the fieldObj variable will be an object such as:

private java.lang.String some.directory.path.SomeClass

I tried doing the example here, but did not seem to work https://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html





Aucun commentaire:

Enregistrer un commentaire