本文整理了Java中groovy.lang.Script
类的一些代码示例,展示了Script
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Script
类的具体详情如下:
包路径:groovy.lang.Script
类名称:Script
[英]This object represents a Groovy script
[中]这个对象代表一个Groovy脚本
代码示例来源:origin: groovy/groovy-core
protected void assertScript(final String text, final String scriptName) throws Exception {
log.info("About to execute script");
log.info(text);
GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new GroovyCodeSource(text, scriptName, "/groovy/testSupport");
}
});
Class groovyClass = loader.parseClass(gcs);
Script script = InvokerHelper.createScript(groovyClass, new Binding());
script.run();
}
代码示例来源:origin: twosigma/beakerx
private Object runScript(Script script) {
groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, groovyEvaluator.getBeakerX());
script.setBinding(groovyEvaluator.getScriptBinding());
return script.run();
}
代码示例来源:origin: org.codehaus.groovy/groovy
String line;
String lineCountName = "count";
s.setProperty(lineCountName, BigInteger.ZERO);
String autoSplitName = "split";
s.setProperty("out", pw);
s.setProperty("line", line);
s.setProperty(lineCountName, ((BigInteger)s.getProperty(lineCountName)).add(BigInteger.ONE));
s.setProperty(autoSplitName, line.split(splitPattern));
Object o = s.run();
代码示例来源:origin: org.codehaus.groovy/groovy
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);
}
代码示例来源:origin: groovy/groovy-core
public Writer writeTo(Writer out) {
Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
PrintWriter pw = new PrintWriter(out);
scriptObject.setProperty("out", pw);
scriptObject.run();
pw.flush();
return out;
}
代码示例来源:origin: groovy/groovy-core
/**
* Write the template document with the set binding applied to the writer.
*
* @see groovy.lang.Writable#writeTo(java.io.Writer)
*/
public Writer writeTo(Writer writer) {
Binding binding;
if (map == null)
binding = new Binding();
else
binding = new Binding(map);
Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
PrintWriter pw = new PrintWriter(writer);
scriptObject.setProperty("out", pw);
scriptObject.run();
pw.flush();
return writer;
}
代码示例来源:origin: org.carewebframework/org.carewebframework.web.script.groovy
@Override
public Object run(Map<String, Object> variables) {
script.setBinding(variables == null ? null : new Binding(variables));
return script.run();
}
代码示例来源:origin: groovy/groovy-core
/**
* 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();
}
代码示例来源:origin: org.codehaus.groovy/groovy
public void set(Object value) {
script.getBinding().setVariable(variable, value);
}
}
代码示例来源:origin: groovy/groovy-core
public void run()
{
final long id = Thread.currentThread().getId();
// run the script numIter times
for (int i = 0; i < numIter; i++)
{
Builder builder = new Builder();
Binding binding = new Binding();
binding.setVariable("builder", builder);
script = InvokerHelper.createScript(scriptClass, binding);
script.run();
}
latch.countDown();
}
}
代码示例来源:origin: groovy/groovy-core
public void testCreateScriptWithScriptClass() {
GroovyClassLoader classLoader = new GroovyClassLoader();
String controlProperty = "text";
String controlValue = "I am a script";
String code = controlProperty + " = '" + controlValue + "'";
GroovyCodeSource codeSource = new GroovyCodeSource(code, "testscript", "/groovy/shell");
Class scriptClass = classLoader.parseClass(codeSource, false);
Script script = InvokerHelper.createScript(scriptClass, new Binding(bindingVariables));
assertEquals(bindingVariables, script.getBinding().getVariables());
script.run();
assertEquals(controlValue, script.getProperty(controlProperty));
}
}
代码示例来源:origin: io.vertx/vertx-lang-groovy-gen
@Test
public void testReuseBindingInScript() throws Exception {
Class clazz = assertScript("ReuseBindingVerticleScript");
Script script = (Script) clazz.newInstance();
Binding binding = new Binding();
binding.setVariable("myobject", new Object());
script.setBinding(binding);
ScriptVerticle verticle = new ScriptVerticle(script);
assertDeploy((vertx, onDeploy) ->
vertx.deployVerticle(verticle, onDeploy));
assertTrue(isStarted());
}
代码示例来源:origin: com.disney.groovity/groovity-portal
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(request instanceof HttpServletRequest){
HttpServletRequest hr = (HttpServletRequest) request;
HttpSession session = hr.getSession(false);
if(session!=null){
Object userId = session.getAttribute("userId");
if(userId != null){
Groovity groovity = (Groovity) servletContext.getAttribute(GroovityServlet.SERVLET_CONTEXT_GROOVITY_INSTANCE);
Script factory;
try {
factory = groovity.load("/data/factory", new Binding());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new ServletException(e);
}
Object user = factory.invokeMethod("call", new String[]{"person",userId.toString()});
if(user!=null && user instanceof Principal){
request = new AuthenticatedRequestWrapper(hr, (Principal)user);
}
}
}
}
chain.doFilter(request, response);
}
代码示例来源:origin: groovy/groovy-core
public void testCreateScriptWithNullClass() {
Script script = InvokerHelper.createScript(null, new Binding(bindingVariables));
assertEquals(bindingVariables, script.getBinding().getVariables());
}
代码示例来源:origin: org.elasticsearch.module/lang-groovy
/**
* 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;
}
代码示例来源:origin: apache/incubator-shardingsphere
private Object evaluate(final String expression) {
Script script;
if (SCRIPTS.containsKey(expression)) {
script = SCRIPTS.get(expression);
} else {
script = SHELL.parse(expression);
SCRIPTS.put(expression, script);
}
return script.run();
}
代码示例来源:origin: twosigma/beakerx
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();
}
}
代码示例来源:origin: org.codehaus.groovy/groovy
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);
}
}
}
代码示例来源:origin: apache/nifi
Map bindings = script.getBinding().getVariables();
bindings.put("SQL", SQL);
script.run();
bindings.clear();
代码示例来源:origin: org.codehaus.groovy/groovy
private Object invokePropertyOrMissing(Object object, String methodName, Object[] originalArguments, boolean fromInsideClass, boolean isCallToSuper) {
// if no method was found, try to find a closure defined as a field of the class and run it
Object value = null;
final MetaProperty metaProperty = this.getMetaProperty(methodName, false);
if (metaProperty != null)
value = metaProperty.getProperty(object);
else {
if (object instanceof Map)
value = ((Map)object).get(methodName);
}
if (value instanceof Closure) { // This test ensures that value != this If you ever change this ensure that value != this
Closure closure = (Closure) value;
MetaClass delegateMetaClass = closure.getMetaClass();
return delegateMetaClass.invokeMethod(closure.getClass(), closure, CLOSURE_DO_CALL_METHOD, originalArguments, false, fromInsideClass);
}
if (object instanceof Script) {
Object bindingVar = ((Script) object).getBinding().getVariables().get(methodName);
if (bindingVar != null) {
MetaClass bindingVarMC = ((MetaClassRegistryImpl) registry).getMetaClass(bindingVar);
return bindingVarMC.invokeMethod(bindingVar, CLOSURE_CALL_METHOD, originalArguments);
}
}
return invokeMissingMethod(object, methodName, originalArguments, null, isCallToSuper);
}
内容来源于网络,如有侵权,请联系作者删除!