Package org.codehaus.groovy.control

Examples of org.codehaus.groovy.control.SourceUnit


         {
            ClassNode classNode = (ClassNode)iter.next();
            ModuleNode module = classNode.getModule();
            if (module != null)
            {
               SourceUnit currentSunit = module.getContext();
               if (currentSunit instanceof JcrSourceUnit)
                  dependencies.add(((JcrSourceUnit)currentSunit).getUrl());
            }
         }
        
View Full Code Here


* @version $Revision$
*/
public abstract class TestParserSupport extends GroovyTestCase {

    public ModuleNode parse(String text, String description) throws Exception {
        SourceUnit unit = SourceUnit.create(description, text);
        unit.parse();
        unit.convert();

        return unit.getAST();
    }
View Full Code Here

    public void testSetsTheSourceLocatorOfItsSyntaxExceptionAsTheNameOfTheCorrespondingSourceUnitWhenInstantiated() {
        SyntaxException syntaxException = new SyntaxException(someString(), -1, -1);
        assertEquals("source locator", null, syntaxException.getSourceLocator());

        String sourceUnitName = someString();
        SourceUnit sourceUnit = SourceUnit.create(sourceUnitName, someString());

        new SyntaxErrorMessage(syntaxException, sourceUnit);
        assertEquals("source locator", sourceUnitName, syntaxException.getSourceLocator());
    }
View Full Code Here

* @author <a href="mailto:martin.kempf@gmail.com">Martin Kempf</a>
*/
public abstract class ASTTest extends TestCase {

    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();
    }
View Full Code Here

        ClassNode stored = classes.get(name);
        if (stored != null && stored != node) {
            // we have a duplicate class!
            // One possibility for this is, that we declared a script and a
            // class in the same file and named the class like the file
            SourceUnit nodeSource = node.getModule().getContext();
            SourceUnit storedSource = stored.getModule().getContext();
            String txt = "Invalid duplicate class definition of class " + node.getName() + " : ";
            if (nodeSource == storedSource) {
                // same class in same source
                txt += "The source " + nodeSource.getName() + " contains at least two definitions of the class " + node.getName() + ".\n";
                if (node.isScriptBody() || stored.isScriptBody()) {
                    txt += "One of the classes is an explicit generated class using the class statement, the other is a class generated from" +
                            " the script body based on the file name. Solutions are to change the file name or to change the class name.\n";
                }
            } else {
                txt += "The sources " + nodeSource.getName() + " and " + storedSource.getName() + " each contain a class with the name " + node.getName() + ".\n";
            }
            nodeSource.getErrorCollector().addErrorAndContinue(
                    new SyntaxErrorMessage(new SyntaxException(txt, node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), nodeSource)
            );
        }
View Full Code Here

                            ScriptCacheEntry entry = scriptCache.get(path);
                            Class clazz = null;
                            if (entry != null) clazz = entry.scriptClass;
                            if (GroovyScriptEngine.this.isSourceNewer(entry)) {
                                try {
                                    SourceUnit su = compilationUnit.addSource(url);
                                    return new LookupResult(su, null);
                                } finally {
                                    forceClose(conn);
                                }
                            } else {
View Full Code Here

        Expression init = node.getInitialExpression();
        if (init != null) init.visit(this);
    }

    protected void addError(String msg, ASTNode expr) {
        SourceUnit source = getSourceUnit();
        source.getErrorCollector().addErrorAndContinue(
                new SyntaxErrorMessage(new SyntaxException(msg + '\n', expr.getLineNumber(), expr.getColumnNumber(), expr.getLastLineNumber(), expr.getLastColumnNumber()), source)
        );
    }
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)) {
View Full Code Here

                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;
View Full Code Here

        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")
View Full Code Here

TOP

Related Classes of org.codehaus.groovy.control.SourceUnit

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.