Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Created March 3, 2020 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioaniche/782a483b2913cff52c3d46d7372d0d43 to your computer and use it in GitHub Desktop.
Save mauricioaniche/782a483b2913cff52c3d46d7372d0d43 to your computer and use it in GitHub Desktop.
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.ModifierVisitor;
import com.github.javaparser.ast.visitor.Visitable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Set;
public class Main {
static Set<String> logMethods = new HashSet<>();
static Set<String> logClasses = new HashSet<>();
static Set<String> combinations = new HashSet<>();
static {
logMethods.add("info");
logMethods.add("debug");
logMethods.add("warn");
logMethods.add("error");
logMethods.add("fatal");
logMethods.add("trace");
logClasses.add("log");
for(String methods : logMethods) {
for(String classes : logClasses) {
combinations.add(classes + "." + methods);
}
}
}
public static void main(String[] args) throws FileNotFoundException {
CompilationUnit cu = StaticJavaParser.parse(new FileInputStream("/Users/mauricioaniche/workspace/logremoval/fixture/A.java"));
cu.accept(new ModifierVisitor<Void>() {
@Override
public Visitable visit(final MethodCallExpr n, Void arg) {
String methodCall = n.toString().trim();
boolean logCall = combinations.stream().anyMatch(x -> methodCall.startsWith(x));
if(logCall) {
n.getParentNode().get().remove();
}
return super.visit(n, arg);
}
}, null);
System.out.println(cu.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment