Search in sources :

Example 1 with MacroSubstitutionKey

use of org.codehaus.groovy.macro.runtime.MacroSubstitutionKey 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

HashMap (java.util.HashMap)1 Map (java.util.Map)1 ClassCodeVisitorSupport (org.codehaus.groovy.ast.ClassCodeVisitorSupport)1 ClassNode (org.codehaus.groovy.ast.ClassNode)1 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)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