From 7c3082a0930e7937c0507021fc0ca6a2d25e1d0a Mon Sep 17 00:00:00 2001 From: Gian Mario Contessa Date: Fri, 24 May 2019 08:09:59 +0100 Subject: [PATCH] BAEL-2899: project structure and example classes --- java-groovy-joint/pom.xml | 132 ++++++++++++++++++ .../main/groovy/com.baeldung/CalcMath.groovy | 19 +++ .../groovy/com.baeldung/CalcScript.groovy | 19 +++ .../src/main/java/com/baeldung/App.java | 54 +++++++ 4 files changed, 224 insertions(+) create mode 100644 java-groovy-joint/pom.xml create mode 100644 java-groovy-joint/src/main/groovy/com.baeldung/CalcMath.groovy create mode 100644 java-groovy-joint/src/main/groovy/com.baeldung/CalcScript.groovy create mode 100644 java-groovy-joint/src/main/java/com/baeldung/App.java diff --git a/java-groovy-joint/pom.xml b/java-groovy-joint/pom.xml new file mode 100644 index 0000000000..67365cf999 --- /dev/null +++ b/java-groovy-joint/pom.xml @@ -0,0 +1,132 @@ + + + + 4.0.0 + java-groovy-joint + 0.1.0-SNAPSHOT + java-groovy-joint + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + UTF-8 + 3.9 + 1.8 + 3.8.1 + 1.2.3 + 2.5.7 + + + + + bintray + Groovy Bintray + https://dl.bintray.com/groovy/maven + + never + + + false + + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + org.codehaus.groovy + groovy-all + ${groovy.version} + pom + + + junit + junit + 4.11 + test + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.2 + + + + com.baeldung.App + true + + + + + + maven-compiler-plugin + 3.8.0 + + groovy-eclipse-compiler + ${java.version} + ${java.version} + + + + org.codehaus.groovy + groovy-eclipse-compiler + 3.3.0-01 + + + org.codehaus.groovy + groovy-eclipse-batch + ${groovy.version}-01 + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.0 + + + + jar-with-dependencies + + + + + com.baeldung.App + + + + + + + make-assembly + + package + + single + + + + + + + + + diff --git a/java-groovy-joint/src/main/groovy/com.baeldung/CalcMath.groovy b/java-groovy-joint/src/main/groovy/com.baeldung/CalcMath.groovy new file mode 100644 index 0000000000..d9709653c7 --- /dev/null +++ b/java-groovy-joint/src/main/groovy/com.baeldung/CalcMath.groovy @@ -0,0 +1,19 @@ +package com.baeldung + +import org.slf4j.LoggerFactory + +class CalcMath { + def log = LoggerFactory.getLogger(this.getClass()) + + def calcSum(x, y) { + log.info "Executing $x + $y" + x + y + } + + def calcSum2(x, y) { + log.info "Executing $x + $y" + // DANGER! This won't throw a compilation issue and fail only at runtime!!! + calcSum3() + log.info("Logging an undefined variable: $z") + } +} \ No newline at end of file diff --git a/java-groovy-joint/src/main/groovy/com.baeldung/CalcScript.groovy b/java-groovy-joint/src/main/groovy/com.baeldung/CalcScript.groovy new file mode 100644 index 0000000000..2278f0dab8 --- /dev/null +++ b/java-groovy-joint/src/main/groovy/com.baeldung/CalcScript.groovy @@ -0,0 +1,19 @@ +package com.baeldung + +import org.slf4j.LoggerFactory + +abstract class CalcScript extends Script { + def log = LoggerFactory.getLogger(this.getClass()) + + def calcSum(x, y) { + log.info "Executing $x + $y" + x + y + } + + def calcSum2(x, y) { + log.info "Executing $x + $y" + // DANGER! This won't throw a compilation issue and fail only at runtime!!! + calcSum3() + log.info("Logging an undefined variable: $z") + } +} \ No newline at end of file diff --git a/java-groovy-joint/src/main/java/com/baeldung/App.java b/java-groovy-joint/src/main/java/com/baeldung/App.java new file mode 100644 index 0000000000..0a201d12e2 --- /dev/null +++ b/java-groovy-joint/src/main/java/com/baeldung/App.java @@ -0,0 +1,54 @@ +package com.baeldung; + +import groovy.lang.Binding; +import groovy.lang.GroovyClassLoader; +import groovy.lang.GroovyShell; +import org.codehaus.groovy.control.CompilerConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Hello world! + * + */ +public class App { + private final static Logger LOG = LoggerFactory.getLogger(App.class); + private final GroovyClassLoader loader; + private final GroovyShell shell; + + private App() { + loader = new GroovyClassLoader(this.getClass().getClassLoader()); + CompilerConfiguration config = new CompilerConfiguration(); + config.setScriptBaseClass("com.baeldung.CalcScript"); + shell = new GroovyShell(loader, new Binding(), config); + } + + private void runScript(int x, int y) { + Object script = shell.parse(String.format("calcSum(%d,%d)", x, y)); + assert script instanceof CalcScript; + Object result = ((CalcScript) script).run(); + LOG.info("Result of run() method is {}", result); + + Object script2 = shell.parse("CalcScript"); + Object result2 = ((CalcScript) script2).calcSum(x + 7, y + 7); + LOG.info("Result of calcSum() method is {}", result2); + + } + + private void runClass(int x, int y) throws ClassNotFoundException, IllegalAccessException, InstantiationException { + Class calcClass = loader.loadClass("com.baeldung.CalcMath"); + Object calc = calcClass.newInstance(); + assert calc instanceof CalcMath; + + Object result = ((CalcMath) calc).calcSum(x, y); + LOG.info("Result is {}", result); + } + + public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { + App app = new App(); + LOG.info("Running a groovy script..."); + app.runScript(5, 10); + LOG.info("Running a groovy class..."); + app.runClass(1, 3); + } +}