Search in sources :

Example 1 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project groovy by apache.

the class NotYetImplementedASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new RuntimeException("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }
    AnnotationNode annotationNode = (AnnotationNode) nodes[0];
    ASTNode node = nodes[1];
    if (!(node instanceof MethodNode)) {
        addError("@NotYetImplemented must only be applied on test methods!", node);
        return;
    }
    MethodNode methodNode = (MethodNode) node;
    ArrayList<Statement> statements = new ArrayList<Statement>();
    Statement statement = methodNode.getCode();
    if (statement instanceof BlockStatement) {
        statements.addAll(((BlockStatement) statement).getStatements());
    }
    if (statements.isEmpty())
        return;
    BlockStatement rewrittenMethodCode = new BlockStatement();
    rewrittenMethodCode.addStatement(tryCatchAssertionFailedError(annotationNode, methodNode, statements));
    rewrittenMethodCode.addStatement(throwAssertionFailedError(annotationNode));
    methodNode.setCode(rewrittenMethodCode);
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ASTNode(org.codehaus.groovy.ast.ASTNode) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 2 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project intellij-community by JetBrains.

the class GroovyCompilerWrapper method addErrorMessage.

private void addErrorMessage(GroovyRuntimeException exception) {
    ASTNode astNode = exception.getNode();
    ModuleNode module = exception.getModule();
    if (module == null) {
        module = findModule(astNode);
    }
    String moduleName = module == null ? "<no module>" : module.getDescription();
    int lineNumber = astNode == null ? -1 : astNode.getLineNumber();
    int columnNumber = astNode == null ? -1 : astNode.getColumnNumber();
    collector.add(new CompilerMessage(GroovyCompilerMessageCategories.ERROR, getExceptionMessage(exception), moduleName, lineNumber, columnNumber));
}
Also used : ASTNode(org.codehaus.groovy.ast.ASTNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 3 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project groovy by apache.

the class BytecodeSequence method visit.

/**
     * Delegates to the visit method used for this class.
     * If the visitor is a ClassGenerator, then 
     * {@link ClassGenerator#visitBytecodeSequence(BytecodeSequence)}
     * is called with this instance. If the visitor is no 
     * ClassGenerator, then this method will call visit on
     * each ASTNode element sorted by this class. If one
     * element is a BytecodeInstruction, then it will be skipped
     * as it is no ASTNode. 
     * 
     * @param visitor the visitor
     * @see ClassGenerator
     */
public void visit(GroovyCodeVisitor visitor) {
    if (visitor instanceof ClassGenerator) {
        ClassGenerator gen = (ClassGenerator) visitor;
        gen.visitBytecodeSequence(this);
        return;
    }
    for (Iterator iterator = instructions.iterator(); iterator.hasNext(); ) {
        Object part = (Object) iterator.next();
        if (part instanceof ASTNode) {
            ((ASTNode) part).visit(visitor);
        }
    }
}
Also used : Iterator(java.util.Iterator) ASTNode(org.codehaus.groovy.ast.ASTNode)

Example 4 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project hale by halestudio.

the class ASTToGraphVisitor method createNode.

@Override
public Vertex createNode(Object node) {
    if (node instanceof ASTNode) {
        ASTNode astn = (ASTNode) node;
        Vertex v = graph.addVertex(null);
        // fill vertex with info
        v.setProperty(P_AST_NODE, astn);
        v.setProperty(P_AST_TYPE, astn.getClass().getSimpleName());
        v.setProperty(P_START_LINE, astn.getLineNumber());
        v.setProperty(P_START_COL, astn.getColumnNumber());
        v.setProperty(P_END_LINE, astn.getLastLineNumber());
        v.setProperty(P_END_COL, astn.getLastColumnNumber());
        return v;
    }
    return null;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ASTNode(org.codehaus.groovy.ast.ASTNode)

Example 5 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project freeplane by freeplane.

the class GroovyScript method handleScriptRuntimeException.

private void handleScriptRuntimeException(final GroovyRuntimeException e) {
    outStream.print("message: " + e.getMessage());
    final ModuleNode module = e.getModule();
    final ASTNode astNode = e.getNode();
    int lineNumber = -1;
    if (module != null) {
        lineNumber = module.getLineNumber();
    } else if (astNode != null) {
        lineNumber = astNode.getLineNumber();
    } else {
        lineNumber = findLineNumberInString(e.getMessage(), lineNumber);
    }
    outStream.print("Line number: " + lineNumber);
    errorHandler.gotoLine(lineNumber);
    throw new ExecuteScriptException(e.getMessage() + " at line " + lineNumber, e);
}
Also used : ASTNode(org.codehaus.groovy.ast.ASTNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Aggregations

ASTNode (org.codehaus.groovy.ast.ASTNode)41 MethodNode (org.codehaus.groovy.ast.MethodNode)21 ClassNode (org.codehaus.groovy.ast.ClassNode)20 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)15 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)15 Expression (org.codehaus.groovy.ast.expr.Expression)13 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)11 Parameter (org.codehaus.groovy.ast.Parameter)11 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)11 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)11 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)10 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)9 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)9 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)9 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)9 ArrayList (java.util.ArrayList)8 Statement (org.codehaus.groovy.ast.stmt.Statement)8 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)7 LinkedList (java.util.LinkedList)6 ModuleNode (org.codehaus.groovy.ast.ModuleNode)6