Refine search
/** * 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(); }
public String call() throws RuntimeException { // if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader. if (cl==null) cl = Thread.currentThread().getContextClassLoader(); CompilerConfiguration cc = new CompilerConfiguration(); cc.addCompilationCustomizers(new ImportCustomizer().addStarImports( "jenkins", "jenkins.model", "hudson", "hudson.model")); GroovyShell shell = new GroovyShell(cl,new Binding(),cc); StringWriter out = new StringWriter(); PrintWriter pw = new PrintWriter(out); shell.setVariable("out", pw); try { Object output = shell.evaluate(script); if(output!=null) pw.println("Result: "+output); } catch (Throwable t) { Functions.printStackTrace(t, pw); } return out.toString(); } }
setWarningLevel(configuration.getWarningLevel()); setOutput(configuration.getOutput()); setTargetDirectory(configuration.getTargetDirectory()); setClasspathList(new LinkedList<String>(configuration.getClasspath())); setVerbose(configuration.getVerbose()); setDebug(configuration.getDebug()); setParameters(configuration.getParameters()); setTolerance(configuration.getTolerance()); setScriptBaseClass(configuration.getScriptBaseClass()); setRecompileGroovySource(configuration.getRecompileGroovySource()); setMinimumRecompilationInterval(configuration.getMinimumRecompilationInterval()); setTargetBytecode(configuration.getTargetBytecode()); setDefaultScriptExtension(configuration.getDefaultScriptExtension()); setSourceEncoding(configuration.getSourceEncoding()); setTargetDirectory(configuration.getTargetDirectory()); Map<String, Object> jointCompilationOptions = configuration.getJointCompilationOptions(); if (jointCompilationOptions != null) { jointCompilationOptions = new HashMap<String, Object>(jointCompilationOptions); setJointCompilationOptions(jointCompilationOptions); setPluginFactory(configuration.getPluginFactory()); setScriptExtensions(configuration.getScriptExtensions()); setOptimizationOptions(new HashMap<String, Boolean>(configuration.getOptimizationOptions()));
/** * Create a new GroovyScriptFactory for the given script source, * specifying a strategy interface that can customize Groovy's compilation * process within the underlying GroovyClassLoader. * @param scriptSourceLocator a locator that points to the source of the script. * Interpreted by the post-processor that actually creates the script. * @param compilationCustomizers one or more customizers to be applied to the * GroovyClassLoader compiler configuration * @since 4.3.3 * @see CompilerConfiguration#addCompilationCustomizers * @see org.codehaus.groovy.control.customizers.ImportCustomizer */ public GroovyScriptFactory(String scriptSourceLocator, CompilationCustomizer... compilationCustomizers) { this(scriptSourceLocator); if (!ObjectUtils.isEmpty(compilationCustomizers)) { this.compilerConfiguration = new CompilerConfiguration(); this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers); } }
private GroovyShell createShell() { CompilerConfiguration compilerSettings = new CompilerConfiguration(); compilerSettings.setScriptBaseClass(DelegatingScript.class.getName()); return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings); } }
public Object run() { CompilerConfiguration configuration = new CompilerConfiguration(); configuration.setClasspath(mgr.getClassPath()); return new GroovyClassLoader(finalParent, configuration); } });
public void testClassLoader() { Binding context = new Binding(); CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(DerivedScript.class.getName()); GroovyShell shell = new GroovyShell(context, config); String script = "evaluate '''\n"+ "class XXXX{}\n"+ "assert evaluate('XXXX') == XXXX\n"+ "'''"; shell.evaluate(script); }
public long execute() throws Exception { ClassLoader cl = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader().getParent()); GroovyClassLoader gcl = new GroovyClassLoader(cl); CompilationUnit cu = new CompilationUnit(new CompilerConfiguration(), null, gcl, new GroovyClassLoader(this.getClass().getClassLoader())); for (File source : sources) { cu.addSource(source); } long sd = System.nanoTime(); cu.compile(CompilePhase.CLASS_GENERATION.getPhaseNumber()); long dur = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sd); return dur; } }
@Override public void afterPropertiesSet() throws Exception { if (this.beanFactory != null && this.beanFactory instanceof ConfigurableListableBeanFactory) { ((ConfigurableListableBeanFactory) this.beanFactory).ignoreDependencyType(MetaClass.class); } CompilerConfiguration compilerConfig = this.compilerConfiguration; if (compilerConfig == null && this.compileStatic) { compilerConfig = new CompilerConfiguration(); compilerConfig.addCompilationCustomizers(new ASTTransformationCustomizer(CompileStatic.class)); } this.groovyClassLoader = new GroovyClassLoader(this.beanClassLoader, compilerConfig); }
@Override public void setup() { CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass("org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL"); ImportCustomizer ic = new ImportCustomizer(); ic.addStarImports("org.codehaus.groovy.ast.expr"); ic.addStaticStars("org.codehaus.groovy.ast.ClassHelper"); ic.addStaticStars("org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport"); config.addCompilationCustomizers(ic); final GroovyClassLoader transformLoader = compilationUnit!=null?compilationUnit.getTransformLoader():typeCheckingVisitor.getSourceUnit().getClassLoader(); Class<?> clazz = transformLoader.loadClass(scriptPath, false, true); if (TypeCheckingDSL.class.isAssignableFrom(clazz)) { script = (TypeCheckingDSL) clazz.newInstance(); context.getErrorCollector().addFatalError( new SimpleMessage("Static type checking extension '" + scriptPath + "' could not be loaded because it doesn't have a constructor accepting StaticTypeCheckingVisitor.", config.getDebug(), typeCheckingVisitor.getSourceUnit()) ); config.getDebug(), typeCheckingVisitor.getSourceUnit())); GroovyShell shell = new GroovyShell(transformLoader, new Binding(), config); script = (TypeCheckingDSL) shell.parse( new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding()) ); } catch (CompilationFailedException e) {
@Override protected void setUp() throws Exception { CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(GroovyScriptCommand.class.getName()); // loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config); shell = new GroovyShell(loader); }
public static CompilerConfiguration generateCompilerConfigurationFromOptions(CommandLine cli) { // // Setup the configuration data CompilerConfiguration configuration = new CompilerConfiguration(); if (cli.hasOption("classpath")) { configuration.setClasspath(cli.getOptionValue("classpath")); } if (cli.hasOption('d')) { configuration.setTargetDirectory(cli.getOptionValue('d')); } if (cli.hasOption("encoding")) { configuration.setSourceEncoding(cli.getOptionValue("encoding")); } // joint compilation parameters if (cli.hasOption('j')) { Map compilerOptions = new HashMap(); String[] opts = cli.getOptionValues("J"); compilerOptions.put("namedValues", opts); opts = cli.getOptionValues("F"); compilerOptions.put("flags", opts); configuration.setJointCompilationOptions(compilerOptions); } return configuration; }
CompilerConfiguration conf = new CompilerConfiguration(); conf.setDebug(true); shell = new GroovyShell(conf); if (addClasspath != null && addClasspath.length() > 0) { for (File fcp : Files.listPathsFiles(addClasspath)) { throw new ProcessException("Path not found `" + fcp + "` for `" + ADD_CLASSPATH.getDisplayName() + "`"); shell.getClassLoader().addClasspath(fcp.toString()); shell.getClassLoader().addClasspath(groovyClasspath);
public static GroovyClassLoader newEvaluator(Imports imports, Classpath classpath, String outDir, ImportCustomizer icz, ClassLoader parent) { try { Class.forName("org.codehaus.groovy.control.customizers.ImportCustomizer"); } catch (ClassNotFoundException e1) { String gjp = System.getenv(GROOVY_JAR_PATH); String errorMsg = null; if (gjp != null && !gjp.isEmpty()) { errorMsg = "Groovy libary not found, GROOVY_JAR_PATH = " + gjp; } else { errorMsg = "Default groovy libary not found. No GROOVY_JAR_PATH variable set."; } throw new GroovyNotFoundException(errorMsg); } icz = addImportsCustomizer(icz, imports); CompilerConfiguration config = new CompilerConfiguration().addCompilationCustomizers(icz); String acloader_cp = String.join(File.pathSeparatorChar + "", classpath.getPathsAsStrings()); config.setClasspath(acloader_cp); return new GroovyClassLoader(parent, config); }
/** * Construct a GroovyScriptEngine that will use the supplied ScriptLocation. * * @param script location of the .groovy script file * @throws EngineException if there is an exception loading, parsing, or constructing the test from the * file. */ public GroovyScriptEngine(ScriptLocation script) throws EngineException { // Get groovy to compile the script and access the callable closure final ClassLoader parent = getClass().getClassLoader(); CompilerConfiguration configuration = new CompilerConfiguration(); configuration.setSourceEncoding("UTF-8"); final GroovyClassLoader loader = new GroovyClassLoader(parent, configuration, true); try { m_groovyClass = loader.parseClass(script.getFile()); m_grinderRunner = new GrinderContextExecutor(m_groovyClass); m_grinderRunner.runBeforeProcess(); assert m_grinderRunner.testCount() > 0; } catch (IOException io) { throw new EngineException("Unable to parse groovy script at: " + script.getFile().getAbsolutePath(), io); } catch (Throwable e) { throw new EngineException("Error while initialize test runner", e); } }
/** * Construct a new configuration loader. * @param loader The class loader to use. */ public ConfigurationLoader(ClassLoader loader) { classLoader = loader != null ? loader : ClassLoaders.inferDefault(ConfigurationLoader.class); binding = new Binding(); CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(LenskitConfigScript.class.getName()); ImportCustomizer imports = new ImportCustomizer(); imports.addStarImports("org.lenskit.api"); imports.addStarImports("org.lenskit.basic"); config.addCompilationCustomizers(imports); shell = new GroovyShell(loader, binding, config); directory = ClassDirectory.forClassLoader(classLoader); }
private GroovyScriptEngine create() { // custom the configuration of the compiler CompilerConfiguration cc = new CompilerConfiguration(); cc.setTargetDirectory(new File(applicationDirectories.getTemporaryDirectory(), "groovy-classes")); cc.setSourceEncoding("UTF-8"); cc.addCompilationCustomizers(secureASTCustomizer()); GroovyClassLoader gcl = new GroovyClassLoader(classLoader, cc); engine = new GroovyScriptEngine(gcl); // HACK: For testing log.info("Created engine: {}", engine); return engine; }
public GroovyConditionShell() { SecureASTCustomizer secureASTCustomizer = createSecureASTCustomizer(); ImportCustomizer importCustomizer = createImportCustomizer(); ASTTransformationCustomizer astTransformationCustomizer = createASTTransformationCustomizer(); CompilerConfiguration compilerConfiguration = new CompilerConfiguration(); compilerConfiguration.addCompilationCustomizers(secureASTCustomizer); compilerConfiguration.addCompilationCustomizers(importCustomizer); compilerConfiguration.addCompilationCustomizers(astTransformationCustomizer); this.shell = new GroovyShell(compilerConfiguration); }
public GroovyRemoteClient(final ClassLoader classLoader, final File classesDir, final List<File> extraDirs, final URL url) throws IOException { checkNotNull(classLoader); log.debug("Class loader: {}", classLoader); checkNotNull(classesDir); log.debug("Classes dir: {}", classesDir); checkNotNull(url); log.debug("URL: {}", url); Binding binding = new Binding(); CompilerConfiguration cc = new CompilerConfiguration(); cc.setTargetDirectory(classesDir); this.shell = new GroovyShell(classLoader, binding, cc); HttpTransport transport = new HttpTransport(url.toExternalForm()); // provide transport with custom class-loader which includes generated classes List<URL> urls = Lists.newArrayList(); urls.add(classesDir.toURI().toURL()); for (File dir : extraDirs) { urls.add(dir.toURI().toURL()); } // TODO: Could probably use GCL here instead, would be simpler ClassLoader cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), shell.getClassLoader()); this.remote = new RemoteControl(transport, cl); }
setWarningLevel(WarningMessage.LIKELY_ERRORS); setOutput(null); setTargetDirectory((File) null); setClasspath(""); setVerbose(false); setDebug(false); setParameters(getSystemPropertySafe("groovy.parameters") != null); setTolerance(10); setScriptBaseClass(null); setRecompileGroovySource(false); setMinimumRecompilationInterval(100); setTargetBytecode(getSystemPropertySafe("groovy.target.bytecode", getMinBytecodeVersion())); setDefaultScriptExtension(getSystemPropertySafe("groovy.default.scriptExtension", ".groovy")); setSourceEncoding(encoding); setOutput(new PrintWriter(System.err)); } catch (Exception e) { setTargetDirectory(target); if (DEFAULT!=null && Boolean.TRUE.equals(DEFAULT.getOptimizationOptions().get(INVOKEDYNAMIC))) { indy = true; options.put(INVOKEDYNAMIC, Boolean.TRUE); setOptimizationOptions(options);