Search in sources :

Example 76 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode 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 77 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project spring-boot by spring-projects.

the class SpringBootCompilerAutoConfiguration method addEnableAutoConfigurationAnnotation.

private void addEnableAutoConfigurationAnnotation(SourceUnit source, ClassNode classNode) {
    if (!hasEnableAutoConfigureAnnotation(classNode)) {
        AnnotationNode annotationNode = new AnnotationNode(ClassHelper.make("EnableAutoConfiguration"));
        classNode.addAnnotation(annotationNode);
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode)

Example 78 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project spring-boot by spring-projects.

the class SpringTestCompilerAutoConfiguration method apply.

@Override
public void apply(GroovyClassLoader loader, GroovyCompilerConfiguration configuration, GeneratorContext generatorContext, SourceUnit source, ClassNode classNode) throws CompilationFailedException {
    if (!AstUtils.hasAtLeastOneAnnotation(classNode, "RunWith")) {
        AnnotationNode runWith = new AnnotationNode(ClassHelper.make("RunWith"));
        runWith.addMember("value", new ClassExpression(ClassHelper.make("SpringRunner")));
        classNode.addAnnotation(runWith);
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 79 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project spock by spockframework.

the class SpecAnnotator method addBlockMetadata.

private void addBlockMetadata(Block block, BlockKind kind) {
    AnnotationNode blockAnn = new AnnotationNode(nodeCache.BlockMetadata);
    blockAnn.setMember(BlockMetadata.KIND, new PropertyExpression(new ClassExpression(nodeCache.BlockKind), kind.name()));
    ListExpression textExprs = new ListExpression();
    for (String text : block.getDescriptions()) textExprs.addExpression(new ConstantExpression(text));
    blockAnn.setMember(BlockMetadata.TEXTS, textExprs);
    blockAnnElems.addExpression(new AnnotationConstantExpression(blockAnn));
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode)

Example 80 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project spock by spockframework.

the class SpecAnnotator method addFieldMetadata.

private void addFieldMetadata(Field field) {
    AnnotationNode ann = new AnnotationNode(nodeCache.FieldMetadata);
    ann.setMember(FieldMetadata.NAME, new ConstantExpression(field.getName()));
    ann.setMember(FieldMetadata.ORDINAL, new ConstantExpression(field.getOrdinal()));
    ann.setMember(FieldMetadata.LINE, new ConstantExpression(field.getAst().getLineNumber()));
    field.getAst().addAnnotation(ann);
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode)

Aggregations

AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)173 ClassNode (org.codehaus.groovy.ast.ClassNode)117 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)67 FieldNode (org.codehaus.groovy.ast.FieldNode)43 MethodNode (org.codehaus.groovy.ast.MethodNode)39 Expression (org.codehaus.groovy.ast.expr.Expression)37 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)36 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)31 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)29 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)29 ArrayList (java.util.ArrayList)26 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)22 Parameter (org.codehaus.groovy.ast.Parameter)18 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)16 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)16 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)16 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)15 AST (antlr.collections.AST)14 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)14 Statement (org.codehaus.groovy.ast.stmt.Statement)14