Search in sources :

Example 1 with ClassCodeVisitorSupport

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

the class ImmutableASTTransformation method doAddConstructor.

private static void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) {
    cNode.addConstructor(constructorNode);
    // GROOVY-5814: Immutable is not compatible with @CompileStatic
    Parameter argsParam = null;
    for (Parameter p : constructorNode.getParameters()) {
        if ("args".equals(p.getName())) {
            argsParam = p;
            break;
        }
    }
    if (argsParam != null) {
        final Parameter arg = argsParam;
        ClassCodeVisitorSupport variableExpressionFix = new ClassCodeVisitorSupport() {

            @Override
            protected SourceUnit getSourceUnit() {
                return cNode.getModule().getContext();
            }

            @Override
            public void visitVariableExpression(final VariableExpression expression) {
                super.visitVariableExpression(expression);
                if ("args".equals(expression.getName())) {
                    expression.setAccessedVariable(arg);
                }
            }
        };
        variableExpressionFix.visitConstructor(constructorNode);
    }
}
Also used : ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 2 with ClassCodeVisitorSupport

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

the class ImmutableASTTransformation method doAddConstructor.

private void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) {
    cNode.addConstructor(constructorNode);
    // GROOVY-5814: Immutable is not compatible with @CompileStatic
    Parameter argsParam = null;
    for (Parameter p : constructorNode.getParameters()) {
        if ("args".equals(p.getName())) {
            argsParam = p;
            break;
        }
    }
    if (argsParam != null) {
        final Parameter arg = argsParam;
        ClassCodeVisitorSupport variableExpressionFix = new ClassCodeVisitorSupport() {

            @Override
            protected SourceUnit getSourceUnit() {
                return cNode.getModule().getContext();
            }

            @Override
            public void visitVariableExpression(final VariableExpression expression) {
                super.visitVariableExpression(expression);
                if ("args".equals(expression.getName())) {
                    expression.setAccessedVariable(arg);
                }
            }
        };
        variableExpressionFix.visitConstructor(constructorNode);
    }
}
Also used : ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 3 with ClassCodeVisitorSupport

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

the class PluginAstReader method visitContents.

protected void visitContents(String className, final ClassNode classNode) {
    ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {

        @Override
        public void visitProperty(PropertyNode node) {
            String name = node.getName();
            final Expression expr = node.getField().getInitialExpression();
            if (expr != null) {
                Object value = null;
                if (expr instanceof ListExpression) {
                    final List<String> list = new ArrayList<String>();
                    for (Expression i : ((ListExpression) expr).getExpressions()) {
                        list.add(i.getText());
                    }
                    value = list;
                } else if (expr instanceof MapExpression) {
                    final Map<String, String> map = new LinkedHashMap<String, String>();
                    value = map;
                    for (MapEntryExpression mee : ((MapExpression) expr).getMapEntryExpressions()) {
                        map.put(mee.getKeyExpression().getText(), mee.getValueExpression().getText());
                    }
                } else {
                    if (expr instanceof ConstantExpression) {
                        value = expr.getText();
                    }
                }
                if (value != null) {
                    pluginInfo.setProperty(name, value);
                    super.visitProperty(node);
                }
            }
        }

        @Override
        protected SourceUnit getSourceUnit() {
            return classNode.getModule().getContext();
        }
    };
    classNode.visitContents(visitor);
    pluginInfo.setName(GrailsNameUtils.getPluginName(className + ".groovy"));
}
Also used : ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with ClassCodeVisitorSupport

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

the class MacroInvocationTrap method buildSubstitutionMap.

private MapExpression buildSubstitutionMap(final ASTNode expr) {
    final Map<MacroSubstitutionKey, ClosureExpression> map = new HashMap<MacroSubstitutionKey, ClosureExpression>();
    final MapExpression mapExpression = new MapExpression();
    ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {

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

        @Override
        public void visitClass(final ClassNode node) {
            super.visitClass(node);
            Iterator<InnerClassNode> it = node.getInnerClasses();
            while (it.hasNext()) {
                InnerClassNode next = it.next();
                visitClass(next);
            }
        }

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            super.visitMethodCallExpression(call);
            if (isBuildInvocation(call, MacroTransformation.DOLLAR_VALUE)) {
                ClosureExpression substitutionClosureExpression = getClosureArgument(call);
                if (substitutionClosureExpression == null) {
                    return;
                }
                Statement code = substitutionClosureExpression.getCode();
                if (code instanceof BlockStatement) {
                    ((BlockStatement) code).setVariableScope(null);
                }
                MacroSubstitutionKey key = new MacroSubstitutionKey(call, expr.getLineNumber(), expr.getColumnNumber());
                map.put(key, substitutionClosureExpression);
            }
        }
    };
    if (expr instanceof ClassNode) {
        visitor.visitClass((ClassNode) expr);
    } else {
        expr.visit(visitor);
    }
    for (Map.Entry<MacroSubstitutionKey, ClosureExpression> entry : map.entrySet()) {
        mapExpression.addMapEntryExpression(entry.getKey().toConstructorCallExpression(), entry.getValue());
    }
    return mapExpression;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) HashMap(java.util.HashMap) Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MacroSubstitutionKey(org.codehaus.groovy.macro.runtime.MacroSubstitutionKey) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ClassCodeVisitorSupport (org.codehaus.groovy.ast.ClassCodeVisitorSupport)4 Parameter (org.codehaus.groovy.ast.Parameter)2 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ClassNode (org.codehaus.groovy.ast.ClassNode)1 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)1 PropertyNode (org.codehaus.groovy.ast.PropertyNode)1 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)1 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)1 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)1 Statement (org.codehaus.groovy.ast.stmt.Statement)1 MacroSubstitutionKey (org.codehaus.groovy.macro.runtime.MacroSubstitutionKey)1