[BAEL-2899] adding more information and changing methods names
This commit is contained in:
parent
9e0e8a6b0b
commit
dd69f71a9c
@ -5,6 +5,12 @@ def calcSum(x, y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def calcSum2(x, y) {
|
def calcSum2(x, y) {
|
||||||
// DANGER! This won't throw a compilation issue and fail only at runtime!!!
|
// DANGER! The variable "log" may be undefined
|
||||||
|
log.info "Executing $x + $y"
|
||||||
|
// DANGER! This method doesn't exist!
|
||||||
calcSum3()
|
calcSum3()
|
||||||
|
// DANGER! The logged variable "z" is undefined!
|
||||||
|
log.info("Logging an undefined variable: $z")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
calcSum(1,5)
|
||||||
|
@ -40,7 +40,7 @@ public class MyJointCompilationApp {
|
|||||||
engineFromFactory = new GroovyScriptEngineFactory().getScriptEngine();
|
engineFromFactory = new GroovyScriptEngineFactory().getScriptEngine();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runCompiledClasses(int x, int y) {
|
private void addWithCompiledClasses(int x, int y) {
|
||||||
LOG.info("Executing {} + {}", x, y);
|
LOG.info("Executing {} + {}", x, y);
|
||||||
Object result1 = new CalcScript().calcSum(x, y);
|
Object result1 = new CalcScript().calcSum(x, y);
|
||||||
LOG.info("Result of CalcScript.calcSum() method is {}", result1);
|
LOG.info("Result of CalcScript.calcSum() method is {}", result1);
|
||||||
@ -49,14 +49,21 @@ public class MyJointCompilationApp {
|
|||||||
LOG.info("Result of CalcMath.calcSum() method is {}", result2);
|
LOG.info("Result of CalcMath.calcSum() method is {}", result2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runDynamicShellScript(int x, int y) throws IOException {
|
private void addWithGroovyShell(int x, int y) throws IOException {
|
||||||
Script script = shell.parse(new File("src/main/groovy/com/baeldung/", "CalcScript.groovy"));
|
Script script = shell.parse(new File("src/main/groovy/com/baeldung/", "CalcScript.groovy"));
|
||||||
LOG.info("Executing {} + {}", x, y);
|
LOG.info("Executing {} + {}", x, y);
|
||||||
Object result = script.invokeMethod("calcSum", new Object[] { x, y });
|
Object result = script.invokeMethod("calcSum", new Object[] { x, y });
|
||||||
LOG.info("Result of CalcScript.calcSum() method is {}", result);
|
LOG.info("Result of CalcScript.calcSum() method is {}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runDynamicClassWithLoader(int x, int y) throws IllegalAccessException, InstantiationException, IOException {
|
private void addWithGroovyShellRun() throws IOException {
|
||||||
|
Script script = shell.parse(new File("src/main/groovy/com/baeldung/", "CalcScript.groovy"));
|
||||||
|
LOG.info("Executing script run method");
|
||||||
|
Object result = script.run();
|
||||||
|
LOG.info("Result of CalcScript.run() method is {}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addWithGroovyClassLoader(int x, int y) throws IllegalAccessException, InstantiationException, IOException {
|
||||||
Class calcClass = loader.parseClass(
|
Class calcClass = loader.parseClass(
|
||||||
new File("src/main/groovy/com/baeldung/", "CalcMath.groovy"));
|
new File("src/main/groovy/com/baeldung/", "CalcMath.groovy"));
|
||||||
GroovyObject calc = (GroovyObject) calcClass.newInstance();
|
GroovyObject calc = (GroovyObject) calcClass.newInstance();
|
||||||
@ -64,9 +71,8 @@ public class MyJointCompilationApp {
|
|||||||
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runDynamicClassWithEngine(int x, int y) throws IllegalAccessException,
|
private void addWithGroovyScriptEngine(int x, int y) throws IllegalAccessException,
|
||||||
InstantiationException, ResourceException, ScriptException {
|
InstantiationException, ResourceException, ScriptException {
|
||||||
|
|
||||||
Class<GroovyObject> calcClass = engine.loadScriptByName("CalcMath.groovy");
|
Class<GroovyObject> calcClass = engine.loadScriptByName("CalcMath.groovy");
|
||||||
GroovyObject calc = calcClass.newInstance();
|
GroovyObject calc = calcClass.newInstance();
|
||||||
//WARNING the following will throw a ClassCastException
|
//WARNING the following will throw a ClassCastException
|
||||||
@ -75,37 +81,40 @@ public class MyJointCompilationApp {
|
|||||||
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runDynamicClassWithEngineFactory(int x, int y) throws IllegalAccessException,
|
private void addWithEngineFactory(int x, int y) throws IllegalAccessException,
|
||||||
InstantiationException, javax.script.ScriptException, FileNotFoundException {
|
InstantiationException, javax.script.ScriptException, FileNotFoundException {
|
||||||
Class calcClas = (Class) engineFromFactory.eval(
|
Class calcClass = (Class) engineFromFactory.eval(
|
||||||
new FileReader(new File("src/main/groovy/com/baeldung/", "CalcMath.groovy")));
|
new FileReader(new File("src/main/groovy/com/baeldung/", "CalcMath.groovy")));
|
||||||
GroovyObject calc = (GroovyObject) calcClas.newInstance();
|
GroovyObject calc = (GroovyObject) calcClass.newInstance();
|
||||||
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
||||||
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runStaticCompiledClasses() {
|
private void addWithStaticCompiledClasses() {
|
||||||
LOG.info("Running the Groovy classes compiled statically...");
|
LOG.info("Running the Groovy classes compiled statically...");
|
||||||
runCompiledClasses(5, 10);
|
addWithCompiledClasses(5, 10);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runDynamicCompiledClasses() throws IOException, IllegalAccessException, InstantiationException,
|
private void addWithDynamicCompiledClasses() throws IOException, IllegalAccessException, InstantiationException,
|
||||||
ResourceException, ScriptException, javax.script.ScriptException {
|
ResourceException, ScriptException, javax.script.ScriptException {
|
||||||
LOG.info("Running a dynamic groovy script...");
|
LOG.info("Invocation of a dynamic groovy script...");
|
||||||
runDynamicShellScript(5, 10);
|
addWithGroovyShell(5, 10);
|
||||||
LOG.info("Running a dynamic groovy class with GroovyClassLoader...");
|
LOG.info("Invocation of the run method of a dynamic groovy script...");
|
||||||
runDynamicClassWithLoader(10, 30);
|
addWithGroovyShellRun();
|
||||||
LOG.info("Running a dynamic groovy class with GroovyScriptEngine...");
|
LOG.info("Invocation of a dynamic groovy class loaded with GroovyClassLoader...");
|
||||||
runDynamicClassWithEngine(15, 0);
|
addWithGroovyClassLoader(10, 30);
|
||||||
LOG.info("Running a dynamic groovy class with GroovyScriptEngine JSR223...");
|
LOG.info("Invocation of a dynamic groovy class loaded with GroovyScriptEngine...");
|
||||||
runDynamicClassWithEngineFactory(5, 6);
|
addWithGroovyScriptEngine(15, 0);
|
||||||
|
LOG.info("Invocation of a dynamic groovy class loaded with GroovyScriptEngine JSR223...");
|
||||||
|
addWithEngineFactory(5, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws InstantiationException, IllegalAccessException,
|
public static void main(String[] args) throws InstantiationException, IllegalAccessException,
|
||||||
ResourceException, ScriptException, IOException, javax.script.ScriptException {
|
ResourceException, ScriptException, IOException, javax.script.ScriptException {
|
||||||
MyJointCompilationApp myJointCompilationApp = new MyJointCompilationApp();
|
MyJointCompilationApp myJointCompilationApp = new MyJointCompilationApp();
|
||||||
myJointCompilationApp.runStaticCompiledClasses();
|
LOG.info("Example of addition operation via Groovy scripts integration with Java.");
|
||||||
myJointCompilationApp.runDynamicCompiledClasses();
|
myJointCompilationApp.addWithStaticCompiledClasses();
|
||||||
|
myJointCompilationApp.addWithDynamicCompiledClasses();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user