我正在使用IntelliJ进行旧的Java应用程序项目,源代码巨大。一类具有一种在播放时每帧打印视频信息的方法。我想禁用此代码以进行打印,但是我不知道它来自哪里或类名是什么。当我直接搜索字符串时,它没有显示。有什么聪明的方法可以找到打印的位置吗?
(来自评论):
输出行为[isDeveloper true]------ get duration(=3.552653) is test impl ------
答案 0 :(得分:5)
使用System.setOut()
将System.out
替换为您自己的PrintStream
,使其与(不需要的)输出匹配,在match子句上放置一个断点,然后查看堆栈跟踪。
由于PrintStream
有很多方法,因此我们不将覆盖所有生成PrintStream
输出的所有可能的方法,而直接覆盖{ {1}}是我们自己设计的;那么我们只需要覆盖一种方法,但是就需要重建字符串。
示例:
FilterOutputStream
程序启动时,安装新的class Prospector extends FilterOutputStream {
int count = 0;
private OutputStream original;
public Prospector(OutputStream out) {
super(out);
original = out;
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
String buffer = new String(b, off, len, StandardCharsets.ISO_8859_1);
if (buffer.contains("]------ get duration(=")) {
if (count == 0) {
new Exception().printStackTrace(original);
}
count++; // Set breakpoint here
}
super.write(b, off, len);
}
}
,将其包裹在我们的PrintStream
中:
Prospector
在指示的行上设置断点。当程序在断点处停止时,查找堆栈跟踪以查找生成输出的类/方法。
当神秘类生成目标行时,可能类似于...
System.setOut(new PrintStream(new Prospector(System.out)));
...可以多次调用此方法,其中System.out.format("[%s %s]------ get duration(=%f) is %s -------%n",
"isDeveloper", "true", 3.552653, "test impl");
依次包含该行的各个部分。在上述情况下,应为:
buffer
"["
"isDeveloper"
" "
"true"
"]------ get duration(="
"3.552653"
") is "
"test impl"
" -------"
理想情况下,"\n"
应该足够独特以找到所需的类/方法,但是您可以根据需要进行调整。
也有可能将整个文本格式化为单个]------ get duration(=
,并在一次调用中将其打印出来。这就是使用String
匹配所需输出的原因。
答案 1 :(得分:0)
我认为
按Ctrl +鼠标单击所需的变量或方法
充当到源的超链接
应该做到这一点(在IntelliJ上)