Checkstyle Regexp以捕获Java中的所有控制台打印
我使用此Checkstyle块来阻止人们在我的项目中打印到控制台:Checkstyle Regexp以捕获Java中的所有控制台打印,java,regex,checkstyle,system.out,Java,Regex,Checkstyle,System.out,我使用此Checkstyle块来阻止人们在我的项目中打印到控制台: <module name="Regexp"> <property name="format" value="((System\.|(out|err)\.print)|printStackTrace)"/> <property name="ignoreComments" value="true"/> <property name="illegalPattern" v
<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>
其中正则表达式为((系统\.|(输出|错误)\.print)printStackTrace)
然而,有人偷偷地把这个输入了:out.println
通过导入导入静态java.lang.System.out代码>
因此我将正则表达式更新为:((System\(out | err))| printStackTrace |(out | err)\(print | write | append))
我的问题是:这个正则表达式是否涵盖了用Java打印到控制台的所有方法?有人有更好的正则表达式,他们在Checkstyle中使用吗?如果你不想让人们使用System.out.println
,那么你能做的最好的事情就是争辩他们的工作有危险,并用其他对策威胁他们。(拜托,我在开玩笑,千万别这么做):)
他们可以做一些类似的事情
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()
的调用。您甚至可以编写一个记录器
类,绕过记录器
配置,将其打印到控制台:
作为一个暗示,永远不要试图用武力证明人类的智慧,你会输的。作为一个好的管理者,试着说服你的员工按预期行事。说服他们使用伐木机的好处,但千万不要用暴力来尝试
如果您在一个月内没有看到应用程序的控制台输出,请尝试向他们提供免费啤酒。。。这几乎总是有效的。\b(?:System\)?(?:out | err)\(?:print | write | append)
应该可以工作。或者,\b(?:System\)(?:(?:out | err)\)(?:print | write | append)
@WiktorStribiżew作者的解决方案也捕捉到了这种情况:PrintStream debug=System.out;调试。打印(“文本”)代码>为什么不干脆禁止系统输出并一起出错,而不管它们调用什么方法?因为还有其他打印方法,例如,Throwable\printStackTrace()