/** * Adds a set of source files to the unit. */ public void addSources(File[] files) { for (File file : files) { addSource(file); } }
/** * Adds a set of file paths to the unit. */ public void addSources(String[] paths) { for (String path : paths) { addSource(new File(path)); } }
@Override public SourceUnit addSource(final File file) { if (hasAcceptedFileExtension(file.getName())) { return super.addSource(file); } return null; }
@Override public SourceUnit addSource(URL url) { if (hasAcceptedFileExtension(url.getPath())) { return super.addSource(url); } return null; }
/** * Compiles a single File. */ public void compile( File file ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( file ); unit.compile(); }
/** * Adds a source file to the unit. */ public SourceUnit addSource(URL url) { return addSource(new SourceUnit(url, configuration, classLoader, getErrorCollector())); }
/** * Adds a source file to the unit. */ public SourceUnit addSource(File file) { return addSource(new SourceUnit(file, configuration, classLoader, getErrorCollector())); }
public SourceUnit addSource(String name, String scriptText) { return addSource(new SourceUnit(name, scriptText, configuration, classLoader, getErrorCollector())); }
/** * Adds a InputStream source to the unit. */ public SourceUnit addSource(String name, InputStream stream) { ReaderSource source = new InputStreamReaderSource(stream, configuration); return addSource(new SourceUnit(name, source, configuration, classLoader, getErrorCollector())); }
unit.addSource(url); unit.compile(Phases.CLASS_GENERATION);
if (GroovyScriptEngine.this.isSourceNewer(entry)) { try { SourceUnit su = compilationUnit.addSource(url); return new LookupResult(su, null); } finally {
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }
/** * try to find a script using the compilation unit class loader. */ private static LookupResult tryAsScript(String name, CompilationUnit compilationUnit, ClassNode oldClass) { LookupResult lr = null; if (oldClass!=null) { lr = new LookupResult(null, oldClass); } if (name.startsWith("java.")) return lr; //TODO: don't ignore inner static classes completely if (name.indexOf('$') != -1) return lr; // try to find a script from classpath*/ GroovyClassLoader gcl = compilationUnit.getClassLoader(); URL url = null; try { url = gcl.getResourceLoader().loadGroovySource(name); } catch (MalformedURLException e) { // fall through and let the URL be null } if (url != null && ( oldClass==null || isSourceNewer(url, oldClass))) { SourceUnit su = compilationUnit.addSource(url); return new LookupResult(su,null); } return lr; }
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; } }
CompilerConfiguration config = new CompilerConfiguration(); SourceUnit su = new SourceUnit(name + "wrapper", "", config, loader, new ErrorCollector(config)); cu.addSource(su); cu.compile(Phases.CONVERSION); su.getAST().addClass(cn);
public ModuleNode getAST(String source, int untilPhase) { SourceUnit unit = SourceUnit.create("Test", source); CompilationUnit compUnit = new CompilationUnit(); compUnit.addSource(unit); compUnit.compile(untilPhase); return unit.getAST(); }
public void testTransitiveDep(){ cu.addSource("testTransitiveDep.gtest", new StringBufferInputStream( "class A1 {}\n" + "class A2 extends A1{}\n" + "class A3 extends A2{}\n" )); cu.compile(Phases.CLASS_GENERATION); cache.makeTransitiveHull(); Set<String> dep = cache.get("A1"); assertEquals(dep.size(),1); dep = cache.get("A2"); assertEquals(dep.size(),2); assertTrue(dep.contains("A1")); dep = cache.get("A3"); assertEquals(dep.size(),3); assertTrue(dep.contains("A1")); assertTrue(dep.contains("A2")); }
public void testDep(){ cu.addSource("testDep.gtest", new StringBufferInputStream( "class C1 {}\n" + "class C2 {}\n" + "class C3 {}\n" + "class A1 {C1 x}\n" + "class A2 extends C2{}\n" + "class A3 {C1 foo(C2 x){new C3()}}\n" )); cu.compile(Phases.CLASS_GENERATION); assertEquals(cache.get("C1").size(),1); assertEquals(cache.get("C2").size(),1); assertEquals(cache.get("C3").size(),1); Set<String> dep = cache.get("A1"); assertEquals(dep.size(),2); assertTrue(dep.contains("C1")); dep = cache.get("A2"); assertEquals(dep.size(),2); assertTrue(dep.contains("C2")); dep = cache.get("A3"); assertEquals(dep.size(),4); assertTrue(dep.contains("C1")); assertTrue(dep.contains("C2")); assertTrue(dep.contains("C3")); }