Search in sources :

Example 16 with AnnotationNode

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

the class AbstractGrailsArtefactTransformer method addEnhancedAnnotation.

protected void addEnhancedAnnotation(ClassNode classNode) {
    final AnnotationNode annotationNode = GrailsASTUtils.addEnhancedAnnotation(classNode);
    AnnotationNode annotation = GrailsASTUtils.findAnnotation(classNode, Mixin.class);
    if (annotation != null) {
        Expression value = annotation.getMember("value");
        if (value != null) {
            annotationNode.setMember("mixins", value);
        }
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression)

Example 17 with AnnotationNode

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

the class MockTransformation method visit.

@Override
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 node = (AnnotationNode) astNodes[0];
    if (!MY_TYPE.equals(node.getClassNode()) || !(parent instanceof ClassNode)) {
        return;
    }
    ClassNode classNode = (ClassNode) parent;
    String cName = classNode.getName();
    if (classNode.isInterface()) {
        error(source, "Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces.");
    }
    ListExpression values = getListOfClasses(node);
    if (values == null) {
        error(source, "Error processing class '" + cName + "'. " + MY_TYPE_NAME + " annotation expects a class or a list of classes to mock");
        return;
    }
    List<ClassExpression> domainClassNodes = new ArrayList<ClassExpression>();
    for (Expression expression : values.getExpressions()) {
        if (expression instanceof ClassExpression) {
            ClassExpression classEx = (ClassExpression) expression;
            ClassNode cn = classEx.getType();
            Class<?> mixinClassForArtefactType = getMixinClassForArtefactType(cn);
            if (mixinClassForArtefactType != null) {
                weaveMock(classNode, classEx, false);
            } else {
                domainClassNodes.add(classEx);
            }
        }
    }
    if (!domainClassNodes.isEmpty()) {
        weaveMixinClass(classNode, DomainClassUnitTestMixin.class);
        addMockCollaborators(classNode, "Domain", domainClassNodes);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Expression(org.codehaus.groovy.ast.expr.Expression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayList(java.util.ArrayList) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 18 with AnnotationNode

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

the class TestMixinTransformation method autoAddTestAnnotation.

private void autoAddTestAnnotation(ClassNode classNode) {
    if (isSpockTest(classNode))
        return;
    Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
    for (String methodName : declaredMethodsMap.keySet()) {
        MethodNode methodNode = declaredMethodsMap.get(methodName);
        ClassNode testAnnotationClassNode = TEST_ANNOTATION.getClassNode();
        List<AnnotationNode> existingTestAnnotations = methodNode.getAnnotations(testAnnotationClassNode);
        if (isCandidateMethod(methodNode) && (methodNode.getName().startsWith("test") || existingTestAnnotations.size() > 0)) {
            if (existingTestAnnotations.size() == 0) {
                ClassNode returnType = methodNode.getReturnType();
                if (returnType.getName().equals(VOID_TYPE)) {
                    methodNode.addAnnotation(TEST_ANNOTATION);
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode)

Example 19 with AnnotationNode

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

the class LineNumberTransform method visit.

// LOG statements commented out because they were causing
// compilation problems when used outside of grails
//    static final Log LOG = LogFactory.getLog(LineNumberTransform)
public void visit(ASTNode[] nodes, SourceUnit source) {
    List<ClassNode> classes = source.getAST().getClasses();
    AnnotationNode annotation = null;
    for (ClassNode clazz : classes) {
        annotation = findAnnotation(clazz);
        if (annotation != null) {
            break;
        }
    }
    if (annotation == null) {
        return;
    }
    int[] array = extractLineNumberArray(annotation);
    if (array != null) {
        LineNumberVisitor visitor = new LineNumberVisitor(array);
        for (ClassNode clazz : classes) {
            visitor.visitClass(clazz);
        }
    }
    String sourceName = extractSourceName(annotation);
    if (sourceName != null) {
        source.getAST().setDescription(sourceName);
        // source.name = sourceName
        Field field = ReflectionUtils.findField(SourceUnit.class, "name");
        field.setAccessible(true);
        ReflectionUtils.setField(field, source, sourceName);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Field(java.lang.reflect.Field) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode)

Example 20 with AnnotationNode

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

the class Java5 method setAnnotationMetaData.

private void setAnnotationMetaData(Annotation[] annotations, AnnotatedNode an) {
    for (Annotation annotation : annotations) {
        AnnotationNode node = new AnnotationNode(ClassHelper.make(annotation.annotationType()));
        configureAnnotation(node, annotation);
        an.addAnnotation(node);
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Annotation(java.lang.annotation.Annotation)

Aggregations

AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)248 ClassNode (org.codehaus.groovy.ast.ClassNode)158 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)78 Expression (org.codehaus.groovy.ast.expr.Expression)68 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)64 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)60 MethodNode (org.codehaus.groovy.ast.MethodNode)57 FieldNode (org.codehaus.groovy.ast.FieldNode)54 ArrayList (java.util.ArrayList)53 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)49 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)48 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)44 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)36 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)35 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)30 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)30 Parameter (org.codehaus.groovy.ast.Parameter)29 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)29 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)29 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)28