public static Script newScript(Class<?> scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException { Script script; try { Constructor constructor = scriptClass.getConstructor(Binding.class); script = (Script) constructor.newInstance(context); } catch (NoSuchMethodException e) { // Fallback for non-standard "Script" classes. script = (Script) scriptClass.newInstance(); script.setBinding(context); } return script; }
public void setProperty(String property, Object newValue) { if ("binding".equals(property)) setBinding((Binding) newValue); else if("metaClass".equals(property)) setMetaClass((MetaClass)newValue); else binding.setVariable(property, newValue); }
public Object parseClassFromScript(String script) { Class<?> parsedClass = groovyClassLoader.parseClass(script); Script instance = null; try { instance = (Script) parsedClass.newInstance(); instance.setBinding(scriptBinding); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return instance.run(); } }
private Object runScript(Script script) { groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, groovyEvaluator.getBeakerX()); script.setBinding(groovyEvaluator.getScriptBinding()); return script.run(); }
public Object build(Script script) { // this used to be synchronized, but we also used to remove the // metaclass. Since adding the metaclass is now a side effect, we // don't need to ensure the meta-class won't be observed and don't // need to hide the side effect. MetaClass scriptMetaClass = script.getMetaClass(); script.setMetaClass(new FactoryInterceptorMetaClass(scriptMetaClass, this)); script.setBinding(this); Object oldScriptName = getProxyBuilder().getVariables().get(SCRIPT_CLASS_NAME); try { getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, script.getClass().getName()); return script.run(); } finally { if(oldScriptName != null) { getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, oldScriptName); } else { getProxyBuilder().getVariables().remove(SCRIPT_CLASS_NAME); } } }
/** * When a method is not found in the current script, checks that it's possible to call a method closure from the binding. * * @throws IOException * @throws CompilationFailedException * @throws IllegalAccessException * @throws InstantiationException */ public void testInvokeMethodFallsThroughToMethodClosureInBinding() throws IOException, CompilationFailedException, IllegalAccessException, InstantiationException { String text = "if (method() == 3) { println 'succeeded' }"; GroovyCodeSource codeSource = new GroovyCodeSource(text, "groovy.script", "groovy.script"); GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader()); Class clazz = loader.parseClass(codeSource); Script script = ((Script) clazz.newInstance()); Binding binding = new Binding(); binding.setVariable("method", new MethodClosure(new Dummy(), "method")); script.setBinding(binding); script.run(); }
@Override public void customize(GroovyObject goo) { Assert.state(goo instanceof Script, "Expected a Script"); ((Script) goo).setBinding(this.binding); super.customize(goo); }
@Override public Object run(Map<String, Object> variables) { script.setBinding(variables == null ? null : new Binding(variables)); return script.run(); }
@Override public void setBinding(Binding binding) { super.setBinding(binding); updateBoundVariables(); }
@Override public Object run(Map<String, Object> variables) { script.setBinding(variables == null ? null : new Binding(variables)); return script.run(); }
/** * Return a script object with the given vars from the compiled script object */ @SuppressWarnings("unchecked") private Script createScript(Object compiledScript, Map<String, Object> vars) throws InstantiationException, IllegalAccessException { Class scriptClass = (Class) compiledScript; Script scriptObject = (Script) scriptClass.newInstance(); Binding binding = new Binding(); binding.getVariables().putAll(vars); scriptObject.setBinding(binding); return scriptObject; }
@Override public void setBinding(final Binding binding) { super.setBinding(binding); this.context = ((GroovyContextBinding) binding).getContext(); }
public void setProperty(String property, Object newValue) { if ("binding".equals(property)) setBinding((Binding) newValue); else if("metaClass".equals(property)) setMetaClass((MetaClass)newValue); else binding.setVariable(property, newValue); }
public void setProperty(String property, Object newValue) { if ("binding".equals(property)) setBinding((Binding) newValue); else if("metaClass".equals(property)) setMetaClass((MetaClass)newValue); else binding.setVariable(property, newValue); }
@Override public Object execute(Object compiledScript, Map<String, Object> vars) { try { Class scriptClass = (Class) compiledScript; Script scriptObject = (Script) scriptClass.newInstance(); Binding binding = new Binding(vars); scriptObject.setBinding(binding); return scriptObject.run(); } catch (Exception e) { throw new ScriptException("failed to execute script", e); } }
@Override public Object execute(Map<String, Object> arguments) throws Exception { Map<String, Object> args = CollectionUtil.nullAsEmpty(arguments); groovyScript.setBinding(new Binding(args)); return groovyScript.run(); } }
public RatpackDslClosures apply(Path file, String scriptContent) throws Exception { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ScriptEngine<Script> scriptEngine = new ScriptEngine<>(classLoader, compileStatic, Script.class); return RatpackDslClosures.capture(function, file, () -> { Script script = scriptEngine.create(file.getFileName().toString(), file, scriptContent); script.setBinding(new Binding(args)); script.run(); }); }
/** * @param context * Variables the script will see. */ private boolean evaluate(Binding context) { script.setBinding(context); try { return TRUE.equals(GroovySandbox.run(script, Whitelist.all())); } catch (RejectedAccessException x) { throw ScriptApproval.get().accessRejected(x, ApprovalContext.create()); } }
public Object build(Script script) { synchronized (script) { MetaClass scriptMetaClass = script.getMetaClass(); try { script.setMetaClass(new FactoryInterceptorMetaClass(scriptMetaClass, this)); script.setBinding(this); return script.run(); } finally { script.setMetaClass(scriptMetaClass); } } }
public <T> T evaluate(Exchange exchange, Class<T> type) { Script script = instantiateScript(exchange); script.setBinding(createBinding(exchange)); Object value = script.run(); return exchange.getContext().getTypeConverter().convertTo(type, value); }