Share this page

# lrapi.lr2.fieldSetter

Reflects an object and sets the value of a field.

Java over HTTP Vuser Functions

    public static Object fieldSetter(Object subject, String path, Object value) throws NoSuchFieldException, IllegalAccessException, ArrayIndexOutOfBoundsException

## Arguments

Argument | Description
--- | ---
subject | The object to reflect.
path | The path to the array or member.
value | The value to set

fieldSetter reflects an object and sets the value of a field.

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

If the type of the value argument is java.lang.Object, pass the Object or String itself. If the value is a primitive type, use either, a primitive argument or a string representation of the argument. For example, "true" or "false" for boolean, 321 or "321" for int.

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

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