假设我正在一个非常大的项目中工作,并且注意到有一个空的打印行,所以我假设有一个 System.out.println("");位于代码中的某个位置。除了在整个项目中搜索所有出现的 System.out.println 之外,我该如何尝试找出它在哪里?
如果您使用的是 Java 8+,Durian https://github.com/diffplug/durian has a 堆栈转储器 https://diffplug.github.io/durian/javadoc/2.0/com/diffplug/common/base/StackDumper.html类可以轻松找到打印给定行的位置:
StackDumper.dumpWhenSysOutContains("SomeTrigger")
当打印“SomeTrigger”时,这将被转储到System.err
:
+----------\
| Triggered by SomeTrigger
| at package.MyClass.myMethod(MyClass.java:62)
| (the rest of the stacktrace)
+----------/
对于您的情况(寻找空字符串),它有点复杂:
PrintStream sysOutClean = System.out;
StringPrinter sysOutReplacement = new StringPrinter(StringPrinter.stringsToLines(line -> {
if (line.isEmpty()) {
StackDumper.dump("Found empty line");
}
sysOutClean.println(line);
}));
System.setOut(sysOutReplacement.toPrintStream());
现在如果有这样的事情:
System.out.println("ABC");
System.out.println("123");
System.out.println("");
System.out.println("DEF");
然后你的控制台将如下所示:
ABC
123
+----------\
| Found empty line
| at package.MyClass.myMethod(MyClass.java:62)
| (the rest of the stacktrace)
+----------/
DEF
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)