Java Code Examples for org.codehaus.groovy.control.SourceUnit#getAST()

The following examples show how to use org.codehaus.groovy.control.SourceUnit#getAST() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PowsyblDslAstTransformation.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
protected void visit(SourceUnit sourceUnit, ClassCodeExpressionTransformer transformer) {
    LOGGER.trace("Apply AST transformation");
    ModuleNode ast = sourceUnit.getAST();
    BlockStatement blockStatement = ast.getStatementBlock();

    if (DEBUG) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(AstUtil.toString(blockStatement));
        }
    }

    List<MethodNode> methods = ast.getMethods();
    for (MethodNode methodNode : methods) {
        methodNode.getCode().visit(transformer);
    }

    blockStatement.visit(transformer);

    if (DEBUG) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(AstUtil.toString(blockStatement));
        }
    }
}
 
Example 2
Source File: TestMethodUtilTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ModuleNode createGroovyModuleNode() {
    final CompilerConfiguration conf = new CompilerConfiguration();
    conf.setTargetDirectory(compileDir);
    final CompilationUnit cu
            = new CompilationUnit(
                    new GroovyClassLoader(
                            getClass().getClassLoader()
                    )
            );
    cu.configure(conf);
    final SourceUnit sn = cu.addSource("AGroovyClass.groovy", groovyScript.toString());
    try {
        cu.compile();
    } catch (Exception e) {
        //this groovy compile bit didn't work when running tests
        //but did work when debugging them, so odd, but the AST is in
        //place either way, and is all we need for this
        this.log(e.getMessage());
    }
    final ModuleNode mn = sn.getAST();
    return mn;
}
 
Example 3
Source File: ImportCustomizer.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) {
    ModuleNode ast = source.getAST();

    // GROOVY-8399: apply import customizations only once per module
    if (!classNode.getName().equals(ast.getMainClassName())) return;

    for (Import anImport : imports) {
        switch (anImport.type) {
            case regular:
                ast.addImport(anImport.alias, anImport.classNode);
                break;
            case staticImport:
                ast.addStaticImport(anImport.classNode, anImport.field, anImport.alias);
                break;
            case staticStar:
                ast.addStaticStarImport(anImport.alias, anImport.classNode);
                break;
            case star:
                ast.addStarImport(anImport.star);
                break;
        }
    }
}
 
Example 4
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitSourceUnit(SourceUnit unit) {
	sourceUnit = unit;
	URI uri = sourceUnit.getSource().getURI();
	nodesByURI.put(uri, new ArrayList<>());
	classNodesByURI.put(uri, new ArrayList<>());
	stack.clear();
	ModuleNode moduleNode = unit.getAST();
	if (moduleNode != null) {
		visitModule(moduleNode);
	}
	sourceUnit = null;
	stack.clear();
}
 
Example 5
Source File: AstUtilTest.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
    ModuleNode ast = sourceUnit.getAST();
    BlockStatement blockStatement = ast.getStatementBlock();
    String ref = "this.print('hello')\n"; // AstNodeToScriptVisitor has hardcoded \n
    assertEquals(ref, AstUtil.toString(blockStatement));
}
 
Example 6
Source File: CalculatedTimeSeriesDslAstTransformation.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
protected void visit(SourceUnit sourceUnit, ClassCodeExpressionTransformer transformer, boolean debug) {
    LOGGER.trace("Apply AST transformation");
    ModuleNode ast = sourceUnit.getAST();
    BlockStatement blockStatement = ast.getStatementBlock();

    List<MethodNode> methods = ast.getMethods();
    for (MethodNode methodNode : methods) {
        methodNode.getCode().visit(transformer);
    }

    blockStatement.visit(transformer);
}
 
Example 7
Source File: GroovyDocParser.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Map<String, GroovyClassDoc> parseGroovy(String packagePath, String file, String src) throws RuntimeException {
    CompilerConfiguration config = new CompilerConfiguration();
    config.getOptimizationOptions().put(CompilerConfiguration.GROOVYDOC, true);
    CompilationUnit compUnit = new CompilationUnit(config);
    SourceUnit unit = new SourceUnit(file, src, config, null, new ErrorCollector(config));
    compUnit.addSource(unit);
    compUnit.compile(Phases.CONVERSION);
    ModuleNode root = unit.getAST();
    GroovydocVisitor visitor = new GroovydocVisitor(unit, packagePath, links);
    visitor.visitClass(root.getClasses().get(0));
    return visitor.getGroovyClassDocs();
}
 
Example 8
Source File: ASTTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
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();
}