Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Last active May 21, 2020 20:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioaniche/b920ca74d721575958baf5673f7138e9 to your computer and use it in GitHub Desktop.
Save mauricioaniche/b920ca74d721575958baf5673f7138e9 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.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
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 IOException {
// careful: it's gonna overwrite the files!
String dir = "/Users/mauricioaniche/workspace/logremoval/fixture";
List<Path> javaFiles = Files.walk(Paths.get(dir))
.filter(Files::isRegularFile)
.filter(x -> !x.toAbsolutePath().toString().contains(".git"))
.map(x -> x.toAbsolutePath())
.collect(Collectors.toList());
for(Path javaFile : javaFiles) {
try {
CompilationUnit cu = StaticJavaParser.parse(new FileInputStream(javaFile.toString()));
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);
PrintWriter pw = new PrintWriter(new FileOutputStream(javaFile.toString()), false);
pw.print(cu.toString());
pw.close();
} catch (Exception e) {
System.out.println("fail " + javaFile.toString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment