Search in sources :

Example 96 with AnnotationNode

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

the class SourceURIASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        setScriptURIOnDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof FieldNode) {
        setScriptURIOnField((FieldNode) parent, node);
    } else {
        addError("Expected to find the annotation " + MY_TYPE_NAME + " on an declaration statement.", parent);
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression)

Example 97 with AnnotationNode

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

the class LogASTTransformation method visit.

public void visit(ASTNode[] nodes, final SourceUnit source) {
    init(nodes, source);
    AnnotatedNode targetClass = (AnnotatedNode) nodes[1];
    AnnotationNode logAnnotation = (AnnotationNode) nodes[0];
    final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
    final LoggingStrategy loggingStrategy = createLoggingStrategy(logAnnotation, classLoader);
    if (loggingStrategy == null)
        return;
    final String logFieldName = lookupLogFieldName(logAnnotation);
    final String categoryName = lookupCategoryName(logAnnotation);
    if (!(targetClass instanceof ClassNode))
        throw new GroovyBugError("Class annotation " + logAnnotation.getClassNode().getName() + " annotated no Class, this must not happen.");
    final ClassNode classNode = (ClassNode) targetClass;
    ClassCodeExpressionTransformer transformer = new ClassCodeExpressionTransformer() {

        private FieldNode logNode;

        @Override
        protected SourceUnit getSourceUnit() {
            return source;
        }

        public Expression transform(Expression exp) {
            if (exp == null)
                return null;
            if (exp instanceof MethodCallExpression) {
                return transformMethodCallExpression(exp);
            }
            return super.transform(exp);
        }

        @Override
        public void visitClass(ClassNode node) {
            FieldNode logField = node.getField(logFieldName);
            if (logField != null && logField.getOwner().equals(node)) {
                addError("Class annotated with Log annotation cannot have log field declared", logField);
            } else if (logField != null && !Modifier.isPrivate(logField.getModifiers())) {
                addError("Class annotated with Log annotation cannot have log field declared because the field exists in the parent class: " + logField.getOwner().getName(), logField);
            } else {
                logNode = loggingStrategy.addLoggerFieldToClass(node, logFieldName, categoryName);
            }
            super.visitClass(node);
        }

        private Expression transformMethodCallExpression(Expression exp) {
            MethodCallExpression mce = (MethodCallExpression) exp;
            if (!(mce.getObjectExpression() instanceof VariableExpression)) {
                return exp;
            }
            VariableExpression variableExpression = (VariableExpression) mce.getObjectExpression();
            if (!variableExpression.getName().equals(logFieldName) || !(variableExpression.getAccessedVariable() instanceof DynamicVariable)) {
                return exp;
            }
            String methodName = mce.getMethodAsString();
            if (methodName == null)
                return exp;
            if (usesSimpleMethodArgumentsOnly(mce))
                return exp;
            variableExpression.setAccessedVariable(logNode);
            if (!loggingStrategy.isLoggingMethod(methodName))
                return exp;
            return loggingStrategy.wrapLoggingMethodCall(variableExpression, methodName, exp);
        }

        private boolean usesSimpleMethodArgumentsOnly(MethodCallExpression mce) {
            Expression arguments = mce.getArguments();
            if (arguments instanceof TupleExpression) {
                TupleExpression tuple = (TupleExpression) arguments;
                for (Expression exp : tuple.getExpressions()) {
                    if (!isSimpleExpression(exp))
                        return false;
                }
                return true;
            }
            return !isSimpleExpression(arguments);
        }

        private boolean isSimpleExpression(Expression exp) {
            if (exp instanceof ConstantExpression)
                return true;
            if (exp instanceof VariableExpression)
                return true;
            return false;
        }
    };
    transformer.visitClass(classNode);
    // GROOVY-6373: references to 'log' field are normally already FieldNodes by now, so revisit scoping
    new VariableScopeVisitor(sourceUnit, true).visitClass(classNode);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) GroovyClassLoader(groovy.lang.GroovyClassLoader) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor) ClassCodeExpressionTransformer(org.codehaus.groovy.ast.ClassCodeExpressionTransformer)

Example 98 with AnnotationNode

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

the class MemoizedASTTransformation method buildDelegatingMethod.

private MethodNode buildDelegatingMethod(final MethodNode annotatedMethod, final ClassNode ownerClassNode) {
    Statement code = annotatedMethod.getCode();
    int access = ACC_PROTECTED;
    if (annotatedMethod.isStatic()) {
        access = ACC_PRIVATE | ACC_STATIC;
    }
    MethodNode method = new MethodNode(buildUniqueName(ownerClassNode, METHOD_LABEL, annotatedMethod), access, annotatedMethod.getReturnType(), cloneParams(annotatedMethod.getParameters()), annotatedMethod.getExceptions(), code);
    List<AnnotationNode> sourceAnnotations = annotatedMethod.getAnnotations();
    method.addAnnotations(new ArrayList<AnnotationNode>(sourceAnnotations));
    return method;
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 99 with AnnotationNode

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

the class NewifyASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    this.source = source;
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes));
    }
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode())) {
        internalError("Transformation called from wrong annotation: " + node.getClassNode().getName());
    }
    boolean autoFlag = determineAutoFlag(node.getMember("auto"));
    Expression value = node.getMember("value");
    if (parent instanceof ClassNode) {
        newifyClass((ClassNode) parent, autoFlag, determineClasses(value, false));
    } else if (parent instanceof MethodNode || parent instanceof FieldNode) {
        newifyMethodOrField(parent, autoFlag, determineClasses(value, false));
    } else if (parent instanceof DeclarationExpression) {
        newifyDeclaration((DeclarationExpression) parent, autoFlag, determineClasses(value, true));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression)

Example 100 with AnnotationNode

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

the class AbstractASTTransformation method copyAnnotatedNodeAnnotations.

/**
     * Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
     * and {@link java.lang.annotation.RetentionPolicy#CLASS}.
     * <p>
     * Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported for now.
     */
protected List<AnnotationNode> copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, String myTypeName) {
    final ArrayList<AnnotationNode> copiedAnnotations = new ArrayList<AnnotationNode>();
    final ArrayList<AnnotationNode> notCopied = new ArrayList<AnnotationNode>();
    GeneralUtils.copyAnnotatedNodeAnnotations(annotatedNode, copiedAnnotations, notCopied);
    for (AnnotationNode annotation : notCopied) {
        addError(myTypeName + " does not support keeping Closure annotation members.", annotation);
    }
    return copiedAnnotations;
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList)

Aggregations

AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)170 ClassNode (org.codehaus.groovy.ast.ClassNode)114 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)67 FieldNode (org.codehaus.groovy.ast.FieldNode)42 MethodNode (org.codehaus.groovy.ast.MethodNode)38 Expression (org.codehaus.groovy.ast.expr.Expression)34 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)33 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)28 ArrayList (java.util.ArrayList)26 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)26 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)26 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)19 Parameter (org.codehaus.groovy.ast.Parameter)18 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)16 AST (antlr.collections.AST)14 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)14 Statement (org.codehaus.groovy.ast.stmt.Statement)14 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)13 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)13 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)13