Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Last active March 3, 2020 17:07
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/11b6e81f521998a1c194ff8d3b7b0efd to your computer and use it in GitHub Desktop.
Save mauricioaniche/11b6e81f521998a1c194ff8d3b7b0efd 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.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 {
logMethods.add("info");
logMethods.add("debug");
logMethods.add("warn");
logMethods.add("error");
logMethods.add("fatal");
logMethods.add("trace");
logClasses.add("log");
}
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) {
boolean isALogClass = n.getScope().isPresent() && (logClasses.stream().anyMatch(x -> n.getScope().get().toString().startsWith(x)));
boolean isALogMethod = logMethods.contains(n.getName().asString());
if(isALogClass && isALogMethod) {
System.out.println("log call!");
if(n.getParentNode().isPresent())
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