java FindBugs 排除过滤器的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1073656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with FindBugs exclude filter
提问by boyd4715
I am in the process of evaluating FindBugs and am trying to make use of the excludeFilter so that the tool does not process the test packages or the generated ejb stubs.
我正在评估 FindBugs 并尝试使用 excludeFilter 以便该工具不处理测试包或生成的 ejb 存根。
I have tried the following:
我尝试了以下方法:
<FindBugsFilter>
<!-- Match any test packages -->
<Match>
<Package name="~.*\.test"/>
</Match>
<Match>
<Or>
<Class name="~.*\.^_*"/>
<Class name="~.*EJS*"/>
</Or>
<Bug pattern="MALICIOUS_CODE"/>
</Match>
The generated EJB's are still being looked at. Can someone provide some better direction on this.
生成的 EJB 仍在查看中。有人可以就此提供一些更好的指导。
I want to exclude out all classes that start with "_"
我想排除所有以“_”开头的类
Example:
例子:
com/mycompany/business/admin/ejb/_AdminRemoteHome_Stub.java
com/mycompany/business/admin/ejb/_AdminRemoteHome_Stub.java
com/mycompany/business/admin/ejb/_EJSRemoteStatelessAdminHome_054d51b9_Tie.java
com/mycompany/business/admin/ejb/_EJSRemoteStatelessAdminHome_054d51b9_Tie.java
Updated filter file.
更新了过滤器文件。
I change the filter file to the following structure using the suggested regx changes and now things are working as expected:
我使用建议的 regx 更改将过滤器文件更改为以下结构,现在一切正常:
<FindBugsFilter>
<!-- Match any test packages -->
<Match>
<Package name="~.*\.test"/>
</Match>
<Match>
<Class name="~.*\._.*"/>
</Match>
<Match>
<Class name="~.*?EJS.*"/>
</Match>
Looks like I need to go back and brush up on my regx.
看起来我需要回去复习一下我的 regx。
采纳答案by VonC
Regarding FindBugFilter,
(just to be sure) are you sure you are considering the compiled class files directories, and not the sourcePath? (as mentioned in this SO answer).
(只是为了确定)您确定您正在考虑编译的类文件目录,而不是 sourcePath 吗?(如在此SO 答案中所述)。
From the Java element name matchingsection:
从Java 元素名称匹配部分:
If the name attribute of Class, Method or Field starts with the ~ character the rest of attribute content is interpreted as a Java regular expressionthat is matched against the names of the Java element in question.
如果 Class、Method 或 Field 的 name 属性以 ~ 字符开头,则属性内容的其余部分将被解释为Java 正则表达式,该表达式与相关 Java 元素的名称相匹配。
Would the following regex be more accurate?
以下正则表达式会更准确吗?
<Class name="~.*\._.*"/>
<Class name="~.*?EJS.*"/>
"
.*\._.*" instead of ".*\.^_*" because the anchoris supposed to match at the start of the string the regex pattern is applied to."
.*?EJS.*" instead of ".*EJS*" because the?quantifiermakes the matching lazy, avoiding to 'eat' EJS. (Plus "S*" means "0 or n S", which does not help here)
回答by anonymous
My findbugs exclude file was not working as above. I'm using the findbugs-maven-plugin v3.0.0. To resolve the issue I ran a build which generated findbugsXml.xml, then issued:
我的 findbugs 排除文件没有像上面那样工作。我正在使用 findbugs-maven-plugin v3.0.0。为了解决这个问题,我运行了一个生成 findbugsXml.xml 的构建,然后发出:
mvn findbugs:gui
This starts the User Interface to findbugs. I then loaded the findbugsXml.xml file, navigated to the warnings I desired excluded, excluded them and then saved the exclusions to findbugs_exclude.xml. I added this to the maven plugin as
这将启动用户界面以查找错误。然后我加载了 findbugsXml.xml 文件,导航到我想要排除的警告,排除它们,然后将排除项保存到 findbugs_exclude.xml。我将此添加到 Maven 插件中
<excludeFilterFile>findbugs_exclude.xml</excludeFilterFile>
The generated file works and the exclusions are truly omitted from the findbugs report.
生成的文件有效并且排除项确实从 findbugs 报告中省略了。
Another great tip I found for the maven plugin was to add:
我为 Maven 插件找到的另一个很棒的技巧是添加:
<omitVisitors>UnreadFields</omitVisitors>
回答by Tiina
It is helpful to mention that if a class is supposed to be excluded, be careful with its inner classes. It took me hours to find out that instead of
值得一提的是,如果一个类应该被排除在外,请注意它的内部类。我花了几个小时才发现,而不是
<Match>
<Class name="com.some.Proto" /> <!--or com.some.Proto$.*-->
</Match>
I should use the following config with its inner classes listed one by one
我应该使用以下配置及其内部类一一列出
<Match>
<Or>
<Class name="com.some.Proto$Event" />
<Class name="com.some.Proto$Msg" />
<Class name="com.some.Proto$Query" />
</Or>
</Match>
And so far I haven't found out how to exclude a class and all its subclass (not a clue in filter), a simple regex like com.some.Proto$.*just does not work. And I also noticed that $in regex means the end of line, while findbugs read it as a text character I think, otherwise it should have been com.some.Proto\$Query. Besides, a valuable regex testerhelped me on regex by explaining every character in it.
到目前为止,我还没有找到如何排除一个类及其所有子类(不是filter 中的线索),像这样的简单正则表达式com.some.Proto$.*不起作用。而且我还注意到,$在正则表达式中意味着行尾,而我认为 findbugs 将其作为文本字符读取,否则应该是com.some.Proto\$Query. 此外,一位有价值的正则表达式测试员通过解释其中的每个字符来帮助我了解正则表达式。

