private static void validate(GroovyCodeSource codeSource) { if (codeSource.getFile() == null) { if (codeSource.getScriptText() == null) { throw new IllegalArgumentException("Script text to compile cannot be null!"); } } }
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! } }
su = unit.addSource(url); } else { su = unit.addSource(codeSource.getName(), codeSource.getScriptText());
@Override public String asScript() { return script.getScriptText(); } }
private void validate(GroovyCodeSource codeSource) { if (codeSource.getFile() == null) { if (codeSource.getScriptText() == null) { throw new IllegalArgumentException("Script text to compile cannot be null!"); } } }
/** * When loading additional scripts, we need to remember its source text so that * when we come back from persistence we can load them again. */ @Override public Script parse(GroovyCodeSource codeSource) throws CompilationFailedException { Script s = doParse(codeSource); if (execution!=null) { execution.loadedScripts.put(s.getClass().getSimpleName(), codeSource.getScriptText()); execution.saveExecutionIfDurable(); } prepareScript(s); return s; }
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; }