mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
change line sep
This commit is contained in:
@@ -1,56 +1,56 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-parent</artifactId>
|
||||
<version>5.5.8-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>hutool-script</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>Hutool 脚本执行封装</description>
|
||||
|
||||
<properties>
|
||||
<jython.version>2.7.2</jython.version>
|
||||
<luaj.version>3.0.1</luaj.version>
|
||||
<groovy.version>3.0.7</groovy.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.python</groupId>
|
||||
<artifactId>jython</artifactId>
|
||||
<version>${jython.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.luaj</groupId>
|
||||
<artifactId>luaj-jse</artifactId>
|
||||
<version>${luaj.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>provided</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>junit</artifactId>
|
||||
<groupId>junit</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-parent</artifactId>
|
||||
<version>5.5.8-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>hutool-script</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>Hutool 脚本执行封装</description>
|
||||
|
||||
<properties>
|
||||
<jython.version>2.7.2</jython.version>
|
||||
<luaj.version>3.0.1</luaj.version>
|
||||
<groovy.version>3.0.7</groovy.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.python</groupId>
|
||||
<artifactId>jython</artifactId>
|
||||
<version>${jython.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.luaj</groupId>
|
||||
<artifactId>luaj-jse</artifactId>
|
||||
<version>${luaj.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>provided</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>junit</artifactId>
|
||||
<groupId>junit</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -1,159 +1,159 @@
|
||||
package cn.hutool.script;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 全功能引擎类,支持Compilable和Invocable
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class FullSupportScriptEngine implements ScriptEngine, Compilable, Invocable {
|
||||
|
||||
ScriptEngine engine;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param engine 脚本引擎
|
||||
*/
|
||||
public FullSupportScriptEngine(ScriptEngine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名或者脚本语言扩展名或者MineType
|
||||
*/
|
||||
public FullSupportScriptEngine(String nameOrExtOrMime) {
|
||||
final ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine engine = manager.getEngineByName(nameOrExtOrMime);
|
||||
if (null == engine) {
|
||||
engine = manager.getEngineByExtension(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
engine = manager.getEngineByMimeType(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
throw new NullPointerException(StrUtil.format("Script for [{}] not support !", nameOrExtOrMime));
|
||||
}
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- Invocable
|
||||
@Override
|
||||
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeMethod(thiz, name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeFunction(name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(clasz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Object thiz, Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(thiz, clasz);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- Compilable
|
||||
@Override
|
||||
public CompiledScript compile(String script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompiledScript compile(Reader script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- ScriptEngine
|
||||
@Override
|
||||
public Object eval(String script, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(script, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(reader, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script) throws ScriptException {
|
||||
return engine.eval(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader) throws ScriptException {
|
||||
return engine.eval(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script, Bindings n) throws ScriptException {
|
||||
return engine.eval(script, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, Bindings n) throws ScriptException {
|
||||
return engine.eval(reader, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, Object value) {
|
||||
engine.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String key) {
|
||||
return engine.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings getBindings(int scope) {
|
||||
return engine.getBindings(scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBindings(Bindings bindings, int scope) {
|
||||
engine.setBindings(bindings, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings createBindings() {
|
||||
return engine.createBindings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptContext getContext() {
|
||||
return engine.getContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(ScriptContext context) {
|
||||
engine.setContext(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngineFactory getFactory() {
|
||||
return engine.getFactory();
|
||||
}
|
||||
|
||||
}
|
||||
package cn.hutool.script;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 全功能引擎类,支持Compilable和Invocable
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class FullSupportScriptEngine implements ScriptEngine, Compilable, Invocable {
|
||||
|
||||
ScriptEngine engine;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param engine 脚本引擎
|
||||
*/
|
||||
public FullSupportScriptEngine(ScriptEngine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名或者脚本语言扩展名或者MineType
|
||||
*/
|
||||
public FullSupportScriptEngine(String nameOrExtOrMime) {
|
||||
final ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine engine = manager.getEngineByName(nameOrExtOrMime);
|
||||
if (null == engine) {
|
||||
engine = manager.getEngineByExtension(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
engine = manager.getEngineByMimeType(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
throw new NullPointerException(StrUtil.format("Script for [{}] not support !", nameOrExtOrMime));
|
||||
}
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- Invocable
|
||||
@Override
|
||||
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeMethod(thiz, name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeFunction(name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(clasz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Object thiz, Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(thiz, clasz);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- Compilable
|
||||
@Override
|
||||
public CompiledScript compile(String script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompiledScript compile(Reader script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- ScriptEngine
|
||||
@Override
|
||||
public Object eval(String script, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(script, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(reader, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script) throws ScriptException {
|
||||
return engine.eval(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader) throws ScriptException {
|
||||
return engine.eval(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script, Bindings n) throws ScriptException {
|
||||
return engine.eval(script, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, Bindings n) throws ScriptException {
|
||||
return engine.eval(reader, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, Object value) {
|
||||
engine.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String key) {
|
||||
return engine.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings getBindings(int scope) {
|
||||
return engine.getBindings(scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBindings(Bindings bindings, int scope) {
|
||||
engine.setBindings(bindings, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings createBindings() {
|
||||
return engine.createBindings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptContext getContext() {
|
||||
return engine.getContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(ScriptContext context) {
|
||||
engine.setContext(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngineFactory getFactory() {
|
||||
return engine.getFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,135 +1,135 @@
|
||||
package cn.hutool.script;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* Javascript引擎类
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class JavaScriptEngine extends FullSupportScriptEngine {
|
||||
|
||||
public JavaScriptEngine() {
|
||||
super(ScriptUtil.createJsEngine());
|
||||
}
|
||||
|
||||
/**
|
||||
* 引擎实例
|
||||
*
|
||||
* @return 引擎实例
|
||||
*/
|
||||
public static JavaScriptEngine instance() {
|
||||
return new JavaScriptEngine();
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- Invocable
|
||||
@Override
|
||||
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeMethod(thiz, name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeFunction(name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(clasz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Object thiz, Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(thiz, clasz);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- Compilable
|
||||
@Override
|
||||
public CompiledScript compile(String script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompiledScript compile(Reader script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- ScriptEngine
|
||||
@Override
|
||||
public Object eval(String script, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(script, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(reader, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script) throws ScriptException {
|
||||
return engine.eval(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader) throws ScriptException {
|
||||
return engine.eval(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script, Bindings n) throws ScriptException {
|
||||
return engine.eval(script, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, Bindings n) throws ScriptException {
|
||||
return engine.eval(reader, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, Object value) {
|
||||
engine.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String key) {
|
||||
return engine.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings getBindings(int scope) {
|
||||
return engine.getBindings(scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBindings(Bindings bindings, int scope) {
|
||||
engine.setBindings(bindings, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings createBindings() {
|
||||
return engine.createBindings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptContext getContext() {
|
||||
return engine.getContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(ScriptContext context) {
|
||||
engine.setContext(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngineFactory getFactory() {
|
||||
return engine.getFactory();
|
||||
}
|
||||
|
||||
}
|
||||
package cn.hutool.script;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* Javascript引擎类
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class JavaScriptEngine extends FullSupportScriptEngine {
|
||||
|
||||
public JavaScriptEngine() {
|
||||
super(ScriptUtil.createJsEngine());
|
||||
}
|
||||
|
||||
/**
|
||||
* 引擎实例
|
||||
*
|
||||
* @return 引擎实例
|
||||
*/
|
||||
public static JavaScriptEngine instance() {
|
||||
return new JavaScriptEngine();
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- Invocable
|
||||
@Override
|
||||
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeMethod(thiz, name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeFunction(name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(clasz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Object thiz, Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(thiz, clasz);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- Compilable
|
||||
@Override
|
||||
public CompiledScript compile(String script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompiledScript compile(Reader script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- ScriptEngine
|
||||
@Override
|
||||
public Object eval(String script, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(script, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(reader, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script) throws ScriptException {
|
||||
return engine.eval(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader) throws ScriptException {
|
||||
return engine.eval(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script, Bindings n) throws ScriptException {
|
||||
return engine.eval(script, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, Bindings n) throws ScriptException {
|
||||
return engine.eval(reader, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, Object value) {
|
||||
engine.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String key) {
|
||||
return engine.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings getBindings(int scope) {
|
||||
return engine.getBindings(scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBindings(Bindings bindings, int scope) {
|
||||
engine.setBindings(bindings, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings createBindings() {
|
||||
return engine.createBindings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptContext getContext() {
|
||||
return engine.getContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(ScriptContext context) {
|
||||
engine.setContext(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngineFactory getFactory() {
|
||||
return engine.getFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,126 +1,126 @@
|
||||
package cn.hutool.script;
|
||||
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本运行时异常
|
||||
*
|
||||
* @author xiaoleilu
|
||||
*/
|
||||
public class ScriptRuntimeException extends RuntimeException {
|
||||
private static final long serialVersionUID = 8247610319171014183L;
|
||||
|
||||
private String fileName;
|
||||
private int lineNumber = -1;
|
||||
private int columnNumber = -1;
|
||||
|
||||
public ScriptRuntimeException(Throwable e) {
|
||||
super(ExceptionUtil.getMessage(e), e);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String messageTemplate, Object... params) {
|
||||
super(StrUtil.format(messageTemplate, params));
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(Throwable throwable, String messageTemplate, Object... params) {
|
||||
super(StrUtil.format(messageTemplate, params), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a <code>ScriptException</code> with message, filename and linenumber to be used in error messages.
|
||||
*
|
||||
* @param message The string to use in the message
|
||||
*
|
||||
* @param fileName The file or resource name describing the location of a script error causing the <code>ScriptException</code> to be thrown.
|
||||
*
|
||||
* @param lineNumber A line number describing the location of a script error causing the <code>ScriptException</code> to be thrown.
|
||||
*/
|
||||
public ScriptRuntimeException(String message, String fileName, int lineNumber) {
|
||||
super(message);
|
||||
this.fileName = fileName;
|
||||
this.lineNumber = lineNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>ScriptException</code> constructor specifying message, filename, line number and column number.
|
||||
*
|
||||
* @param message The message.
|
||||
* @param fileName The filename
|
||||
* @param lineNumber the line number.
|
||||
* @param columnNumber the column number.
|
||||
*/
|
||||
public ScriptRuntimeException(String message, String fileName, int lineNumber, int columnNumber) {
|
||||
super(message);
|
||||
this.fileName = fileName;
|
||||
this.lineNumber = lineNumber;
|
||||
this.columnNumber = columnNumber;
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(ScriptException e) {
|
||||
super(e);
|
||||
this.fileName = e.getFileName();
|
||||
this.lineNumber = e.getLineNumber();
|
||||
this.columnNumber = e.getColumnNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a message containing the String passed to a constructor as well as line and column numbers and filename if any of these are known.
|
||||
*
|
||||
* @return The error message.
|
||||
*/
|
||||
@Override
|
||||
public String getMessage() {
|
||||
String ret = super.getMessage();
|
||||
if (fileName != null) {
|
||||
ret += (" in " + fileName);
|
||||
if (lineNumber != -1) {
|
||||
ret += " at line number " + lineNumber;
|
||||
}
|
||||
|
||||
if (columnNumber != -1) {
|
||||
ret += " at column number " + columnNumber;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the line number on which an error occurred.
|
||||
*
|
||||
* @return The line number. Returns -1 if a line number is unavailable.
|
||||
*/
|
||||
public int getLineNumber() {
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the column number on which an error occurred.
|
||||
*
|
||||
* @return The column number. Returns -1 if a column number is unavailable.
|
||||
*/
|
||||
public int getColumnNumber() {
|
||||
return columnNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source of the script causing the error.
|
||||
*
|
||||
* @return The file name of the script or some other string describing the script source. May return some implementation-defined string such as <i><unknown></i> if a description of the
|
||||
* source is unavailable.
|
||||
*/
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
package cn.hutool.script;
|
||||
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本运行时异常
|
||||
*
|
||||
* @author xiaoleilu
|
||||
*/
|
||||
public class ScriptRuntimeException extends RuntimeException {
|
||||
private static final long serialVersionUID = 8247610319171014183L;
|
||||
|
||||
private String fileName;
|
||||
private int lineNumber = -1;
|
||||
private int columnNumber = -1;
|
||||
|
||||
public ScriptRuntimeException(Throwable e) {
|
||||
super(ExceptionUtil.getMessage(e), e);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String messageTemplate, Object... params) {
|
||||
super(StrUtil.format(messageTemplate, params));
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(Throwable throwable, String messageTemplate, Object... params) {
|
||||
super(StrUtil.format(messageTemplate, params), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a <code>ScriptException</code> with message, filename and linenumber to be used in error messages.
|
||||
*
|
||||
* @param message The string to use in the message
|
||||
*
|
||||
* @param fileName The file or resource name describing the location of a script error causing the <code>ScriptException</code> to be thrown.
|
||||
*
|
||||
* @param lineNumber A line number describing the location of a script error causing the <code>ScriptException</code> to be thrown.
|
||||
*/
|
||||
public ScriptRuntimeException(String message, String fileName, int lineNumber) {
|
||||
super(message);
|
||||
this.fileName = fileName;
|
||||
this.lineNumber = lineNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>ScriptException</code> constructor specifying message, filename, line number and column number.
|
||||
*
|
||||
* @param message The message.
|
||||
* @param fileName The filename
|
||||
* @param lineNumber the line number.
|
||||
* @param columnNumber the column number.
|
||||
*/
|
||||
public ScriptRuntimeException(String message, String fileName, int lineNumber, int columnNumber) {
|
||||
super(message);
|
||||
this.fileName = fileName;
|
||||
this.lineNumber = lineNumber;
|
||||
this.columnNumber = columnNumber;
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(ScriptException e) {
|
||||
super(e);
|
||||
this.fileName = e.getFileName();
|
||||
this.lineNumber = e.getLineNumber();
|
||||
this.columnNumber = e.getColumnNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a message containing the String passed to a constructor as well as line and column numbers and filename if any of these are known.
|
||||
*
|
||||
* @return The error message.
|
||||
*/
|
||||
@Override
|
||||
public String getMessage() {
|
||||
String ret = super.getMessage();
|
||||
if (fileName != null) {
|
||||
ret += (" in " + fileName);
|
||||
if (lineNumber != -1) {
|
||||
ret += " at line number " + lineNumber;
|
||||
}
|
||||
|
||||
if (columnNumber != -1) {
|
||||
ret += " at column number " + columnNumber;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the line number on which an error occurred.
|
||||
*
|
||||
* @return The line number. Returns -1 if a line number is unavailable.
|
||||
*/
|
||||
public int getLineNumber() {
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the column number on which an error occurred.
|
||||
*
|
||||
* @return The column number. Returns -1 if a column number is unavailable.
|
||||
*/
|
||||
public int getColumnNumber() {
|
||||
return columnNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source of the script causing the error.
|
||||
*
|
||||
* @return The file name of the script or some other string describing the script source. May return some implementation-defined string such as <i><unknown></i> if a description of the
|
||||
* source is unavailable.
|
||||
*/
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
|
@@ -1,281 +1,281 @@
|
||||
package cn.hutool.script;
|
||||
|
||||
import cn.hutool.core.lang.SimpleCache;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本工具类
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class ScriptUtil {
|
||||
|
||||
private static final ScriptEngineManager MANAGER = new ScriptEngineManager();
|
||||
private static final SimpleCache<String, ScriptEngine> CACHE = new SimpleCache<>();
|
||||
|
||||
/**
|
||||
* 获得单例的{@link ScriptEngine} 实例
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
*/
|
||||
public static ScriptEngine getScript(String nameOrExtOrMime) {
|
||||
return CACHE.get(nameOrExtOrMime, () -> createScript(nameOrExtOrMime));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 {@link ScriptEngine} 实例
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createScript(String nameOrExtOrMime) {
|
||||
ScriptEngine engine = MANAGER.getEngineByName(nameOrExtOrMime);
|
||||
if (null == engine) {
|
||||
engine = MANAGER.getEngineByExtension(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
engine = MANAGER.getEngineByMimeType(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
throw new NullPointerException(StrUtil.format("Script for [{}] not support !", nameOrExtOrMime));
|
||||
}
|
||||
return engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得非单例的 Javascript引擎 {@link JavaScriptEngine}
|
||||
*
|
||||
* @return {@link JavaScriptEngine}
|
||||
*/
|
||||
public static JavaScriptEngine getJavaScriptEngine() {
|
||||
return new JavaScriptEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的JavaScript引擎
|
||||
*
|
||||
* @return Javascript引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getJsEngine() {
|
||||
return getScript("js");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的JavaScript引擎
|
||||
*
|
||||
* @return Javascript引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createJsEngine() {
|
||||
return createScript("js");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Python引擎<br>
|
||||
* 需要引入org.python:jython
|
||||
*
|
||||
* @return Python引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getPythonEngine() {
|
||||
System.setProperty("python.import.site", "false");
|
||||
return getScript("python");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Python引擎<br>
|
||||
* 需要引入org.python:jython
|
||||
*
|
||||
* @return Python引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createPythonEngine() {
|
||||
System.setProperty("python.import.site", "false");
|
||||
return createScript("python");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Lua引擎<br>
|
||||
* 需要引入org.luaj:luaj-jse
|
||||
*
|
||||
* @return Lua引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getLuaEngine() {
|
||||
return getScript("lua");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Lua引擎<br>
|
||||
* 需要引入org.luaj:luaj-jse
|
||||
*
|
||||
* @return Lua引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createLuaEngine() {
|
||||
return createScript("lua");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Groovy引擎<br>
|
||||
* 需要引入org.codehaus.groovy:groovy-all
|
||||
*
|
||||
* @return Groovy引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getGroovyEngine() {
|
||||
return getScript("groovy");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Groovy引擎<br>
|
||||
* 需要引入org.codehaus.groovy:groovy-all
|
||||
*
|
||||
* @return Groovy引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createGroovyEngine() {
|
||||
return createScript("groovy");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Javascript脚本,返回Invocable,此方法分为两种情况:
|
||||
*
|
||||
* <ol>
|
||||
* <li>执行的脚本返回值是可执行的脚本方法</li>
|
||||
* <li>脚本为函数库,则ScriptEngine本身为可执行方法</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return 执行结果
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static Invocable evalInvocable(String script) throws ScriptRuntimeException {
|
||||
final ScriptEngine jsEngine = getJsEngine();
|
||||
final Object eval;
|
||||
try {
|
||||
eval = jsEngine.eval(script);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
if(eval instanceof Invocable){
|
||||
return (Invocable)eval;
|
||||
} else if(jsEngine instanceof Invocable){
|
||||
return (Invocable)jsEngine;
|
||||
}
|
||||
throw new ScriptRuntimeException("Script is not invocable !");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的Javascript脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return 执行结果
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script) throws ScriptRuntimeException {
|
||||
try {
|
||||
return getJsEngine().eval(script);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param context 脚本上下文
|
||||
* @return 执行结果
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script, ScriptContext context) throws ScriptRuntimeException {
|
||||
try {
|
||||
return getJsEngine().eval(script, context);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param bindings 绑定的参数
|
||||
* @return 执行结果
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script, Bindings bindings) throws ScriptRuntimeException {
|
||||
try {
|
||||
return getJsEngine().eval(script, bindings);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行JS脚本中的指定方法
|
||||
*
|
||||
* @param script js脚本
|
||||
* @param func 方法名
|
||||
* @param args 方法参数
|
||||
* @return 结果
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static Object invoke(String script, String func, Object... args) {
|
||||
final Invocable eval = evalInvocable(script);
|
||||
try {
|
||||
return eval.invokeFunction(func, args);
|
||||
} catch (ScriptException | NoSuchMethodException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译Javascript脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static CompiledScript compile(String script) throws ScriptRuntimeException {
|
||||
try {
|
||||
return compile(getJsEngine(), script);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译Javascript脚本
|
||||
*
|
||||
* @param engine 引擎
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptException 脚本异常
|
||||
*/
|
||||
public static CompiledScript compile(ScriptEngine engine, String script) throws ScriptException {
|
||||
if (engine instanceof Compilable) {
|
||||
final Compilable compEngine = (Compilable) engine;
|
||||
return compEngine.compile(script);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
package cn.hutool.script;
|
||||
|
||||
import cn.hutool.core.lang.SimpleCache;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本工具类
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class ScriptUtil {
|
||||
|
||||
private static final ScriptEngineManager MANAGER = new ScriptEngineManager();
|
||||
private static final SimpleCache<String, ScriptEngine> CACHE = new SimpleCache<>();
|
||||
|
||||
/**
|
||||
* 获得单例的{@link ScriptEngine} 实例
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
*/
|
||||
public static ScriptEngine getScript(String nameOrExtOrMime) {
|
||||
return CACHE.get(nameOrExtOrMime, () -> createScript(nameOrExtOrMime));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 {@link ScriptEngine} 实例
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createScript(String nameOrExtOrMime) {
|
||||
ScriptEngine engine = MANAGER.getEngineByName(nameOrExtOrMime);
|
||||
if (null == engine) {
|
||||
engine = MANAGER.getEngineByExtension(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
engine = MANAGER.getEngineByMimeType(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
throw new NullPointerException(StrUtil.format("Script for [{}] not support !", nameOrExtOrMime));
|
||||
}
|
||||
return engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得非单例的 Javascript引擎 {@link JavaScriptEngine}
|
||||
*
|
||||
* @return {@link JavaScriptEngine}
|
||||
*/
|
||||
public static JavaScriptEngine getJavaScriptEngine() {
|
||||
return new JavaScriptEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的JavaScript引擎
|
||||
*
|
||||
* @return Javascript引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getJsEngine() {
|
||||
return getScript("js");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的JavaScript引擎
|
||||
*
|
||||
* @return Javascript引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createJsEngine() {
|
||||
return createScript("js");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Python引擎<br>
|
||||
* 需要引入org.python:jython
|
||||
*
|
||||
* @return Python引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getPythonEngine() {
|
||||
System.setProperty("python.import.site", "false");
|
||||
return getScript("python");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Python引擎<br>
|
||||
* 需要引入org.python:jython
|
||||
*
|
||||
* @return Python引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createPythonEngine() {
|
||||
System.setProperty("python.import.site", "false");
|
||||
return createScript("python");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Lua引擎<br>
|
||||
* 需要引入org.luaj:luaj-jse
|
||||
*
|
||||
* @return Lua引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getLuaEngine() {
|
||||
return getScript("lua");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Lua引擎<br>
|
||||
* 需要引入org.luaj:luaj-jse
|
||||
*
|
||||
* @return Lua引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createLuaEngine() {
|
||||
return createScript("lua");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Groovy引擎<br>
|
||||
* 需要引入org.codehaus.groovy:groovy-all
|
||||
*
|
||||
* @return Groovy引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getGroovyEngine() {
|
||||
return getScript("groovy");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Groovy引擎<br>
|
||||
* 需要引入org.codehaus.groovy:groovy-all
|
||||
*
|
||||
* @return Groovy引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createGroovyEngine() {
|
||||
return createScript("groovy");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Javascript脚本,返回Invocable,此方法分为两种情况:
|
||||
*
|
||||
* <ol>
|
||||
* <li>执行的脚本返回值是可执行的脚本方法</li>
|
||||
* <li>脚本为函数库,则ScriptEngine本身为可执行方法</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return 执行结果
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static Invocable evalInvocable(String script) throws ScriptRuntimeException {
|
||||
final ScriptEngine jsEngine = getJsEngine();
|
||||
final Object eval;
|
||||
try {
|
||||
eval = jsEngine.eval(script);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
if(eval instanceof Invocable){
|
||||
return (Invocable)eval;
|
||||
} else if(jsEngine instanceof Invocable){
|
||||
return (Invocable)jsEngine;
|
||||
}
|
||||
throw new ScriptRuntimeException("Script is not invocable !");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的Javascript脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return 执行结果
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script) throws ScriptRuntimeException {
|
||||
try {
|
||||
return getJsEngine().eval(script);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param context 脚本上下文
|
||||
* @return 执行结果
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script, ScriptContext context) throws ScriptRuntimeException {
|
||||
try {
|
||||
return getJsEngine().eval(script, context);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param bindings 绑定的参数
|
||||
* @return 执行结果
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script, Bindings bindings) throws ScriptRuntimeException {
|
||||
try {
|
||||
return getJsEngine().eval(script, bindings);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行JS脚本中的指定方法
|
||||
*
|
||||
* @param script js脚本
|
||||
* @param func 方法名
|
||||
* @param args 方法参数
|
||||
* @return 结果
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static Object invoke(String script, String func, Object... args) {
|
||||
final Invocable eval = evalInvocable(script);
|
||||
try {
|
||||
return eval.invokeFunction(func, args);
|
||||
} catch (ScriptException | NoSuchMethodException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译Javascript脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static CompiledScript compile(String script) throws ScriptRuntimeException {
|
||||
try {
|
||||
return compile(getJsEngine(), script);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译Javascript脚本
|
||||
*
|
||||
* @param engine 引擎
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptException 脚本异常
|
||||
*/
|
||||
public static CompiledScript compile(ScriptEngine engine, String script) throws ScriptException {
|
||||
if (engine instanceof Compilable) {
|
||||
final Compilable compEngine = (Compilable) engine;
|
||||
return compEngine.compile(script);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Script模块主要针对Java的javax.script封装,可以运行Javascript脚本。
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* Script模块主要针对Java的javax.script封装,可以运行Javascript脚本。
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.script;
|
@@ -1,22 +1,22 @@
|
||||
package cn.hutool.script.test;
|
||||
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import org.junit.Assert;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
public class NashornDeepTest {
|
||||
|
||||
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
|
||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
|
||||
|
||||
engine.eval(ResourceUtil.readUtf8Str("filter1.js"));
|
||||
|
||||
final Object filter1 = ((Invocable) engine).invokeFunction("filter1", 1, 2);
|
||||
Assert.assertFalse((Boolean) filter1);
|
||||
}
|
||||
}
|
||||
package cn.hutool.script.test;
|
||||
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import org.junit.Assert;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
public class NashornDeepTest {
|
||||
|
||||
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
|
||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
|
||||
|
||||
engine.eval(ResourceUtil.readUtf8Str("filter1.js"));
|
||||
|
||||
final Object filter1 = ((Invocable) engine).invokeFunction("filter1", 1, 2);
|
||||
Assert.assertFalse((Boolean) filter1);
|
||||
}
|
||||
}
|
||||
|
@@ -1,59 +1,59 @@
|
||||
package cn.hutool.script.test;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.script.ScriptRuntimeException;
|
||||
import cn.hutool.script.ScriptUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本单元测试类
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class ScriptUtilTest {
|
||||
|
||||
@Test
|
||||
public void compileTest() {
|
||||
CompiledScript script = ScriptUtil.compile("print('Script test!');");
|
||||
try {
|
||||
script.eval();
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void evalTest() {
|
||||
ScriptUtil.eval("print('Script test!');");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeTest() {
|
||||
final Object result = ScriptUtil.invoke(ResourceUtil.readUtf8Str("filter1.js"), "filter1", 2, 1);
|
||||
Assert.assertTrue((Boolean) result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pythonTest() throws ScriptException {
|
||||
final ScriptEngine pythonEngine = ScriptUtil.getPythonEngine();
|
||||
pythonEngine.eval("print('Hello Python')");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void luaTest() throws ScriptException {
|
||||
final ScriptEngine engine = ScriptUtil.getLuaEngine();
|
||||
engine.eval("print('Hello Lua')");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groovyTest() throws ScriptException {
|
||||
final ScriptEngine engine = ScriptUtil.getGroovyEngine();
|
||||
engine.eval("println 'Hello Groovy'");
|
||||
}
|
||||
}
|
||||
package cn.hutool.script.test;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.script.ScriptRuntimeException;
|
||||
import cn.hutool.script.ScriptUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本单元测试类
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class ScriptUtilTest {
|
||||
|
||||
@Test
|
||||
public void compileTest() {
|
||||
CompiledScript script = ScriptUtil.compile("print('Script test!');");
|
||||
try {
|
||||
script.eval();
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void evalTest() {
|
||||
ScriptUtil.eval("print('Script test!');");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeTest() {
|
||||
final Object result = ScriptUtil.invoke(ResourceUtil.readUtf8Str("filter1.js"), "filter1", 2, 1);
|
||||
Assert.assertTrue((Boolean) result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pythonTest() throws ScriptException {
|
||||
final ScriptEngine pythonEngine = ScriptUtil.getPythonEngine();
|
||||
pythonEngine.eval("print('Hello Python')");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void luaTest() throws ScriptException {
|
||||
final ScriptEngine engine = ScriptUtil.getLuaEngine();
|
||||
engine.eval("print('Hello Lua')");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groovyTest() throws ScriptException {
|
||||
final ScriptEngine engine = ScriptUtil.getGroovyEngine();
|
||||
engine.eval("println 'Hello Groovy'");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user