Search in sources :

Example 91 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.

the class GrailsApplicationCompilerAutoConfiguration method createGrabAnnotation.

public static AnnotationNode createGrabAnnotation(String group, String module, String version, String classifier, String type, boolean transitive) {
    AnnotationNode annotationNode = new AnnotationNode(new ClassNode(Grab.class));
    annotationNode.addMember("group", new ConstantExpression(group));
    annotationNode.addMember("module", new ConstantExpression(module));
    annotationNode.addMember("version", new ConstantExpression(version));
    if (classifier != null) {
        annotationNode.addMember("classifier", new ConstantExpression(classifier));
    }
    if (type != null) {
        annotationNode.addMember("type", new ConstantExpression(type));
    }
    annotationNode.addMember("transitive", new ConstantExpression(transitive));
    annotationNode.addMember("initClass", new ConstantExpression(false));
    return annotationNode;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Grab(groovy.lang.Grab)

Example 92 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.

the class DirtiesRuntimeTransformation method visit.

@Override
public void visit(final ASTNode[] astNodes, final SourceUnit source) {
    if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof MethodNode)) {
        throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
    }
    final MethodNode methodNode = (MethodNode) astNodes[1];
    final ClassNode declaringClassNode = methodNode.getDeclaringClass();
    if (declaringClassNode != null) {
        final List<AnnotationNode> annotations = declaringClassNode.getAnnotations(ClassHelper.make(FreshRuntime.class));
        if (annotations != null && annotations.size() > 0) {
            final String message = "The [" + methodNode.getName() + "] method in [" + declaringClassNode.getName() + "] is marked with @DirtiesRuntime.  " + "There is no need to mark a test method with @DirtiesRuntime " + "inside of a test class which is marked with @FreshRuntime.";
            GrailsASTUtils.warning(source, methodNode, message);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) FreshRuntime(grails.test.runtime.FreshRuntime)

Example 93 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.

the class TestMixinTransformation method addSpockFieldMetadata.

private void addSpockFieldMetadata(FieldNode field, int ordinal) {
    AnnotationNode ann = new AnnotationNode(ClassHelper.make(FieldMetadata.class));
    ann.setMember(FieldMetadata.NAME, new ConstantExpression(field.getName()));
    ann.setMember(FieldMetadata.ORDINAL, new ConstantExpression(ordinal));
    ann.setMember(FieldMetadata.LINE, new ConstantExpression(field.getLineNumber()));
    field.addAnnotation(ann);
}
Also used : FieldMetadata(org.spockframework.runtime.model.FieldMetadata) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 94 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.

the class TestMixinTransformation method visit.

public void visit(ASTNode[] astNodes, SourceUnit source) {
    if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof AnnotatedNode)) {
        throw new RuntimeException("Internal error: wrong types: " + astNodes[0].getClass() + " / " + astNodes[1].getClass());
    }
    AnnotatedNode parent = (AnnotatedNode) astNodes[1];
    AnnotationNode annotationNode = (AnnotationNode) astNodes[0];
    if (!MY_TYPE.equals(annotationNode.getClassNode()) || !(parent instanceof ClassNode)) {
        return;
    }
    String mainClass = MainClassFinder.searchMainClass(source.getSource().getURI());
    ClassNode applicationClassNode = null;
    if (mainClass != null) {
        applicationClassNode = ClassHelper.make(mainClass);
    }
    ClassNode classNode = (ClassNode) parent;
    ListExpression values = getListOfClasses(annotationNode);
    weaveTestMixins(classNode, values, applicationClassNode);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 95 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.

the class TestMixinTransformation method addJunitRuleFields.

protected void addJunitRuleFields(ClassNode classNode) {
    if (classNode.getField(JUNIT_ADAPTER_FIELD_NAME) != null) {
        return;
    }
    ClassNode junitAdapterType = ClassHelper.make(TestRuntimeJunitAdapter.class);
    FieldNode junitAdapterFieldNode = classNode.addField(JUNIT_ADAPTER_FIELD_NAME, Modifier.STATIC, junitAdapterType, new ConstructorCallExpression(junitAdapterType, MethodCallExpression.NO_ARGUMENTS));
    boolean spockTest = isSpockTest(classNode);
    FieldNode staticRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "StaticClassRule", Modifier.PRIVATE | Modifier.STATIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newClassRule", new ClassExpression(classNode)));
    AnnotationNode classRuleAnnotation = new AnnotationNode(ClassHelper.make(ClassRule.class));
    if (spockTest) {
        // @ClassRule must be added to @Shared field in spock
        FieldNode spockSharedRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "SharedClassRule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new FieldExpression(staticRuleFieldNode));
        spockSharedRuleFieldNode.addAnnotation(classRuleAnnotation);
        spockSharedRuleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Shared.class)));
        addSpockFieldMetadata(spockSharedRuleFieldNode, 0);
    } else {
        staticRuleFieldNode.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
        staticRuleFieldNode.addAnnotation(classRuleAnnotation);
    }
    FieldNode ruleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "Rule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newRule", new VariableExpression("this")));
    ruleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Rule.class)));
    if (spockTest) {
        addSpockFieldMetadata(ruleFieldNode, 0);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) TestRule(org.junit.rules.TestRule) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassRule(org.junit.ClassRule) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

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