gpt4 book ai didi

java - Checkstyle Regexp 以捕获 Java 中的所有控制台打印

转载 作者:行者123 更新时间:2023-12-05 06:25:34 25 4
gpt4 key购买 nike

我有这个 Checkstyle block 来阻止人们在我的项目中打印到控制台:

<module name="Regexp">
<property name="format" value="((System\.|(out|err)\.print)|printStackTrace)"/>
<property name="ignoreComments" value="true"/>
<property name="illegalPattern" value="true"/>
<property name="message" value="No printing to console, use a logger."/>
</module>

正则表达式是 ((System\.|(out|err)\.print)|printStackTrace)

然而,有人偷偷把它放进去:out.println通过导入 import static java.lang.System.out;

所以我将正则表达式更新为:((System\.(out|err))|printStackTrace|(out|err)\.(print|write|append))

我的问题是:这个正则表达式是否涵盖了在 Java 中打印到控制台的所有方式?有没有人有更好的在 Checkstyle 中使用的正则表达式?

最佳答案

如果您不希望人们使用 System.out.println,那么您能做的最好的事情就是争辩说他们的工作处于危险之中并用其他对策<来威胁他们/em>。 (拜托,我在开玩笑,永远不要这样做):)

他们可以做类似的事情

import static java.lang.System.out;

public class App
{
public interface wvs {
void abc(Object o);
}

static final wvs bwz = out::println; /* this is the only reference to
* println, but you can hide it
* in another part of the
* application */

static void blah(wvs o, Object oo)
{
o.abc( oo );
}

public static void main( String[] args )
{
System.out.println( "Hello World!" ); /* this will be filtered */
blah(bwz, "another message by courtesy of LC"); /* will this? */
}
}

让您疯狂地寻找对 System.out.println() 的调用。您甚至可以编写一个 Logger 类来绕过 Logger 配置打印到控制台。 :)

作为提示,永远不要试图用武力证明人类的智慧,否则你会输。作为一名优秀的经理,请尝试说服您的员工按预期行事。让他们相信使用 Loggers 的好处,但永远不要用蛮力来尝试。

如果您在一个月内没有看到应用程序的控制台输出,请尝试向他们提供免费啤酒......这几乎总是有效。

关于java - Checkstyle Regexp 以捕获 Java 中的所有控制台打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56833428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com