// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import bsh.EvalError;
import bsh.Interpreter;

public class EmbeddedBeanShell {
    public static class Data {
        public int iv;
        public String sv;
        public Data(int iv, String sv) {
            this.iv = iv;
            this.sv = sv;
        }
        public void m() {
            iv++;
            sv += "X";
        }
        @Override
        public String toString() {
            return String.format("(%d,%s)", iv, sv);
        }
    }
	public static void main(String[] args) throws EvalError {
		Interpreter i = new Interpreter(); 
        i.set("n", 3);
        i.set("o", new Data(123, "ABC"));
		i.eval("for(int i = 0; i < n; i++) {\r\n" +
               "    System.out.println(\"Hi from BeanShell!\");\r\n" +
               "}\r\n" +
               "o.iv = o.iv + 1;\r\n" +
               "o.sv = o.sv + \"X\";\r\n" +
               "o.m();\r\n");
        Data o = (Data)i.get("o");
        System.out.println(o);
	}
}
