BAEL-2899: separating static and dynamic classes examples
This commit is contained in:
parent
653717e25e
commit
b9999c1af1
@ -4,14 +4,14 @@ import groovy.lang.*;
|
|||||||
import groovy.util.GroovyScriptEngine;
|
import groovy.util.GroovyScriptEngine;
|
||||||
import groovy.util.ResourceException;
|
import groovy.util.ResourceException;
|
||||||
import groovy.util.ScriptException;
|
import groovy.util.ScriptException;
|
||||||
import org.codehaus.groovy.control.CompilerConfiguration;
|
|
||||||
import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
|
import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.script.Compilable;
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
@ -24,83 +24,76 @@ public class App {
|
|||||||
private final GroovyClassLoader loader;
|
private final GroovyClassLoader loader;
|
||||||
private final GroovyShell shell;
|
private final GroovyShell shell;
|
||||||
private final GroovyScriptEngine engine;
|
private final GroovyScriptEngine engine;
|
||||||
|
private final ScriptEngine engineFromFactory;
|
||||||
|
|
||||||
private App() throws IOException {
|
private App() throws IOException {
|
||||||
loader = new GroovyClassLoader(this.getClass().getClassLoader());
|
loader = new GroovyClassLoader(this.getClass().getClassLoader());
|
||||||
CompilerConfiguration config = new CompilerConfiguration();
|
shell = new GroovyShell(loader, new Binding());
|
||||||
config.setScriptBaseClass("com.baeldung.CalcScript");
|
|
||||||
shell = new GroovyShell(loader, new Binding(), config);
|
|
||||||
engine = new GroovyScriptEngine(new URL[] { new File("src/main/groovy/com/baeldung/").toURI().toURL() }, this.getClass().getClassLoader());
|
engine = new GroovyScriptEngine(new URL[] { new File("src/main/groovy/com/baeldung/").toURI().toURL() }, this.getClass().getClassLoader());
|
||||||
|
engineFromFactory = new GroovyScriptEngineFactory().getScriptEngine();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runCompiledClasses(int x, int y) {
|
private void runCompiledClasses(int x, int y) {
|
||||||
|
LOG.info("Executing {} + {}", x, y);
|
||||||
Object result1 = new CalcScript().calcSum(x, y);
|
Object result1 = new CalcScript().calcSum(x, y);
|
||||||
LOG.info("Result of calcSum() method is {}", result1);
|
LOG.info("Result of CalcScript.calcSum() method is {}", result1);
|
||||||
|
|
||||||
Object result2 = new CalcMath().calcSum(x, y);
|
Object result2 = new CalcMath().calcSum(x, y);
|
||||||
LOG.info("Result of calcSum() method is {}", result2);
|
LOG.info("Result of CalcMath.calcSum() method is {}", result2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runShellScript(int x, int y) {
|
private void runDynamicShellScript(int x, int y) throws IOException {
|
||||||
Script script = shell.parse(String.format("calcSum(%d,%d)", x, y));
|
Script script = shell.parse(new File("src/main/groovy/com/baeldung/", "CalcScript.groovy"));
|
||||||
assert script instanceof CalcScript;
|
LOG.info("Executing {} + {}", x, y);
|
||||||
Object result = script.run();
|
Object result = script.invokeMethod("calcSum", new Object[] { x, y });
|
||||||
LOG.info("Result of run() method is {}", result);
|
LOG.info("Result of CalcScript.calcSum() method is {}", result);
|
||||||
|
|
||||||
Object script2 = shell.parse("CalcScript");
|
|
||||||
assert script2 instanceof CalcScript;
|
|
||||||
Object result2 = ((CalcScript) script2).calcSum(x + 7, y + 7);
|
|
||||||
LOG.info("Result of calcSum() method is {}", result2);
|
|
||||||
|
|
||||||
Script script3 = shell.parse("");
|
|
||||||
assert script3 instanceof CalcScript;
|
|
||||||
Object result3 = script3.invokeMethod("calcSum", new Object[] { x + 14, y + 14 });
|
|
||||||
LOG.info("Result of run() method is {}", result3);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runClassWithLoader(int x, int y) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
|
private void runDynamicClassWithLoader(int x, int y) throws IllegalAccessException, InstantiationException, IOException {
|
||||||
Class calcClass = loader.loadClass("com.baeldung.CalcMath");
|
Class calcClass = loader.parseClass(new File("src/main/groovy/com/baeldung/", "CalcMath.groovy"));
|
||||||
Object calc = calcClass.newInstance();
|
Object calc = calcClass.newInstance();
|
||||||
assert calc instanceof CalcMath;
|
Object result = ((GroovyObject) calc).invokeMethod("calcSum", new Object[] { x + 14, y + 14 });
|
||||||
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||||
Object result = ((CalcMath) calc).calcSum(x, y);
|
|
||||||
LOG.info("Result is {}", result);
|
|
||||||
|
|
||||||
Object result2 = ((GroovyObject) calc).invokeMethod("calcSum", new Object[] { x + 14, y + 14 });
|
|
||||||
LOG.info("Result is {}", result2);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runClassWithEngine(int x, int y) throws ClassNotFoundException, IllegalAccessException, InstantiationException, ResourceException, ScriptException {
|
private void runDynamicClassWithEngine(int x, int y) throws IllegalAccessException, 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();
|
||||||
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
|
||||||
//WARNING the following will throw a ClassCastException
|
//WARNING the following will throw a ClassCastException
|
||||||
//((CalcMath)calc).calcSum(1,2);
|
//((CalcMath)calc).calcSum(1,2);
|
||||||
LOG.info("Result is {}", result);
|
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
||||||
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runClassWithEngineFactory(int x, int y) throws ClassNotFoundException, IllegalAccessException, InstantiationException, ResourceException, ScriptException, javax.script.ScriptException {
|
private void runDynamicClassWithEngineFactory(int x, int y) throws IllegalAccessException, InstantiationException, javax.script.ScriptException, FileNotFoundException {
|
||||||
ScriptEngine engine = new GroovyScriptEngineFactory().getScriptEngine();
|
Class calcClas = (Class) engineFromFactory.eval(new FileReader(new File("src/main/groovy/com/baeldung/", "CalcMath.groovy")));
|
||||||
Class calcClass = (Class) ((Compilable) engine).compile("com.baeldung.CalcMath").eval();
|
GroovyObject calc = (GroovyObject) calcClas.newInstance();
|
||||||
Object calc = calcClass.newInstance();
|
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
||||||
Object result = ((CalcMath) calc).calcSum(1, 20);
|
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||||
LOG.info("Result 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ResourceException, ScriptException, IOException, javax.script.ScriptException {
|
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ResourceException, ScriptException, IOException, javax.script.ScriptException {
|
||||||
App app = new App();
|
App app = new App();
|
||||||
LOG.info("Running an already compiled groovy class instance...");
|
app.runStaticCompiledClasses();
|
||||||
app.runCompiledClasses(5, 10);
|
app.runDynamicCompiledClasses();
|
||||||
LOG.info("Running a groovy script...");
|
|
||||||
app.runShellScript(5, 10);
|
|
||||||
LOG.info("Running a groovy class...");
|
|
||||||
app.runClassWithLoader(1, 3);
|
|
||||||
LOG.info("Running a groovy class using the engine...");
|
|
||||||
app.runClassWithEngine(10, 30);
|
|
||||||
LOG.info("Running a groovy class using the engine factory...");
|
|
||||||
app.runClassWithEngineFactory(10, 30);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user