Last active
May 21, 2020 20:54
-
-
Save mauricioaniche/b920ca74d721575958baf5673f7138e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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