Here is sample code.
package base;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Test {
int value1;
ScriptEngine engine;
public Test(){
this.engine = new ScriptEngineManager().getEngineByName("js");
this.engine.put("p",this);
}
public boolean execute(String script){
try {
if (script != ""){
this.engine.eval(script);
}
return true;
} catch (ScriptException e) {
e.printStackTrace();
return false;
}
}
public void setValue1(int v){
this.value1 = v;
}
public void setValue2(int v){
this.value2 = v;
}
}
And this is sample script to be execute.
p.setValue1(2);
p.setValue2(5);
How to reduce script to
setValue1(2);
setValue2(5);
Edit: I want to run those javascript scripts in method execute() and make script shorter.