groovy.lang.GroovyShell.parse()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(708)

本文整理了Java中groovy.lang.GroovyShell.parse()方法的一些代码示例,展示了GroovyShell.parse()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GroovyShell.parse()方法的具体详情如下:
包路径:groovy.lang.GroovyShell
类名称:GroovyShell
方法名:parse

GroovyShell.parse介绍

[英]Parses the given script and returns it ready to be run. When running in a secure environment (-Djava.security.manager) codeSource.getCodeSource() determines what policy grants should be given to the script.
[中]解析给定脚本并返回准备运行的脚本。在安全环境中运行时(-Djava.security.manager)codeSource。getCodeSource()确定应向脚本授予哪些策略授权。

代码示例

代码示例来源:origin: org.codehaus.groovy/groovy

public Script parse(final String scriptText, final String fileName) throws CompilationFailedException {
  GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>() {
    public GroovyCodeSource run() {
      return new GroovyCodeSource(scriptText, fileName, DEFAULT_CODE_BASE);
    }
  });
  return parse(gcs);
}

代码示例来源: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: org.codehaus.groovy/groovy

/**
 * Parses the given script and returns it ready to be run
 *
 * @param reader    the stream reading the script
 * @param fileName  is the logical file name of the script (which is used to create the class name of the script)
 * @return the parsed script which is ready to be run via {@link Script#run()}
 */
public Script parse(final Reader reader, final String fileName) throws CompilationFailedException {
  return parse(new GroovyCodeSource(reader, fileName, DEFAULT_CODE_BASE));
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Parses the given script and returns it ready to be run
 *
 * @param uri is the URI of the script (which is used to create the class name of the script)
 */
public Script parse(URI uri) throws CompilationFailedException, IOException {
  return parse(new GroovyCodeSource(uri));
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Parses the given script and returns it ready to be run
 *
 * @param scriptText the text of the script
 */
public Script parse(String scriptText) throws CompilationFailedException {
  return parse(scriptText, generateScriptName());
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Parses the given script and returns it ready to be run
 *
 * @param in the stream reading the script
 */
public Script parse(Reader in) throws CompilationFailedException {
  return parse(in, generateScriptName());
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Evaluates some script against the current Binding and returns the result
 *
 * @param codeSource
 * @throws CompilationFailedException
 */
public Object evaluate(GroovyCodeSource codeSource) throws CompilationFailedException {
  Script script = parse(codeSource);
  return script.run();
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Parses the bean definition groovy script by first exporting the given {@link Binding}. 
 */
public void parse(InputStream script, Binding binding) {
  if (script==null)
    throw new IllegalArgumentException("No script is provided");
  setBinding(binding);
  CompilerConfiguration cc = new CompilerConfiguration();
  cc.setScriptBaseClass(ClosureScript.class.getName());
  GroovyShell shell = new GroovyShell(classLoader,binding,cc);
  ClosureScript s = (ClosureScript)shell.parse(new InputStreamReader(script));
  s.setDelegate(this);
  s.run();
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
* Runs this server. There is typically no need to call this method, as the object's constructor
* creates a new thread and runs this object automatically. 
*/ 
public void run() {
  try {
    ServerSocket serverSocket = new ServerSocket(url.getPort());
    while (true) {
      // Create one script per socket connection.
      // This is purposefully not caching the Script
      // so that the script source file can be changed on the fly,
      // as each connection is made to the server.
      //FIXME: Groovy has other mechanisms specifically for watching to see if source code changes.
      // We should probably be using that here.
      // See also the comment about the fact we recompile a script that can't change.
      Script script = groovy.parse(source);
      new GroovyClientConnection(script, autoOutput, serverSocket.accept());
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: groovy/groovy-core

public Template createTemplate(Reader reader) throws CompilationFailedException, IOException {
  SimpleTemplate template = new SimpleTemplate();
  String script = template.parse(reader);
  if (verbose) {
    System.out.println("\n-- script source --");
    System.out.print(script);
    System.out.println("\n-- script end --\n");
  }
  try {
    template.script = groovyShell.parse(script, "SimpleTemplateScript" + counter++ + ".groovy");
  } catch (Exception e) {
    throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
  }
  return template;
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Parses the given script and returns it ready to be run
 *
 * @param file is the file of the script (which is used to create the class name of the script)
 */
public Script parse(File file) throws CompilationFailedException, IOException {
  return parse(new GroovyCodeSource(file, config.getSourceEncoding()));
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Evaluates some script against the current Binding and returns the result
 *
 * @param in       the stream reading the script
 * @param fileName is the logical file name of the script (which is used to create the class name of the script)
 */
public Object evaluate(Reader in, String fileName) throws CompilationFailedException {
  Script script = null;
  try {
    script = parse(in, fileName);
    return script.run();
  } finally {
    if (script != null) {
      InvokerHelper.removeClass(script.getClass());
    }
  }
}

代码示例来源:origin: groovy/groovy-core

public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException {
  Node root ;
  try {
    root = xmlParser.parse(reader);
  } catch (SAXException e) {
    throw new RuntimeException("Parsing XML source failed.", e);
  }
  if (root == null) {
    throw new IOException("Parsing XML source failed: root node is null.");
  }
  StringWriter writer = new StringWriter(1024);
  writer.write("/* Generated by XmlTemplateEngine */\n");
  new GspPrinter(new PrintWriter(writer), indentation).print(root);
  Script script;
  try {
    script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter++ + ".groovy");
  } catch (Exception e) {
    throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
  }
  return new XmlTemplate(script);
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Process the input files.
 */
private void processFiles() throws CompilationFailedException, IOException, URISyntaxException {
  GroovyShell groovy = new GroovyShell(conf);
  setupContextClassLoader(groovy);
  Script s = groovy.parse(getScriptSource(isScriptFile, script));
  if (args.isEmpty()) {
    try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
      PrintWriter writer = new PrintWriter(System.out);
      processReader(s, reader, writer);
      writer.flush();
    }
  } else {
    Iterator i = args.iterator();
    while (i.hasNext()) {
      String filename = (String) i.next();
      //TODO: These are the arguments for -p and -i.  Why are we searching using Groovy script extensions?
      // Where is this documented?
      File file = huntForTheScriptFile(filename);
      processFile(s, file);
    }
  }
}

代码示例来源:origin: spockframework/spock

@Nullable
private DelegatingScript loadScriptFromFileSystemLocation(String location) {
 File file = new File(location);
 try {
  if (!file.exists()) return null;
 } catch (AccessControlException e) {
  // no permission to check for existence of file (e.g. on Spock Web Console),
  // so let's just assume it's not there and continue
  return null;
 }
 GroovyShell shell = createShell();
 try {
  return (DelegatingScript) shell.parse(file);
 } catch (IOException e) {
  throw new ConfigurationException("Error reading configuration script '%s'", location);
 } catch (CompilationFailedException e) {
  throw new ConfigurationException("Error compiling configuration script '%s'", location);
 }
}

代码示例来源:origin: spockframework/spock

@Nullable
private DelegatingScript loadScriptFromClassPathLocation(String location) {
 URL url = this.getClass().getClassLoader().getResource(location);
 if (url == null) return null;
 GroovyShell shell = createShell();
 GroovyCodeSource codeSource = null;
 // try block below for compatibility pre/post Groovy 2.4.7 (see GROOVY-8126)
 try {
  codeSource = new GroovyCodeSource(url);
 } catch (Exception e) {
  throw new ConfigurationException("Error reading configuration script '%s'", location);
 }
 try {
  return (DelegatingScript) shell.parse(codeSource);
 } catch (CompilationFailedException e) {
  throw new ConfigurationException("Error compiling configuration script '%s'", location);
 }
}

代码示例来源:origin: groovy/groovy-core

private Script compile(String input, GroovyShell groovyShell) throws Exception {
  TraversalTestHelper traverser = new TraversalTestHelper();
  traverser.traverse(input, SourcePrinter.class, Boolean.FALSE);
  return groovyShell.parse(input);
}

代码示例来源:origin: apache/nifi

scriptText = scriptBody;
script = shell.parse(PRELOADS + scriptText, scriptName);
compiled = (Class<Script>) script.getClass();

代码示例来源:origin: org.codehaus.groovy/groovy

script = (TypeCheckingDSL) shell.parse(
    new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding())
);

代码示例来源:origin: crashub/crash

public void testExplicitReturnFromCRaSHScript() throws Exception {
 CompilerConfiguration config = new CompilerConfiguration();
 config.setScriptBaseClass(GroovyScriptCommand.class.getName());
 GroovyShell shell = new GroovyShell(config);
 Script script = shell.parse("return 'something'");
 Field f = script.getClass().getDeclaredField("org_crsh_has_explicit_return");
 assertTrue(Modifier.isPublic(f.getModifiers()));
 assertTrue(Modifier.isStatic(f.getModifiers()));
}

相关文章