• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java Compilable类的典型用法代码示例

java 8次浏览

本文整理汇总了Java中javax.script.Compilable的典型用法代码示例。如果您正苦于以下问题:Java Compilable类的具体用法?Java Compilable怎么用?Java Compilable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。

Compilable类属于javax.script包,在下文中一共展示了Compilable类的34个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: invokeFunctionWithCustomScriptContextTest

点赞 3

import javax.script.Compilable; //导入依赖的package包/类
@Test
public void invokeFunctionWithCustomScriptContextTest() throws Exception {
    final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

    // create an engine and a ScriptContext, but don't set it as default
    final ScriptContext scriptContext = new SimpleScriptContext();

    // Set some value in the context
    scriptContext.setAttribute("myString", "foo", ScriptContext.ENGINE_SCOPE);

    // Evaluate script with custom context and get back a function
    final String script = "function (c) { return myString.indexOf(c); }";
    final CompiledScript compiledScript = ((Compilable)engine).compile(script);
    final Object func = compiledScript.eval(scriptContext);

    // Invoked function should be able to see context it was evaluated with
    final Object result = ((Invocable) engine).invokeMethod(func, "call", func, "o", null);
    assertTrue(((Number)result).intValue() == 1);
}
 

开发者ID:AdoptOpenJDK,
项目名称:openjdk-jdk10,
代码行数:20,
代码来源:ScopeTest.java

示例2: init

点赞 3

import javax.script.Compilable; //导入依赖的package包/类
private void init(Snapshot snapshot) throws RuntimeException {
    this.snapshot = snapshot;
    try {
        ScriptEngineManager manager = new ScriptEngineManager();
        engine = manager.getEngineByName("JavaScript"); // NOI18N
        InputStream strm = getInitStream();
        CompiledScript cs = ((Compilable)engine).compile(new InputStreamReader(strm));
        cs.eval();
        Object heap = ((Invocable)engine).invokeFunction("wrapHeapSnapshot", snapshot); // NOI18N
        engine.put("heap", heap); // NOI18N
        engine.put("cancelled", cancelled); // NOI18N
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Error initializing snapshot", ex); // NOI18N
        throw new RuntimeException(ex);
    }
}
 

开发者ID:apache,
项目名称:incubator-netbeans,
代码行数:17,
代码来源:OQLEngineImpl.java

示例3: ScriptExecutor

点赞 3

import javax.script.Compilable; //导入依赖的package包/类
/**
 * Construct a new script executors
 *
 * @param engine
 *            the script engine to use, must not be <code>null</code>
 * @param command
 *            the command to execute, may be <code>null</code>
 * @param classLoader
 *            the class loader to use when executing, may be
 *            <code>null</code>
 * @throws ScriptException
 */
public ScriptExecutor ( final ScriptEngine engine, final String command, final ClassLoader classLoader, final String sourceName ) throws Exception
{
    this.engine = engine;
    this.command = command;
    this.commandUrl = null;
    this.classLoader = classLoader;
    this.sourceName = sourceName;

    if ( command != null && engine instanceof Compilable && !Boolean.getBoolean ( PROP_NAME_DISABLE_COMPILE ) )
    {
        if ( sourceName != null )
        {
            engine.put ( ScriptEngine.FILENAME, sourceName );
        }

        Scripts.executeWithClassLoader ( classLoader, new Callable<Void> () {
            @Override
            public Void call () throws Exception
            {
                ScriptExecutor.this.compiledScript = ( (Compilable)engine ).compile ( command );
                return null;
            }
        } );
    }
}
 

开发者ID:eclipse,
项目名称:neoscada,
代码行数:38,
代码来源:ScriptExecutor.java

示例4: createScriptEngine

点赞 3

import javax.script.Compilable; //导入依赖的package包/类
@Override
protected ScriptEngine createScriptEngine() {
    String scripEngineName = SCRIPT_ENGINE_NAME;
    // ScriptEngine result = new ScriptEngineManager().getEngineByName(scripEngineName);
    NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
    ScriptEngine result = factory.getScriptEngine("-scripting");

    Validate.isInstanceOf(Compilable.class, result, "ScriptingEngine %s doesn't implement Compilable", scripEngineName);
    Validate.isInstanceOf(Invocable.class, result, "ScriptingEngine %s doesn't implement Invocable", scripEngineName);

    PROCESSOR_CLASSES.forEach((interfaceClass, scriptClass) -> addImport(result, scriptClass, interfaceClass.getSimpleName()));
    addImport(result, NashornPlugin.class, Plugin.class.getSimpleName());

    getStandardImportClasses().forEach(cls -> addImport(result, cls));

    result.put(KnowledgeBaseConstants.VAR_ENGINE_OPERATIONS, getEngineOperations());

    eval(result, "load(\"classpath:" + INITIAL_SCRIPT + "\");");

    return result;
}
 

开发者ID:softelnet,
项目名称:sponge,
代码行数:22,
代码来源:NashornKnowledgeBaseInterpreter.java

示例5: createScriptEngine

点赞 3

import javax.script.Compilable; //导入依赖的package包/类
@Override
protected ScriptEngine createScriptEngine() {
    String scripEngineName = SCRIPT_ENGINE_NAME;
    ScriptEngine result = new ScriptEngineManager().getEngineByName(scripEngineName);

    Validate.isInstanceOf(Compilable.class, result, "ScriptingEngine %s doesn't implement Compilable", scripEngineName);
    Validate.isInstanceOf(Invocable.class, result, "ScriptingEngine %s doesn't implement Invocable", scripEngineName);

    KotlinConstants.PROCESSOR_CLASSES
            .forEach((interfaceClass, scriptClass) -> addImport(result, scriptClass, interfaceClass.getSimpleName()));
    addImport(result, KPlugin.class, Plugin.class.getSimpleName());

    // TODO The line below performs very slow in Kotlin
    eval(result, getStandardImportClasses().stream().map(cls -> "import " + cls.getName()).collect(Collectors.joining("\n")));

    setVariable(result, KnowledgeBaseConstants.VAR_ENGINE_OPERATIONS, getEngineOperations());

    return result;
}
 

开发者ID:softelnet,
项目名称:sponge,
代码行数:20,
代码来源:ScriptKotlinKnowledgeBaseInterpreter.java

示例6: test

点赞 3

import javax.script.Compilable; //导入依赖的package包/类
@Test
public final void test() throws Exception {
	String csvMapperScript = "mapFunction = function(inputHeaders, inputField, inputValue, outputField, line) return inputValue end";
	String simpleScript = "return inputValue";
	CompiledScript compiledScript = (CompiledScript) ((Compilable) scriptEngine).compile(simpleScript);
	Bindings bindings = scriptEngine.createBindings();
	// inputHeaders, inputField, inputValue, outputField, line
	bindings.put("inputHeaders", "");
	bindings.put("inputField", "");
	bindings.put("inputValue", "testreturnvalue");
	bindings.put("outputField", "");
	bindings.put("line", "");
	String result = (String) compiledScript.eval(bindings);
	System.out.println(result);
	assertEquals("testreturnvalue", result);
}
 

开发者ID:ansell,
项目名称:csvsum,
代码行数:17,
代码来源:ScriptEngineTest.java


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)