All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codenarc.source.ExpressionCollector Maven / Gradle / Ivy

There is a newer version: 3.5.0-groovy-4.0
Show newest version
package org.codenarc.source;


import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.control.SourceUnit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class ExpressionCollector {

    Map> getMethodCalls(ModuleNode module) {

        Map> result = new HashMap>();

        if (module != null && module.getClasses() != null) {
            for (ClassNode classNode : module.getClasses()) {
                ExpressionCollectorVisitor collector = new ExpressionCollectorVisitor();
                collector.visitClass(classNode);
                result.put(classNode, collector.methodCalls);
            }
        }
        return result; 
    }

    private static class ExpressionCollectorVisitor extends ClassCodeVisitorSupport {
        
        private final List methodCalls = new ArrayList();

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            if (!methodCalls.contains(call)) {
                methodCalls.add(call);
            }
            super.visitMethodCallExpression(call);
        }

        @Override
        protected SourceUnit getSourceUnit() {
            throw new UnsupportedOperationException();
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy