org.codenarc.source.ExpressionCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of CodeNarc Show documentation
Show all versions of CodeNarc Show documentation
The CodeNarc project provides a static analysis tool for Groovy code.
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();
}
}
}