Last active
December 9, 2021 03:51
-
-
Save RomiTT/8cc98ea7c1a7d1dd34b4840c7c67b027 to your computer and use it in GitHub Desktop.
Java with Groovy script #java #groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hello.hellospring.controller; | |
import groovy.lang.*; | |
import groovy.util.GroovyScriptEngine; | |
import hello.hellospring.scriptRoots.ScriptRoot1; | |
import hello.hellospring.util.TaxCalculator; | |
import org.codehaus.groovy.control.CompilerConfiguration; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import java.io.File; | |
import java.math.BigDecimal; | |
import java.net.URL; | |
@Controller | |
public class HomeController { | |
static class MyClosure extends Closure { | |
protected HomeController controller; | |
public MyClosure(Object owner) { | |
super(owner); | |
} | |
void setController(HomeController controller) { | |
this.controller = controller; | |
} | |
@Override | |
public Object call(Object... args) { | |
return this.controller.getStringVar() + "-" + args[0] + args[1]; | |
} | |
} | |
private String stringVar = "HomeController"; | |
public String getStringVar() { | |
return stringVar; | |
} | |
@GetMapping("/") | |
public String home() throws Exception { | |
URL url = new File("src/groovy/").toURI().toURL(); | |
GroovyScriptEngine engine = new GroovyScriptEngine(new URL[]{url}, this.getClass().getClassLoader()); | |
Class GreeterClass = engine.loadScriptByName("Test.groovy"); | |
GroovyObject greeter = (GroovyObject) GreeterClass.getConstructor().newInstance(); | |
MyClosure callback = new MyClosure(this); | |
callback.setController(this); | |
greeter.setProperty("a", 0.7); | |
greeter.setProperty("b", 15.9); | |
greeter.setProperty("callback", callback); | |
TaxCalculator calculator = (TaxCalculator) greeter.getProperty("calculator"); | |
calculator.setHomeController(this); | |
String result = (String) greeter.invokeMethod("sayHello", null); | |
Double doubleResult = (Double) greeter.invokeMethod("plus", null); | |
result = (String) greeter.invokeMethod("callCallback", new Object[]{"StringValue", 12.0}); | |
BigDecimal tax = (BigDecimal) greeter.invokeMethod("세금계산", new Object[]{5500000.0}); | |
CompilerConfiguration configuration = new CompilerConfiguration(); | |
configuration.setScriptBaseClass(ScriptRoot1.class.getName()); | |
GroovyShell shell = new GroovyShell(this.getClass().getClassLoader(), new Binding(), configuration); | |
String strScriptCode = "a = false;\n if (a) { 세금계산(8500000.0); }\n else { 0.0; }"; | |
Script script = shell.parse(strScriptCode); | |
tax = (BigDecimal)script.run(); | |
return "home"; | |
} | |
} | |
// src/groovy/Test.groovy | |
import hello.hellospring.util.*; | |
class Greeter { | |
double a; | |
double b; | |
Closure callback; | |
TaxCalculator calculator = new TaxCalculator(); | |
String sayHello() { | |
def greet = "Hello, world!" | |
greet | |
} | |
double plus() { | |
return a + b; | |
} | |
String callCallback(String a, double b) { | |
return callback(a, b); | |
} | |
BigDecimal 세금계산(BigDecimal value) { | |
return calculator.calc(value); | |
} | |
} | |
// // src/main/hello.hellospring.scriptRoots/ScriptRoot1.java | |
package hello.hellospring.scriptRoots; | |
import groovy.lang.Script; | |
import hello.hellospring.util.TaxCalculator; | |
import java.math.BigDecimal; | |
public abstract class ScriptRoot1 extends Script { | |
TaxCalculator calculator = new TaxCalculator(); | |
BigDecimal 공제금액 = new BigDecimal(0); | |
BigDecimal 세금계산(BigDecimal value) { | |
return calculator.calc(value); | |
} | |
} | |
// src/main/hello.hellospring.util/TaxCalculator.java | |
package hello.hellospring.util; | |
import hello.hellospring.controller.HomeController; | |
import java.math.BigDecimal; | |
public class TaxCalculator { | |
private BigDecimal taxRate = new BigDecimal(0.3); | |
private HomeController homeController; | |
public void setTaxRate(BigDecimal rate) { | |
taxRate = rate; | |
} | |
public void setHomeController(HomeController controller) { | |
homeController = controller; | |
} | |
public BigDecimal calc(BigDecimal value) { | |
return value.multiply(taxRate); | |
} | |
} | |
// build.gradle | |
// https://mvnrepository.com/artifact/org.codehaus.groovy/groovy | |
implementation group: 'org.codehaus.groovy', name: 'groovy', version: '3.0.4' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment