本文整理匯總了Java中javax.script.Compilable.compile方法的典型用法代碼示例。如果您正苦於以下問題:Java Compilable.compile方法的具體用法?Java Compilable.compile怎麽用?Java Compilable.compile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.script.Compilable
的用法示例。
在下文中一共展示了Compilable.compile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: init
import javax.script.Compilable; //導入方法依賴的package包/類
private void init() throws ProcessingException {
// check formula
if (StringUtility.isNullOrEmpty(StringUtility.trim(script))) {
throw new VetoException(Texts.get("FormulaEmptyMessage"));
}
// Script
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// Compile Formula
Compilable compilingEngine = (Compilable) engine;
try {
compiledScript = compilingEngine.compile(script);
}
catch (ScriptException e) {
handleScriptException(e, script);
}
bindings = engine.createBindings();
}
示例2: getElValue
import javax.script.Compilable; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public <T> T getElValue(String exp, Object target, Object[] arguments, Object retVal, boolean hasRetVal, Class<T> valueType) throws Exception {
Bindings bindings=new SimpleBindings();
bindings.put(TARGET, target);
bindings.put(ARGS, arguments);
if(hasRetVal) {
bindings.put(RET_VAL, retVal);
}
CompiledScript script=expCache.get(exp);
if(null != script) {
return (T)script.eval(bindings);
}
if(engine instanceof Compilable) {
Compilable compEngine=(Compilable)engine;
script=compEngine.compile(funcs + exp);
expCache.put(exp, script);
return (T)script.eval(bindings);
} else {
return (T)engine.eval(funcs + exp, bindings);
}
}
示例3: intArrayLengthTest
import javax.script.Compilable; //導入方法依賴的package包/類
@Test
public void intArrayLengthTest() throws ScriptException {
Map<String, Object> vars = new HashMap<String, Object>();
Integer[] l = new Integer[] { 1,2,3 };
vars.put("l", l);
String script = "l.length";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
ScriptContext context = engine.getContext();
Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.putAll(vars);
Compilable compilable = (Compilable)engine;
CompiledScript compiledScript = compilable.compile(script);
Object o = compiledScript.eval();
assertThat(((Number) o).intValue(), equalTo(3));
}
示例4: objectArrayTest
import javax.script.Compilable; //導入方法依賴的package包/類
@Test
public void objectArrayTest() throws ScriptException {
Map<String, Object> vars = new HashMap<String, Object>();
final Map<String, Object> obj2 = new HashMap<String,Object>() {{
put("prop2", "value2");
}};
final Map<String, Object> obj1 = new HashMap<String,Object>() {{
put("prop1", "value1");
put("obj2", obj2);
}};
vars.put("l", new Object[] { "1", "2", "3", obj1 });
String script = "l.length";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.putAll(vars);
Compilable compilable = (Compilable)engine;
CompiledScript compiledScript = compilable.compile(script);
Object o = compiledScript.eval(bindings);
assertThat(((Number) o).intValue(), equalTo(4));
}
示例5: testInvoke
import javax.script.Compilable; //導入方法依賴的package包/類
public void testInvoke() throws Exception {
eng = new ScriptEngineManager().getEngineByName("jav8");
Compilable compiler = (Compilable) this.eng;
CompiledScript script = compiler.compile(calcFunction);
int max = 100000;
Bindings binding = this.eng.getBindings(ScriptContext.GLOBAL_SCOPE);
binding.put("num", 3);
Object r = script.eval(binding);
System.out.println(r);
long startM = System.currentTimeMillis();
for ( int i=0; i<max; i++ ) {
script.eval(binding);
}
long endM = System.currentTimeMillis();
System.out.println(" V8 engine loop " + max + ":" + (endM-startM));
}
示例6: main
import javax.script.Compilable; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval(new FileReader("src/main/javascript/Example.js"));
Compilable compilingEngine = (Compilable) engine;
CompiledScript script = compilingEngine.compile(new FileReader(
"src/main/javascript/Function.js"));
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("date", new Date());
bindings.put("out", System.out);
for (Map.Entry me : bindings.entrySet()) {
System.out.printf("%s: %s\n", me.getKey(),
String.valueOf(me.getValue()));
}
script.eval(bindings);
Invocable invocable = (Invocable) script.getEngine();
invocable.invokeFunction("sayhello", "Jose");
}
示例7: GraphvizWriter
import javax.script.Compilable; //導入方法依賴的package包/類
public GraphvizWriter(Graph graph) throws ScriptException, IOException
{
super(graph);
if (true)
{
throw new RuntimeException(
"Not yet implemented; this is unstable. The javascript requires Int32Array, which isn't currently provided by Rhino.");
}
// precompile the javascript.
StringBuilder builder = new StringBuilder();
builder.append(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("vizjs/viz.js")));
builder = new StringBuilder();
builder.append("function Viz() { console.log('Hello World'); }");
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
String script = builder.toString();
Compilable compilingEngine = (Compilable) engine;
vizJsCompiled = compilingEngine.compile(builder.toString());
}
示例8: eval
import javax.script.Compilable; //導入方法依賴的package包/類
public Object eval(ScriptEngine engine, String script, ScriptContext context) throws ScriptException
{
if (engine instanceof Compilable && ATTEMPT_COMPILATION)
{
Compilable eng = (Compilable) engine;
CompiledScript cs = eng.compile(script);
return context != null ? cs.eval(context) : cs.eval();
} else
return context != null ? engine.eval(script, context) : engine.eval(script);
}
示例9: loadCompiledScript
import javax.script.Compilable; //導入方法依賴的package包/類
public CompiledScript loadCompiledScript(ScriptEngine engine, File file) throws FileNotFoundException, ScriptException
{
int len = L2ScriptEngineManager.SCRIPT_FOLDER.getPath().length() + 1;
String relativeName = file.getPath().substring(len);
CompiledScriptHolder csh = _compiledScriptCache.get(relativeName);
if (csh != null && csh.matches(file))
{
if (Config.DEBUG)
LOG.fine("Reusing cached compiled script: " + file);
return csh.getCompiledScript();
}
else
{
if (Config.DEBUG)
LOG.info("Compiling script: " + file);
Compilable eng = (Compilable) engine;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
// TODO lock file
CompiledScript cs = eng.compile(reader);
if (cs instanceof Serializable)
synchronized (_compiledScriptCache)
{
_compiledScriptCache.put(relativeName, new CompiledScriptHolder(cs, file));
_modified = true;
}
return cs;
}
}
示例10: route
import javax.script.Compilable; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
try {
List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings();
bindings.put("invokers", invokersCopy);
bindings.put("invocation", invocation);
bindings.put("context", RpcContext.getContext());
CompiledScript function = compilable.compile(rule);
Object obj = function.eval(bindings);
if (obj instanceof Invoker[]) {
invokersCopy = Arrays.asList((Invoker<T>[]) obj);
} else if (obj instanceof Object[]) {
invokersCopy = new ArrayList<Invoker<T>>();
for (Object inv : (Object[]) obj) {
invokersCopy.add((Invoker<T>)inv);
}
} else {
invokersCopy = (List<Invoker<T>>) obj;
}
return invokersCopy;
} catch (ScriptException e) {
//fail then ignore rule .invokers.
logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
return invokers;
}
}
示例11: route
import javax.script.Compilable; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
try {
List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings();
bindings.put("invokers", invokersCopy);
bindings.put("invocation", invocation);
bindings.put("context", RpcContext.getContext());
CompiledScript function = compilable.compile(rule);
Object obj = function.eval(bindings);
if (obj instanceof Invoker[]) {
invokersCopy = Arrays.asList((Invoker<T>[]) obj);
} else if (obj instanceof Object[]) {
invokersCopy = new ArrayList<Invoker<T>>();
for (Object inv : (Object[]) obj) {
invokersCopy.add((Invoker<T>) inv);
}
} else {
invokersCopy = (List<Invoker<T>>) obj;
}
return invokersCopy;
} catch (ScriptException e) {
//fail then ignore rule .invokers.
logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
return invokers;
}
}
示例12: compileAndEvalInDiffContextTest
import javax.script.Compilable; //導入方法依賴的package包/類
@Test
public void compileAndEvalInDiffContextTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine engine = m.getEngineByName("js");
final Compilable compilable = (Compilable) engine;
final CompiledScript compiledScript = compilable.compile("foo");
final ScriptContext ctxt = new SimpleScriptContext();
ctxt.setAttribute("foo", "hello", ScriptContext.ENGINE_SCOPE);
assertEquals(compiledScript.eval(ctxt), "hello");
}
示例13: compileScript
import javax.script.Compilable; //導入方法依賴的package包/類
private static CompiledScript compileScript(String script) {
ScriptEngine engine = factory.getScriptEngine(new String[] { "--no-java" });
Compilable compEngine = (Compilable) engine;
try {
return compEngine.compile(script);
} catch (ScriptException e) {
log.warn("Failed to compile filter script: {}", e.getMessage(), e);
throw new IllegalArgumentException("Can't compile script: " + e.getMessage());
}
}
示例14: compileScriptFile
import javax.script.Compilable; //導入方法依賴的package包/類
/**
* Returns a compiled script.
*
* @param filePath Script path and file name.
* @return The compiled script, or <code>null</code> if the script engine does not support compilation.
* @throws IllegalArgumentException
* @throws ScriptException
* @throws IOException
*/
public static CompiledScript compileScriptFile(String filePath) throws ScriptException, IOException {
Assert.notNull("filePath", filePath);
CompiledScript script = parsedScripts.get(filePath);
if (script == null) {
ScriptEngineManager manager = getScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension(getFileExtension(filePath));
if (engine == null) {
throw new IllegalArgumentException("The script type is not supported for location: " + filePath);
}
engine = configureScriptEngineForInvoke(engine); // SCIPIO: 2017-01-27: Custom configurations for the engine
try {
Compilable compilableEngine = (Compilable) engine;
URL scriptUrl = FlexibleLocation.resolveLocation(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(scriptUrl.openStream()));
script = compilableEngine.compile(reader);
if (Debug.verboseOn()) {
Debug.logVerbose("Compiled script " + filePath + " using engine " + engine.getClass().getName(), module);
}
} catch (ClassCastException e) {
if (Debug.verboseOn()) {
Debug.logVerbose("Script engine " + engine.getClass().getName() + " does not implement Compilable", module);
}
}
if (script != null) {
parsedScripts.putIfAbsent(filePath, script);
}
}
return script;
}
示例15: compileScriptString
import javax.script.Compilable; //導入方法依賴的package包/類
/**
* Returns a compiled script.
*
* @param language
* @param script
* @return The compiled script, or <code>null</code> if the script engine does not support compilation.
* @throws IllegalArgumentException
* @throws ScriptException
*/
public static CompiledScript compileScriptString(String language, String script) throws ScriptException {
Assert.notNull("language", language, "script", script);
String cacheKey = language.concat("://").concat(script);
CompiledScript compiledScript = parsedScripts.get(cacheKey);
if (compiledScript == null) {
ScriptEngineManager manager = getScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(language);
if (engine == null) {
throw new IllegalArgumentException("The script type is not supported for language: " + language);
}
engine = configureScriptEngineForInvoke(engine); // SCIPIO: 2017-01-27: Custom configurations for the engine
try {
Compilable compilableEngine = (Compilable) engine;
compiledScript = compilableEngine.compile(script);
if (Debug.verboseOn()) {
Debug.logVerbose("Compiled script [" + script + "] using engine " + engine.getClass().getName(), module);
}
} catch (ClassCastException e) {
if (Debug.verboseOn()) {
Debug.logVerbose("Script engine " + engine.getClass().getName() + " does not implement Compilable", module);
}
}
if (compiledScript != null) {
parsedScripts.putIfAbsent(cacheKey, compiledScript);
}
}
return compiledScript;
}