How do I modified Java object in script?

This example show how to modified Java object from scripting environment. Below we manipulate a collection of string data. To pass data into the scripting engine we use a key-value binding to the script engine.

package org.kodejava.example.script;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.List;
import java.util.ArrayList;

public class ModifiedJavaObjectFromScript {
    public static void main(String[] args) {
        // Creating an array of five colors
        List<String> colors = new ArrayList<>();
        colors.add("White");
        colors.add("Black");
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");

        // Obtain a ScriptEngine instance.
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByExtension("js");

        // Place the colors list into the engine using colorList key.
        // After setting the list into the engine our script will be
        // able to read it.
        engine.put("colorList", colors);

        try {
            engine.eval(getScript());

            // Redisplay the modified version of colors list object.
            for (String color : colors) {
                System.out.println("Color = " + color);
            }
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }

    private static String getScript() {
        // Below is our script to read the values of Java List that
        // contains string of colors. We also add some other colors
        // to the list object in the script environment.
        return "var index; " +
            "var colors = colorList.toArray(); " +
            " " +
            "for (index in colors) { " +
            "    println(colors[index]); " +
            "}" +
            " " +
            "colorList.add(\"Yellow\"); " +
            "colorList.add(\"Purple\"); " +
            "colorList.add(\"Orange\"); ";
    }
}

The output of the code snippet above is:

White
Black
Red
Green
Blue
Color = White
Color = Black
Color = Red
Color = Green
Color = Blue
Color = Yellow
Color = Purple
Color = Orange

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.