如何检查“未使用局部变量/字段的值”?在Checkstyle中? [英] How to check "The value of the local variable/field is not used" in Checkstyle?
问题描述
如何检查未使用局部变量的值
和未使用字段的值
使用Checkstyle?在Eclipse中有一个警告。但是Checkstyle中没有(我使用 sun_checks.xml
,稍作修改)。
How to check The value of the local variable is not used
and The value of the field is not used
using Checkstyle? In Eclipse there is a warning about that. But there's no in Checkstyle (i use sun_checks.xml
, slightly modified).
我试图写自己的检查,但是作为一个新手,只找到了如何计算变量声明(使用 TokenTypes.VARIABLE_DEF
),但是我不确定如何找到用法这些变量和字段...
I tried to write my own check, but being a newbie, only found how to calculate variable declarations (using TokenTypes.VARIABLE_DEF
), but i am not sure how to find namely usage of these variables and fields...
推荐答案
恐怕Checkstyle无法做到这一点,但是 PMD(规则)和查找错误(规则)可以。
Checkstyle can't do that, I'm afraid, but PMD (rule) and FindBugs (rule) can.
Checkstyle和PMD仅在Java源代码,很难仅从源代码中推断出字段和变量的使用。 FindBugs对已编译的类文件进行操作,因此在这种情况下,它的工作更加轻松。 PMD只能检测未使用的私有字段和变量,这可以通过查看单个源文件找到。
Checkstyle and PMD operate only on the Java source, and it is harder to deduce field and variable use only from the source. FindBugs operates on the compiled class files, so it has an easier job in this case. PMD can only detect unused private fields and variables, which can be found by looking at a single source file.
为Checkstyle编写未使用的私有字段/变量检测器是可能,如果很乏味。我建议为此使用PMD工具,该工具还具有轻量级的Eclipse插件。
Writing an "unused private field/variable detector" for Checkstyle is possible, if tedious. I would recommend going with the PMD tool for this, which also features a light-weight Eclipse plugin.
这篇关于如何检查“未使用局部变量/字段的值”?在Checkstyle中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!