Java Code Examples for groovy.lang.GroovyShell#evaluate()
The following examples show how to use
groovy.lang.GroovyShell#evaluate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadEmbeddedGroovyTest.java From rice with Educational Community License v2.0 | 7 votes |
@Test public void testNativeGroovy() { Binding binding = new Binding(); binding.setVariable("foo", new Integer(2)); GroovyShell shell = new GroovyShell(binding); Object value = shell.evaluate("println 'Hello World!'; x = 123; return foo * 10"); Assert.assertTrue(value.equals(new Integer(20))); Assert.assertTrue(binding.getVariable("x").equals(new Integer(123))); }
Example 2
Source File: ConditionAndActionPortalFactory.java From stendhal with GNU General Public License v2.0 | 6 votes |
/** * Extract the quest name from a context. * * @param ctx * The configuration context. * @return The quest name. * @throws IllegalArgumentException * If the quest attribute is missing. */ protected ChatAction getAction(final ConfigurableFactoryContext ctx) { String value = ctx.getString("action", null); if (value == null) { return null; } Binding groovyBinding = new Binding(); final GroovyShell interp = new GroovyShell(groovyBinding); try { String code = "import games.stendhal.server.entity.npc.action.*;\r\n" + value; return (ChatAction) interp.evaluate(code); } catch (CompilationFailedException e) { throw new IllegalArgumentException(e); } }
Example 3
Source File: GroovyScriptPostProcessor.java From powsybl-core with Mozilla Public License 2.0 | 6 votes |
@Override public void process(Network network, ComputationManager computationManager) throws Exception { if (Files.exists(script)) { LOGGER.debug("Execute groovy post processor {}", script); try (Reader reader = Files.newBufferedReader(script, StandardCharsets.UTF_8)) { CompilerConfiguration conf = new CompilerConfiguration(); Binding binding = new Binding(); binding.setVariable("network", network); binding.setVariable("computationManager", computationManager); GroovyShell shell = new GroovyShell(binding, conf); shell.evaluate(reader); } } }
Example 4
Source File: AccessCheckingPortalFactory.java From stendhal with GNU General Public License v2.0 | 6 votes |
/** * Creates a new ChatAction from ConfigurableFactoryContext. * * @param ctx * ConfigurableFactoryContext * @return * ChatAction instance */ protected ChatAction getRejectedAction(final ConfigurableFactoryContext ctx) { String value = ctx.getString("rejectedAction", null); if (value == null) { return null; } Binding groovyBinding = new Binding(); final GroovyShell interp = new GroovyShell(groovyBinding); try { String code = "import games.stendhal.server.entity.npc.action.*;\r\n" + value; return (ChatAction) interp.evaluate(code); } catch (CompilationFailedException e) { throw new IllegalArgumentException(e); } }
Example 5
Source File: GroovyAssert.java From groovy with Apache License 2.0 | 6 votes |
/** * Asserts that the given script fails when it is evaluated * and that a particular type of exception is thrown. * * @param clazz the class of the expected exception * @param script the script that should fail * @return the caught exception */ public static Throwable shouldFail(Class clazz, String script) { Throwable th = null; try { GroovyShell shell = new GroovyShell(); shell.evaluate(script, genericScriptName()); } catch (GroovyRuntimeException gre) { th = ScriptBytecodeAdapter.unwrap(gre); } catch (Throwable e) { th = e; } if (th == null) { fail("Script should have failed with an exception of type " + clazz.getName()); } else if (!clazz.isInstance(th)) { fail("Script should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + th); } return th; }
Example 6
Source File: ApiGroovyCompiler.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
private void applyConfigurationScript(File configScript, CompilerConfiguration configuration) { VersionNumber version = parseGroovyVersion(); if (version.compareTo(VersionNumber.parse("2.1")) < 0) { throw new GradleException("Using a Groovy compiler configuration script requires Groovy 2.1+ but found Groovy " + version + ""); } Binding binding = new Binding(); binding.setVariable("configuration", configuration); CompilerConfiguration configuratorConfig = new CompilerConfiguration(); ImportCustomizer customizer = new ImportCustomizer(); customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder"); configuratorConfig.addCompilationCustomizers(customizer); GroovyShell shell = new GroovyShell(binding, configuratorConfig); try { shell.evaluate(configScript); } catch (Exception e) { throw new GradleException("Could not execute Groovy compiler configuration script: " + configScript.getAbsolutePath(), e); } }
Example 7
Source File: Groovy.java From groovy with Apache License 2.0 | 6 votes |
private void configureCompiler() { if (scriptBaseClass != null) { configuration.setScriptBaseClass(scriptBaseClass); } if (configscript != null) { Binding binding = new Binding(); binding.setVariable("configuration", configuration); CompilerConfiguration configuratorConfig = new CompilerConfiguration(); ImportCustomizer customizer = new ImportCustomizer(); customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder"); configuratorConfig.addCompilationCustomizers(customizer); GroovyShell shell = new GroovyShell(binding, configuratorConfig); File confSrc = new File(configscript); try { shell.evaluate(confSrc); } catch (IOException e) { throw new BuildException("Unable to configure compiler using configuration file: " + confSrc, e); } } }
Example 8
Source File: DigAreaFactory.java From stendhal with GNU General Public License v2.0 | 5 votes |
protected ChatCondition getCondition(final ConfigurableFactoryContext ctx) { String value = ctx.getString("condition", null); if (value == null) { return null; } Binding groovyBinding = new Binding(); final GroovyShell interp = new GroovyShell(groovyBinding); try { String code = "import games.stendhal.server.entity.npc.condition.*;\r\n" + value; return (ChatCondition) interp.evaluate(code); } catch (CompilationFailedException e) { throw new IllegalArgumentException(e); } }
Example 9
Source File: DigAreaFactory.java From stendhal with GNU General Public License v2.0 | 5 votes |
protected ChatAction getAction(final ConfigurableFactoryContext ctx) { String value = ctx.getString("action", null); if (value == null) { return null; } Binding groovyBinding = new Binding(); final GroovyShell interp = new GroovyShell(groovyBinding); try { String code = "import games.stendhal.server.entity.npc.action.*;\r\n" + value; return (ChatAction) interp.evaluate(code); } catch (CompilationFailedException e) { throw new IllegalArgumentException(e); } }
Example 10
Source File: GateFactory.java From stendhal with GNU General Public License v2.0 | 5 votes |
@Override public Object create(ConfigurableFactoryContext ctx) { final String orientation = ctx.getRequiredString("orientation"); final String image = ctx.getRequiredString("image"); final int autoclose = ctx.getInt("autoclose", 0); final String id = ctx.getString("identifier", null); final String message = ctx.getString("message", null); ChatCondition condition = null; final String condString = ctx.getString("condition", null); if (condString != null) { final GroovyShell interp = new GroovyShell(new Binding()); String code = "import games.stendhal.server.entity.npc.condition.*;\r\n" + condString; try { condition = (ChatCondition) interp.evaluate(code); } catch (CompilationFailedException e) { throw new IllegalArgumentException(e); } } final Gate gate = new Gate(orientation, image, condition); gate.setAutoCloseDelay(autoclose); gate.setRefuseMessage(message); gate.setIdentifier(id); return gate; }
Example 11
Source File: GroovyScriptEngine.java From jeewx with Apache License 2.0 | 5 votes |
public Object executeObject(String script, Map<String, Object> vars) { this.logger.debug("执行:" + script); this.binding.clearVariables(); GroovyShell shell = new GroovyShell(this.binding); setParameters(shell, vars); script = script.replace("'", "'").replace(""", "\"").replace(">", ">").replace("<", "<").replace("&nuot;", "\n").replace("&", "&"); Object rtn = shell.evaluate(script); //this.binding.clearVariables(); return rtn; }
Example 12
Source File: GroovyExecutor.java From orbit-image-analysis with GNU General Public License v3.0 | 5 votes |
public static void main(String[] args) throws Exception { OrbitLogAppender.GUI_APPENDER = false; if (args == null || args.length < 1) throw new IllegalArgumentException("No URL argument found. Call: GroovyExecutor <URL>"); // TODO: Do this properly... //doTrustToCertificates(); //URL url = new URL("https://chiron.idorsia.com/stash/projects/ORBIT/repos/public-scripts/browse/QuantPerGroupLocalTest.groovy?at=5ff41667c494870aaacbe37fa43fd46fd4c9659c&raw"); URL url = new URL(args[0]); String content = RawUtilsCommon.getContentStr(url); logger.debug("executing code:\n" + content); logger.info("start executing groovy code"); GroovyShell shell = new GroovyShell(); shell.evaluate(content); logger.info("finished"); }
Example 13
Source File: ConditionAndActionAreaFactory.java From stendhal with GNU General Public License v2.0 | 5 votes |
protected ChatAction getAction(final ConfigurableFactoryContext ctx) { String value = ctx.getString("action", null); if (value == null) { return null; } Binding groovyBinding = new Binding(); final GroovyShell interp = new GroovyShell(groovyBinding); try { String code = "import games.stendhal.server.entity.npc.action.*;\r\n" + value; return (ChatAction) interp.evaluate(code); } catch (CompilationFailedException e) { throw new IllegalArgumentException(e); } }
Example 14
Source File: GroovySandbox.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
public Object evaluate(String script) { try { Helper.checkNotNull(script, "script"); GroovyShell sandboxShell = getSandboxShell(); setBindingsOnShell(sandboxShell); Object res = sandboxShell.evaluate(script); return res; } catch (CompilationFailedException | IOException e) { throw new SpagoBIRuntimeException("Error evaluating groovy script", e); } }
Example 15
Source File: Groovy7826Bug.java From groovy with Apache License 2.0 | 5 votes |
public void testComplexTypeArguments() throws Exception { String script = "def f(org.codehaus.groovy.ast.Groovy7826Bug.C1 c1) { }"; CompilerConfiguration config = new CompilerConfiguration(); config.getOptimizationOptions().put("asmResolving", false); GroovyShell shell = new GroovyShell(config); shell.evaluate(script, "bug7826.groovy"); }
Example 16
Source File: GroovyDynamicData.java From phoenix.webui.framework with Apache License 2.0 | 5 votes |
@Override public String getValue(String orginData) { String value; Binding binding = new Binding(); GroovyShell shell = new GroovyShell(binding); binding.setVariable("globalMap", globalMap); Object resObj = null; try { resObj = shell.evaluate(groovyCls + orginData); if(resObj != null) { value = resObj.toString(); } else { value = "groovy not return!"; } } catch(CompilationFailedException e) { value = e.getMessage(); logger.error("Groovy动态数据语法错误!", e); } return value; }
Example 17
Source File: BlockTargetFactory.java From stendhal with GNU General Public License v2.0 | 5 votes |
/** * Create a ChatCondtion * * @param condition the configuration String * @return the condition or null * @throws CompilationFailedException */ private ChatCondition createCondition(String condition) throws CompilationFailedException { final GroovyShell interp = createGroovyShell(); String code = "import games.stendhal.server.entity.npc.condition.*;\r\n" + condition; ChatCondition created = (ChatCondition) interp.evaluate(code); return created; }
Example 18
Source File: ConditionAndActionAreaFactory.java From stendhal with GNU General Public License v2.0 | 5 votes |
protected ChatCondition getCondition(final ConfigurableFactoryContext ctx) { String value = ctx.getString("condition", null); if (value == null) { return null; } Binding groovyBinding = new Binding(); final GroovyShell interp = new GroovyShell(groovyBinding); try { String code = "import games.stendhal.server.entity.npc.condition.*;\r\n" + value; return (ChatCondition) interp.evaluate(code); } catch (CompilationFailedException e) { throw new IllegalArgumentException(e); } }
Example 19
Source File: DefaultScriptingImpl.java From yarg with Apache License 2.0 | 4 votes |
@Override public <T> T evaluateGroovy(String script, Map<String, Object> params) { Binding binding = new Binding(params); GroovyShell shell = new GroovyShell(Thread.currentThread().getContextClassLoader(), binding); return (T) shell.evaluate(script); }
Example 20
Source File: GroovyAssert.java From groovy with Apache License 2.0 | 2 votes |
/** * Asserts that the script runs without any exceptions * * @param script the script that should pass without any exception thrown */ public static void assertScript(final String script) throws Exception { GroovyShell shell = new GroovyShell(); shell.evaluate(script, genericScriptName()); }