Package org.codehaus.groovy.control

Examples of org.codehaus.groovy.control.CompilationUnit$SourceUnitOperation


            cache.clear();
            localTh.set(null);
            return answer;
        }
        private String getPath(Class clazz, Map<String,String> precompiledEntries) {
            CompilationUnit cu = getLocalData().get().cu;
            String name = clazz.getName();
            ClassNode classNode = cu.getClassNode(name);
            if (classNode == null) {
                // this is a precompiled class!
                String path = precompiledEntries.get(name);
                if (path == null) throw new GroovyBugError("Precompiled class "+name+" should be available in precompiled entries map, but was not.");
                return path;
View Full Code Here


}

class CustomGroovyClassLoader extends GroovyClassLoader {

    protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
        CompilationUnit unit = super.createCompilationUnit(config, source);
        unit.addPhaseOperation(new CustomPrimaryClassNodeOperation(), Phases.SEMANTIC_ANALYSIS);
        return unit;
    }
View Full Code Here

                            }
                        }
                    };

                    final ClassLoader parent = theClass.getClassLoader();
                    CompilationUnit unit = new CompilationUnit();
                    unit.setClassgenCallback(search);
                    unit.addSource(url);
                    unit.compile(Phases.CLASS_GENERATION);
                }
                catch (Exception e) {
                    throw new GroovyRuntimeException("Exception thrown parsing: " + groovyFile + ". Reason: " + e, e);
                }
            }
View Full Code Here

            }
        }
        if (!traits.isEmpty()) {
            String name = superClass.getName() + "$TraitAdapter";
            ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(new ClassNode[traits.size()]), 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)) {
                    return loader.defineClass(name,groovyClass.getBytes());
                }
            }
View Full Code Here

                // Let's compile the groovy source
                final List<GroovyClass> groovyClassesForThisTemplate = new ArrayList<GroovyClass>();
                // ~~~ Please !
                CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
                compilerConfiguration.setSourceEncoding("utf-8"); // ouf
                CompilationUnit compilationUnit = new CompilationUnit(compilerConfiguration);
                compilationUnit.addSource(new SourceUnit(name, compiledSource, compilerConfiguration, tClassLoader, compilationUnit.getErrorCollector()));
                Field phasesF = compilationUnit.getClass().getDeclaredField("phaseOperations");
                phasesF.setAccessible(true);
                LinkedList[] phases = (LinkedList[]) phasesF.get(compilationUnit);
                LinkedList<GroovyClassOperation> output = new LinkedList<GroovyClassOperation>();
                phases[Phases.OUTPUT] = output;
                output.add(new GroovyClassOperation() {
                    public void call(GroovyClass gclass) {
                        groovyClassesForThisTemplate.add(gclass);
                    }
                });
                compilationUnit.compile();
                // ouf

                // Define script classes
                StringBuilder sb = new StringBuilder();
                sb.append("LINESMATRIX" + "\n");
View Full Code Here

    this.loader.clearCache();
    List<Class<?>> classes = new ArrayList<Class<?>>();

    CompilerConfiguration configuration = this.loader.getConfiguration();

    CompilationUnit compilationUnit = new CompilationUnit(configuration, null,
        this.loader);
    ClassCollector collector = this.loader.createCollector(compilationUnit, null);
    compilationUnit.setClassgenCallback(collector);

    for (String source : sources) {
      List<String> paths = ResourceUtils.getUrls(source, this.loader);
      for (String path : paths) {
        compilationUnit.addSource(new URL(path));
      }
    }

    addAstTransformations(compilationUnit);

    compilationUnit.compile(Phases.CLASS_GENERATION);
    for (Object loadedClass : collector.getLoadedClasses()) {
      classes.add((Class<?>) loadedClass);
    }
    ClassNode mainClassNode = getMainClass(compilationUnit);
View Full Code Here

    public Code compile(final Sources sources) throws CompileException {
        if (sources == null) {
            throw new IllegalArgumentException("Sources are null.");
        }
        try {
            CompilationUnit cu = new CompilationUnit(config, null, groovyClassLoader);
            Map<Source,SourceUnit> sourceUnitMap = new HashMap<Source,SourceUnit>();
            Set<Source> sourceSet = sources.getSourceSet();
            for (Source source : sourceSet) {
                SourceUnit su = addToCompilationUnit(cu, source, sources);
                //System.out.println("SU Name: " + su.getName());
                sourceUnitMap.put(source, su);
            }

            int phase = (config.getTargetDirectory() == null) ? Phases.CLASS_GENERATION : Phases.OUTPUT;
            cu.compile(phase);

            Map<Source,CompiledSourceInfo> compiledSourceInfos = new HashMap<Source,CompiledSourceInfo>();
            for (Entry<Source, SourceUnit> entry : sourceUnitMap.entrySet()) {
                Source source = entry.getKey();
                SourceUnit su = entry.getValue();
                Set<String> classNames = new HashSet<String>();
                List<ClassNode> nodes = su.getAST().getClasses();
                for (ClassNode node : nodes) {
                    classNames.add(node.getName());
                }
                CompiledSourceInfo compiledSourceInfo = new CompiledSourceInfo(source,
                        su.getAST().getMainClassName(), classNames, source.getLastModified());
                //System.out.println("SU MainClassName: " + su.getAST().getMainClassName());
                compiledSourceInfos.put(source, compiledSourceInfo);
            }

            @SuppressWarnings("unchecked")
            List<GroovyClass> groovyClasses = cu.getClasses();
            Map<String, Bytecode> bytecodes = new HashMap<String,Bytecode>();
            for (GroovyClass groovyClass : groovyClasses) {
                String name = groovyClass.getName();
                bytecodes.put(name, new Bytecode(name, groovyClass.getBytes()));
            }
View Full Code Here

                    throw new BuildException(ioe);
                }
            }
            return new JavaAwareCompilationUnit(configuration, buildClassLoaderFor());
        } else {
            return new CompilationUnit(configuration, null, buildClassLoaderFor());
        }
    }
View Full Code Here

        if (cu != null) {
            unit = cu;
        } else if (configuration.getJointCompilationOptions() != null) {
            unit = new JavaAwareCompilationUnit(configuration);
        } else {
            unit = new CompilationUnit(configuration);
        }
    }
View Full Code Here

    *  Compiles a single File.
    */
  
    public void compile( File file ) throws CompilationFailedException
    {
        CompilationUnit unit = new CompilationUnit( configuration );
        unit.addSource( file );
        unit.compile();
    }
View Full Code Here

TOP

Related Classes of org.codehaus.groovy.control.CompilationUnit$SourceUnitOperation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.