String name = superClass.getName() + "$TraitAdapter"; ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(ClassNode.EMPTY_ARRAY), null); CompilationUnit cu = new CompilationUnit(loader); 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); cu.compile(Phases.CLASS_GENERATION); @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses(); for (GroovyClass groovyClass : classes) { if (groovyClass.getName().equals(name)) {
try { cache.get(depSourcePath); cu.addSource(getResourceConnection(depSourcePath).getURL()); } catch (ResourceException e) { cu.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { @Override public void call(final SourceUnit source, GeneratorContext context, ClassNode classNode) cu.setClassNodeResolver(new ClassNodeResolver() { @Override public LookupResult findClassNode(String origName, CompilationUnit compilationUnit) {
public ClassNode resolveClassNullable(String className) { ClassNode beingCompiled = unit.getAST().getClass(className); if (beingCompiled != null) { return beingCompiled; } ClassNodeResolver.LookupResult lookupResult = resolver.resolveName(className, unit); return lookupResult == null ? null :lookupResult.getClassNode(); }
/** * Compiles a single File. */ public void compile( File file ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( file ); unit.compile(); }
/** * Compiles a series of Files from file names. */ public void compile( String[] files ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSources( files ); unit.compile(); }
public void compile(String[] paths) throws Exception { unit.addSources(paths); unit.compile(); }
/** * 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(); }
CompilationUnit cu = new CompilationUnit(objectGroovyClassFactory.config); cu.addSource(name, script); try { cu.compile(Phases.CONVERSION); CompileUnit ast = cu.getAST(); if (ast.getClasses().size() > 0) { ClassNode classNode= (ClassNode)ast.getClasses().get(0);
CompilationUnit unit = new CompilationUnit(); unit.setClassgenCallback(search); unit.addSource(url); unit.compile(Phases.CLASS_GENERATION);
/** * A helper method that can be used to evaluate expressions as found in annotation * parameters. For example, it will evaluate a constant, be it referenced directly as * an integer or as a reference to a field. * <p> * If this method throws an exception, then the expression cannot be evaluated on its own. * * @param expr the expression to be evaluated * @param config the compiler configuration * @return the result of the expression */ public static Object evaluateExpression(Expression expr, CompilerConfiguration config) { String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$'); ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, OBJECT_TYPE); ReturnStatement code = new ReturnStatement(expr); addGeneratedMethod(node, "eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code); CompilerConfiguration copyConf = new CompilerConfiguration(config); CompilationUnit cu = new CompilationUnit(copyConf); cu.addClassNode(node); cu.compile(Phases.CLASS_GENERATION); @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses(); Class aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes()); try { return aClass.getMethod("eval").invoke(null); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new GroovyBugError(e); } }
CompilationUnit cu = new CompilationUnit(gcl); cu.configure(conf); cu.addSource("PluginConfigurator", code); cu.compile(); } catch (Exception ex) { if (action != null) {
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")); }
unit.addFirstPhaseOperation(TimestampAdder.INSTANCE, CompilePhase.CLASS_GENERATION.getPhaseNumber()); su = unit.addSource(file); } else { URL url = codeSource.getURL(); if (url != null) { su = unit.addSource(url); } else { su = unit.addSource(codeSource.getName(), codeSource.getScriptText()); unit.setClassgenCallback(collector); int goalPhase = Phases.CLASS_GENERATION; if (config != null && config.getTargetDirectory() != null) goalPhase = Phases.OUTPUT; unit.compile(goalPhase);
/** * Adds a source file to the unit. */ public SourceUnit addSource(URL url) { return addSource(new SourceUnit(url, configuration, classLoader, getErrorCollector())); }
/** * Adds a set of source files to the unit. */ public void addSources(File[] files) { for (File file : files) { addSource(file); } }
/** * Loads the given class node returning the implementation Class. * <p> * WARNING: this compilation is not synchronized * * @param classNode * @return a class */ public Class defineClass(ClassNode classNode, String file, String newCodeBase) { CodeSource codeSource = null; try { codeSource = new CodeSource(new URL("file", "", newCodeBase), (java.security.cert.Certificate[]) null); } catch (MalformedURLException e) { //swallow } CompilationUnit unit = createCompilationUnit(config, codeSource); ClassCollector collector = createCollector(unit, classNode.getModule().getContext()); try { unit.addClassNode(classNode); unit.setClassgenCallback(collector); unit.compile(Phases.CLASS_GENERATION); definePackageInternal(collector.generatedClass.getName()); return collector.generatedClass; } catch (CompilationFailedException e) { throw new RuntimeException(e); } }
@Override protected void setUp() throws Exception { super.setUp(); cu = new CompilationUnit(); cache = new StringSetMap(); cu.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { @Override public void call(final SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { DependencyTracker dt = new DependencyTracker(source,cache); dt.visitClass(classNode); } }, Phases.CLASS_GENERATION); }
/** * creates a new CompilationUnit. If you want to add additional * phase operations to the CompilationUnit (for example to inject * additional methods, variables, fields), then you should overwrite * this method. * * @param config the compiler configuration, usually the same as for this class loader * @param source the source containing the initial file to compile, more files may follow during compilation * @return the CompilationUnit */ protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) { return new CompilationUnit(config, source, this); }
private static void addPhaseOperationsForGlobalTransforms(CompilationUnit compilationUnit, Map<String, URL> transformNames, boolean isFirstScan) { GroovyClassLoader transformLoader = compilationUnit.getTransformLoader(); for (Map.Entry<String, URL> entry : transformNames.entrySet()) { try { compilationUnit.getErrorCollector().addWarning(new WarningMessage( WarningMessage.POSSIBLE_ERRORS, "Transform Class " + entry.getKey() + " is specified as a global transform in " + entry.getValue().toExternalForm() compilationUnit.addPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber()); } else { compilationUnit.addNewPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber()); compilationUnit.getErrorCollector().addError(new SimpleMessage( "Transform Class " + entry.getKey() + " specified at " + entry.getValue().toExternalForm() + " is not an ASTTransformation.", null)); compilationUnit.getErrorCollector().addError(new SimpleMessage( "Could not instantiate global transform class " + entry.getKey() + " specified at " + entry.getValue().toExternalForm() + " because of exception " + e.toString(), null));
/** * 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; }