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).
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 can't do that, I'm afraid, but PMD (rule) and FindBugs (rule) can.
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.
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.
来源:https://stackoverflow.com/questions/20116032/how-to-check-the-value-of-the-local-variable-field-is-not-used-in-checkstyle