Refine search
/** * Parses the given file into a Java class capable of being run * * @param file the file name to parse * @return the main class defined in the given script */ public Class parseClass(File file) throws CompilationFailedException, IOException { return parseClass(new GroovyCodeSource(file, config.getSourceEncoding())); }
private Class doParseClass(GroovyCodeSource codeSource) { validate(codeSource); CompilationUnit unit = createCompilationUnit(config, codeSource.getCodeSource()); if (recompile!=null && recompile || recompile==null && config.getRecompileGroovySource()) { unit.addFirstPhaseOperation(TimestampAdder.INSTANCE, CompilePhase.CLASS_GENERATION.getPhaseNumber()); File file = codeSource.getFile(); if (file != null) { su = unit.addSource(file); } else { URL url = codeSource.getURL(); if (url != null) { su = unit.addSource(url); } else { su = unit.addSource(codeSource.getName(), codeSource.getScriptText()); ClassCollector collector = createCollector(unit, su); unit.setClassgenCallback(collector); int goalPhase = Phases.CLASS_GENERATION;
/** * Parses the given text into a Java class capable of being run * * @param text the text of the script/class to parse * @param fileName the file name to use as the name of the class * @return the main class defined in the given script */ public Class parseClass(final String text, final String fileName) throws CompilationFailedException { GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>() { public GroovyCodeSource run() { return new GroovyCodeSource(text, fileName, "/groovy/script"); } }); gcs.setCachable(false); return parseClass(gcs); }
private static void validate(GroovyCodeSource codeSource) { if (codeSource.getFile() == null) { if (codeSource.getScriptText() == null) { throw new IllegalArgumentException("Script text to compile cannot be null!"); } } }
@SuppressWarnings("unchecked") public MarkupTemplateMaker(final URL resource, Map<String, String> modelTypes) throws IOException { boolean cache = templateConfiguration.isCacheTemplates(); GroovyCodeSource codeSource; if (cache) { // we use a map in addition to the internal caching mechanism of Groovy because the latter // will always read from the URL even if it's cached String key = resource.toExternalForm(); codeSource = codeSourceCache.get(key); if (codeSource == null) { codeSource = new GroovyCodeSource(resource); codeSourceCache.put(key, codeSource); } } else { codeSource = new GroovyCodeSource(resource); } codeSource.setCachable(cache); templateClass = groovyClassLoader.parseClass(codeSource, modelTypes); this.modeltypes = modelTypes; }
private String genSourceCacheKey(GroovyCodeSource codeSource) { StringBuilder strToDigest; String scriptText = codeSource.getScriptText(); if (null != scriptText) { strToDigest = new StringBuilder((int) (scriptText.length() * 1.2)); strToDigest.append("scriptText:").append(scriptText); CodeSource cs = codeSource.getCodeSource(); if (null != cs) { strToDigest.append("/codeSource:").append(cs); } } else { strToDigest = new StringBuilder(32); // if the script text is null, i.e. the script content is invalid // use the name as cache key for the time being to trigger the validation by `groovy.lang.GroovyClassLoader.validate` // note: the script will not be cached due to the invalid script content, // so it does not matter even if cache key is not the md5 value of script content strToDigest.append("name:").append(codeSource.getName()); } try { return EncodingGroovyMethods.md5(strToDigest); } catch (NoSuchAlgorithmException e) { throw new GroovyRuntimeException(e); // should never reach here! } }
/** * 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(); }
public Class<?> run() { try { GroovyCodeSource codeSource = new GroovyCodeSource(scriptSource, className, BootstrapInfo.UNTRUSTED_CODEBASE); codeSource.setCachable(false); CompilerConfiguration configuration = new CompilerConfiguration() .addCompilationCustomizers(new ImportCustomizer().addStarImports("org.joda.time").addStaticStars("java.lang.Math")) .addCompilationCustomizers(new GroovyBigDecimalTransformer(CompilePhase.CONVERSION)); // always enable invokeDynamic, not the crazy softreference-based stuff configuration.getOptimizationOptions().put(GROOVY_INDY_SETTING_NAME, true); GroovyClassLoader groovyClassLoader = new GroovyClassLoader(loader, configuration); return groovyClassLoader.parseClass(codeSource); } catch (Throwable e) { if (logger.isTraceEnabled()) { logger.trace("Exception compiling Groovy script:", e); } throw new ScriptException("Error compiling script " + className, e); } } });
public GroovierJellyScript parse(URL script) throws IOException { // we do the caching on our own, so don't let GroovyClassLoader cache this. Or else // dynamic reloading won't work GroovyCodeSource gcs = new GroovyCodeSource(script); gcs.setCachable(false); return new GroovierJellyScript(gcl.parseClass(gcs),script); }
public GroovyCodeSource run() { return new GroovyCodeSource(in, fileName, DEFAULT_CODE_BASE); } });
if (source != null) { if (oldClass == null || isSourceNewer(source, oldClass)) { String name = source.toExternalForm(); if (isFile(source)) { try { return parseClass(new GroovyCodeSource(new File(source.toURI()), sourceEncoding)); } catch (URISyntaxException e) {
groovyClassLoader.setResourceLoader(NO_OP_GROOVY_RESOURCE_LOADER); String scriptText = source.getResource().getText(); String scriptName = source.getClassName(); GroovyCodeSource codeSource = new GroovyCodeSource(scriptText == null ? "" : scriptText, scriptName, "/groovy/script"); try { try { groovyClassLoader.parseClass(codeSource, false); } catch (MultipleCompilationErrorsException e) { wrapCompilationFailure(source, e);
ScriptCacheEntry origEntry = scriptCache.get(codeSource.getName()); Set<String> origDep = null; if (origEntry != null) origDep = origEntry.dependencies; if (origDep != null) cache.put(".", origDep); Class answer = super.parseClass(codeSource, false);
Class answer = (Class) sourceCache.get(codeSource.getName()); if (answer != null) return answer; CompilationUnit unit = createCompilationUnit(config, codeSource.getCodeSource()); SourceUnit su = null; if (codeSource.getFile() == null) { su = unit.addSource(codeSource.getName(), codeSource.getInputStream()); } else { su = unit.addSource(codeSource.getFile()); ClassCollector collector = createCollector(unit, su); unit.setClassgenCallback(collector); int goalPhase = Phases.CLASS_GENERATION; for (Iterator iter = collector.getLoadedClasses().iterator(); iter.hasNext();) { Class clazz = (Class) iter.next(); setClassCacheEntry(clazz); if (shouldCacheSource) sourceCache.put(codeSource.getName(), answer); return answer;
public void testCodeSource() throws IOException, CompilationFailedException { URL script = loader.getResource("groovy/ArrayTest.groovy"); try { new GroovyCodeSource(script); } catch (RuntimeException re) { assertEquals("Could not construct a GroovyCodeSource from a null URL", re.getMessage()); } }
private Class doParseClass(GroovyCodeSource codeSource) { validate(codeSource); Class answer; // Was neither already loaded nor compiling, so compile and add to cache. CompilationUnit unit = createCompilationUnit(config, codeSource.getCodeSource()); SourceUnit su = null; if (codeSource.getFile() == null) { su = unit.addSource(codeSource.getName(), codeSource.getScriptText()); } else { su = unit.addSource(codeSource.getFile()); } ClassCollector collector = createCollector(unit, su); unit.setClassgenCallback(collector); int goalPhase = Phases.CLASS_GENERATION; if (config != null && config.getTargetDirectory() != null) goalPhase = Phases.OUTPUT; unit.compile(goalPhase); answer = collector.generatedClass; String mainClass = su.getAST().getMainClassName(); for (Object o : collector.getLoadedClasses()) { Class clazz = (Class) o; String clazzName = clazz.getName(); definePackage(clazzName); setClassCacheEntry(clazz); if (clazzName.equals(mainClass)) answer = clazz; } return answer; }
protected void execute(GroovyCodeSource s) { try { createShell().evaluate(s); } catch (RuntimeException x) { LOGGER.log(WARNING, "Failed to run script " + s.getName(), x); } }
/** * Parses the given code source into a Java class. If there is a class file * for the given code source, then no parsing is done, instead the cached class is returned. * * @param shouldCacheSource if true then the generated class will be stored in the source cache * @return the main class defined in the given script */ public Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException { synchronized (sourceCache) { Class answer = sourceCache.get(codeSource.getName()); if (answer != null) return answer; answer = doParseClass(codeSource); if (shouldCacheSource) sourceCache.put(codeSource.getName(), answer); return answer; } }
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)); } }
public GroovierJellyScript parseGSP(URL res) throws IOException, ClassNotFoundException { GroovyCodeSource gcs = new GroovyCodeSource(parser.parse(res), res.toExternalForm(), res.toExternalForm()); gcs.setCachable(false); return new GroovierJellyScript(gcl.parseClass(gcs),res); } }