2019-05-24 08:09:59 +01:00
|
|
|
package com.baeldung;
|
|
|
|
|
2019-05-26 01:09:20 +01:00
|
|
|
import groovy.lang.*;
|
|
|
|
import groovy.util.GroovyScriptEngine;
|
|
|
|
import groovy.util.ResourceException;
|
|
|
|
import groovy.util.ScriptException;
|
|
|
|
import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
|
2019-05-24 08:09:59 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2019-05-26 01:09:20 +01:00
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
import java.io.File;
|
2019-05-27 11:25:19 +01:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileReader;
|
2019-05-26 01:09:20 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URL;
|
|
|
|
|
2019-05-24 08:09:59 +01:00
|
|
|
/**
|
|
|
|
* Hello world!
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class App {
|
|
|
|
private final static Logger LOG = LoggerFactory.getLogger(App.class);
|
|
|
|
private final GroovyClassLoader loader;
|
|
|
|
private final GroovyShell shell;
|
2019-05-26 01:09:20 +01:00
|
|
|
private final GroovyScriptEngine engine;
|
2019-05-27 11:25:19 +01:00
|
|
|
private final ScriptEngine engineFromFactory;
|
2019-05-24 08:09:59 +01:00
|
|
|
|
2019-05-26 01:09:20 +01:00
|
|
|
private App() throws IOException {
|
2019-05-24 08:09:59 +01:00
|
|
|
loader = new GroovyClassLoader(this.getClass().getClassLoader());
|
2019-05-27 11:25:19 +01:00
|
|
|
shell = new GroovyShell(loader, new Binding());
|
2019-05-27 12:33:09 +01:00
|
|
|
engine = new GroovyScriptEngine(new URL[] {
|
|
|
|
new File("src/main/groovy/com/baeldung/").toURI().toURL()
|
|
|
|
}, this.getClass().getClassLoader());
|
2019-05-27 11:25:19 +01:00
|
|
|
engineFromFactory = new GroovyScriptEngineFactory().getScriptEngine();
|
2019-05-26 01:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void runCompiledClasses(int x, int y) {
|
2019-05-27 11:25:19 +01:00
|
|
|
LOG.info("Executing {} + {}", x, y);
|
2019-05-26 01:09:20 +01:00
|
|
|
Object result1 = new CalcScript().calcSum(x, y);
|
2019-05-27 11:25:19 +01:00
|
|
|
LOG.info("Result of CalcScript.calcSum() method is {}", result1);
|
2019-05-26 01:09:20 +01:00
|
|
|
|
|
|
|
Object result2 = new CalcMath().calcSum(x, y);
|
2019-05-27 11:25:19 +01:00
|
|
|
LOG.info("Result of CalcMath.calcSum() method is {}", result2);
|
2019-05-24 08:09:59 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:25:19 +01:00
|
|
|
private void runDynamicShellScript(int x, int y) throws IOException {
|
|
|
|
Script script = shell.parse(new File("src/main/groovy/com/baeldung/", "CalcScript.groovy"));
|
|
|
|
LOG.info("Executing {} + {}", x, y);
|
|
|
|
Object result = script.invokeMethod("calcSum", new Object[] { x, y });
|
|
|
|
LOG.info("Result of CalcScript.calcSum() method is {}", result);
|
2019-05-24 08:09:59 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:25:19 +01:00
|
|
|
private void runDynamicClassWithLoader(int x, int y) throws IllegalAccessException, InstantiationException, IOException {
|
2019-05-27 12:33:09 +01:00
|
|
|
Class calcClass = loader.parseClass(
|
|
|
|
new File("src/main/groovy/com/baeldung/", "CalcMath.groovy"));
|
|
|
|
GroovyObject calc = (GroovyObject) calcClass.newInstance();
|
|
|
|
Object result = calc.invokeMethod("calcSum", new Object[] { x + 14, y + 14 });
|
2019-05-27 11:25:19 +01:00
|
|
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
2019-05-26 01:09:20 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:25:19 +01:00
|
|
|
private void runDynamicClassWithEngine(int x, int y) throws IllegalAccessException, InstantiationException, ResourceException, ScriptException {
|
2019-05-26 01:09:20 +01:00
|
|
|
|
|
|
|
Class<GroovyObject> calcClass = engine.loadScriptByName("CalcMath.groovy");
|
|
|
|
GroovyObject calc = calcClass.newInstance();
|
|
|
|
//WARNING the following will throw a ClassCastException
|
|
|
|
//((CalcMath)calc).calcSum(1,2);
|
2019-05-27 11:25:19 +01:00
|
|
|
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
|
|
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
2019-05-26 01:09:20 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:25:19 +01:00
|
|
|
private void runDynamicClassWithEngineFactory(int x, int y) throws IllegalAccessException, InstantiationException, javax.script.ScriptException, FileNotFoundException {
|
2019-05-27 12:33:09 +01:00
|
|
|
Class calcClas = (Class) engineFromFactory.eval(
|
|
|
|
new FileReader(new File("src/main/groovy/com/baeldung/", "CalcMath.groovy")));
|
2019-05-27 11:25:19 +01:00
|
|
|
GroovyObject calc = (GroovyObject) calcClas.newInstance();
|
|
|
|
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
|
|
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runStaticCompiledClasses() {
|
|
|
|
LOG.info("Running the Groovy classes compiled statically...");
|
|
|
|
runCompiledClasses(5, 10);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runDynamicCompiledClasses() throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException, ResourceException, ScriptException, javax.script.ScriptException {
|
|
|
|
LOG.info("Running a dynamic groovy script...");
|
|
|
|
runDynamicShellScript(5, 10);
|
|
|
|
LOG.info("Running a dynamic groovy class with GroovyClassLoader...");
|
|
|
|
runDynamicClassWithLoader(10, 30);
|
|
|
|
LOG.info("Running a dynamic groovy class with GroovyScriptEngine...");
|
|
|
|
runDynamicClassWithEngine(15, 0);
|
|
|
|
LOG.info("Running a dynamic groovy class with GroovyScriptEngine JSR223...");
|
|
|
|
runDynamicClassWithEngineFactory(5, 6);
|
2019-05-24 08:09:59 +01:00
|
|
|
}
|
|
|
|
|
2019-05-26 01:09:20 +01:00
|
|
|
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ResourceException, ScriptException, IOException, javax.script.ScriptException {
|
2019-05-24 08:09:59 +01:00
|
|
|
App app = new App();
|
2019-05-27 11:25:19 +01:00
|
|
|
app.runStaticCompiledClasses();
|
|
|
|
app.runDynamicCompiledClasses();
|
2019-05-24 08:09:59 +01:00
|
|
|
}
|
|
|
|
}
|