Share this page

# Example: fieldGetter and fieldSetter

Below are a short example of fieldGetter and fieldSetter and a larger example showing the script context.

## Short example

        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

    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";
            }
        }
    }

Tips to improve your search results... VuGen Function Reference Version 25.1-25.3 Function Reference 25.1-25.3 All View Local Help Center View Online Help Center Version: