Search in sources :

Example 51 with ASTNode

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

the class TraitComposer method createException.

private static SyntaxException createException(ClassNode trait, ClassNode targetNode, MethodNode forwarder, MethodNode existingMethod) {
    String middle;
    ASTNode errorTarget;
    if (existingMethod.getLineNumber() == -1) {
        // came from a trait
        errorTarget = targetNode;
        List<AnnotationNode> allAnnos = existingMethod.getAnnotations(Traits.TRAITBRIDGE_CLASSNODE);
        AnnotationNode bridgeAnno = allAnnos == null ? null : allAnnos.get(0);
        String fromTrait = null;
        if (bridgeAnno != null) {
            Expression traitClass = bridgeAnno.getMember("traitClass");
            if (traitClass instanceof ClassExpression) {
                ClassExpression ce = (ClassExpression) traitClass;
                fromTrait = ce.getType().getNameWithoutPackage();
            }
        }
        middle = "in '" + targetNode.getNameWithoutPackage();
        if (fromTrait != null) {
            middle += "' from trait '" + fromTrait;
        }
    } else {
        errorTarget = existingMethod;
        middle = "declared in '" + targetNode.getNameWithoutPackage();
    }
    String message = "The static '" + forwarder.getName() + "' method " + middle + "' conflicts with the instance method having the same signature from trait '" + trait.getNameWithoutPackage() + "'";
    return new SyntaxException(message, errorTarget);
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ASTNode(org.codehaus.groovy.ast.ASTNode) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 52 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project micronaut-core by micronaut-projects.

the class GroovyVisitorContext method info.

@Override
public void info(String message, @Nullable Element element) {
    StringBuilder msg = new StringBuilder("Note: ").append(message);
    if (element != null) {
        ASTNode expr = (ASTNode) element.getNativeType();
        final String sample = sourceUnit.getSample(expr.getLineNumber(), expr.getColumnNumber(), new Janitor());
        msg.append("\n\n").append(sample);
    }
    System.out.println(msg);
}
Also used : Janitor(org.codehaus.groovy.control.Janitor) ASTNode(org.codehaus.groovy.ast.ASTNode)

Example 53 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project micronaut-core by micronaut-projects.

the class GroovyVisitorContext method warn.

@Override
public void warn(String message, @Nullable Element element) {
    StringBuilder msg = new StringBuilder("WARNING: ").append(message);
    if (element != null) {
        ASTNode expr = (ASTNode) element.getNativeType();
        final String sample = sourceUnit.getSample(expr.getLineNumber(), expr.getColumnNumber(), new Janitor());
        msg.append("\n\n").append(sample);
    }
    System.out.println(msg);
}
Also used : Janitor(org.codehaus.groovy.control.Janitor) ASTNode(org.codehaus.groovy.ast.ASTNode)

Aggregations

ASTNode (org.codehaus.groovy.ast.ASTNode)53 MethodNode (org.codehaus.groovy.ast.MethodNode)22 ClassNode (org.codehaus.groovy.ast.ClassNode)20 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)19 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)19 Expression (org.codehaus.groovy.ast.expr.Expression)18 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)15 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)13 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)13 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)13 Statement (org.codehaus.groovy.ast.stmt.Statement)13 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)11 Parameter (org.codehaus.groovy.ast.Parameter)11 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)11 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)10 ArrayList (java.util.ArrayList)9 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)9 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)9 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)7