简体   繁体   English

FindBugs的问题排除过滤器

[英]Problems with FindBugs exclude filter

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. 生成的EJB仍然在被查看。 Can someone provide some better direction on this. 有人可以为此提供更好的指导。

I want to exclude out all classes that start with "_" 我想排除所有以“_”开头的类

Example: 例:

com/mycompany/business/admin/ejb/_AdminRemoteHome_Stub.java COM / myCompany中/业务/管理/ EJB / _AdminRemoteHome_Stub.java

com/mycompany/business/admin/ejb/_EJSRemoteStatelessAdminHome_054d51b9_Tie.java COM / myCompany中/业务/管理/ 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。

Regarding FindBugFilter , 关于FindBugFilter

(just to be sure) are you sure your are considering the compiled class files directoriesa, and not the sourcePath? (只是为了确定)你确定你正在考虑编译的类文件目录,而不是sourcePath? (as mentioned in this SO answer ). (如本SO答案所述 )。

From the Java element name matching section: 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 expression that 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 anchor is supposed to match at the start of the string the regex pattern is applied to. .*\\._.* ”而不是“ .*\\.^_* ”,因为应该在应用正则表达式模式的字符串的开头匹配。

  • " .*?EJS.* " instead of " .*EJS* " because the ? .*?EJS.* ”而不是“ .*EJS* ”因为? quantifier makes the matching lazy, avoiding to 'eat' EJS. 量词使得匹配变得懒惰,避免“吃掉”EJS。 (Plus " S* " means "0 or n S", which does not help here) (加上“ S* ”表示“0或n S”,这对此没有帮助)

My findbugs exclude file was not working as above. 我的findbugs排除文件不能像上面那样工作。 I'm using the findbugs-maven-plugin v3.0.0. 我正在使用findbugs-maven-plugin v3.0.0。 To resolve the issue I ran a build which generated findbugsXml.xml, then issued: 为了解决这个问题,我运行了一个生成findbugsXml.xml的构建,然后发出:

mvn findbugs:gui

This starts the User Interface to findbugs. 这将启动用户界面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. 然后我加载了findbugsXml.xml文件,导航到我想要排除的警告,排除它们,然后将排除项保存到findbugs_exclude.xml。 I added this to the maven plugin as 我把它添加到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>

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. 到目前为止,我还没有找到如何排除一个类及其所有子类(不是过滤器中的线索),像com.some.Proto$.*这样的简单正则com.some.Proto$.*只是不起作用。 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 . 我还注意到$ in regex意味着行结束,而findbugs将其视为文本字符,我认为,否则应该是com.some.Proto\\$Query Besides, a valuable regex tester helped me on regex by explaining every character in it. 此外,一个有价值的正则表达式测试人员通过解释其中的每个字符帮助我进行正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM