lrapi.lr2.fieldGetter

Reflects an object and returns the value of a field.

Java over HTTP Vuser Functions
public static Object fieldGetter(Object subject, String path) throws IllegalAccessException, NoSuchFieldException, ArrayIndexOutOfBoundsException

Arguments

ArgumentDescription
subject The object to reflect. 
path The path to the array or member. 

Return values

fieldGetter reflects an object and returns the value of a field.

If the path leads to a primitive variable, fieldGetter returns an Object wrapper. For example, Integer for int or Boolean for boolean.If the path leads to an Object, the function returns the value.

In specifying the path, members from inherited parent classes must be accessed using the super keyword. This is different than the default Java behavior, which is to access members directly.

Parameterization

Parameterization is not applicable to this function.

Example

The following example returns a student's grades.

Copy code

    RemoteInvocation RemoteInvocation2 =
        (RemoteInvocation) JavaHTTP.readObject(RemoteInvocationBA0);
    RemoteInvocation.methodName="applyToSchool";
    Student student=RemoteInvocation.arguments[0];

    //grades is a private member of Student
    Map grades=lr2.fieldGetter(student,"grades");
    grades.put("Math","95");

    //Student class inherits the name field from Person. name field is a string
    lr2.fieldSetter(student,"super.name","Tom");

    //Student class inherits the ID field from Person. ID field is an int
    lr2.fieldSetter(student,"super.ID","98764321");

    RemoteInvocationResult RemoteInvocationResult_ArrayList2 =
        (RemoteInvocationResult) JavaHTTP.sendSerialized(
            RemoteInvocation2, 2, "ObjectsDeserializerDefaultImpl"0;

Sample script

The following sample script reflects objects and returns the field values.

Copy code

import lrapi.lr2;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Actions {
    public int init() throws Throwable {
        return 0;
    }//end of init
    public int action() throws Throwable {
        //part1
        {
            ExObj1 exObj1 = new ExObj1(0);
            System.out.println(exObj1);
            lr2.fieldSetter(exObj1, "dbl", "1.23");
            lr2.fieldSetter(exObj1, "integer", "1234");
            List<String> lst =
                (List<String>) lr2.fieldGetter(exObj1, "list");
            lst.add("four");
            System.out.println(exObj1);
        }
        //part2
        ExObj2 exObj2 = new ExObj2();
        ExObj3 exObj3 = new ExObj3();
        System.out.println(exObj3);
        lr2.fieldSetter(exObj3,
            "super.privateObj", "parent access!");
        lr2.fieldSetter(exObj2,
            "objArray[1]", "array access");
        lr2.fieldSetter(exObj2,
            "objArray[2].privateObj",
            "through array & access to private field");
        lr2.fieldSetter(exObj2,
            "objArray[2].publicObj", new Object[20]);
        lr2.fieldSetter(exObj2,
            "objArray[2].publicObj[10]", new Object[10]);
        lr2.fieldSetter(exObj2,
            "objArray[2].publicObj[10][5]", new Object[5]);
        lr2.fieldSetter(exObj2,
            "objArray[2].publicObj[10][5][2]", exObj3);
        lr2.fieldSetter(exObj2,
            "objArray[2].publicObj[10][5][2].privateObj",
            "set from outside!");
        lr2.fieldSetter(exObj2,
            "objArray[2].publicObj[10][5][2].super.privateObj",
            "super set from outside!");
        System.out.println(exObj3);
        return 0;
    }//end of action
    public int end() throws Throwable {
        return 0;
    }//end of end
    class ExObj1 {
        private String str = "preset string";
        private double dbl;
        private int integer;
        private List<String> list = new LinkedList<String>();
        public ExObj1(int integer) {
            this.integer = integer;
            list.addAll(Arrays.asList("one", "two", "three"));
        }
        public String toString() {
            String ans = this.getClass().getName() + "\n"
                + "\tinteger:{\n\t" + integer + "\n\t}\n"
                + "\tdbl:{\n\t" + dbl + "\n\t}\n";
            ans += "\tlist:{\n\t";
            for (String str : list) {
                ans += str + "\n\t";
            }
            ans += "}\n";
            return ans;
        }
    }
    static class ExObj2 {
        public Object publicObj;
        private Object privateObj;
        public static Object[] objArray =
            new Object[]{"1", "12", new ExObj2()};
        public String toString() {
            return this.getClass().getName() + "\n"
                + "\t{publicObj:\t" + publicObj
                + "\n\tprivateObj:" + privateObj
                + "\n\t}\n";
        }
    }
    class ExObj3 extends ExObj2 {
        private Object privateObj;
        public Object publicObj;
        public String toString() {
            return this.getClass().getName() + "\n"
                + "\t{publicObj:" + publicObj
                + "\n\tprivateObj:" + privateObj
                + "\n\t" + super.toString() + "}\n";
        }
    }
}