UnusedFormalParameter vs. AvoidDuplicateLiterals in maven-pmd-plugin -
i'm using built-in rulesets strings.xml
, unusedcode.xml
with
<?xml version="1.0"?> <ruleset name="custom ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"> <description> default ruleset </description> <rule ref="rulesets/java/strings.xml"/> <rule ref="rulesets/java/unusedcode.xml"/> </ruleset>
in maven-pmd-plugin
follows:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-pmd-plugin</artifactid> <version>3.8</version> <executions> <execution> <id>pmd-check</id> <phase>validate</phase> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <printfailingerrors>true</printfailingerrors> <detail>true</detail> <rulesets> <ruleset>${basedir}/src/main/resources/ruleset.xml</ruleset> </rulesets> </configuration> </plugin>
so following problem in following use case:
public class newclass { private final static string pmd_unused_formal_parameter = "pmd.unusedformalparameter"; @suppresswarnings(pmd_unused_formal_parameter) private void somemethod1(object parameter1) { system.out.println("somemethod1"); } @suppresswarnings("pmd.unusedformalparameter") private void somemethod2(object parameter1) { system.out.println("somemethod2"); } @suppresswarnings("pmd.unusedformalparameter") private void somemethod3(object parameter1) { system.out.println("somemethod3"); } @suppresswarnings("pmd.unusedformalparameter") private void somemethod4(object parameter1) { system.out.println("somemethod4"); } public static void main(string[] args) { newclass newclazz = new newclass(); newclazz.somemethod1(null); newclazz.somemethod2(null); newclazz.somemethod3(null); } }
having annotation parameter value "pmd.unusedformalparameter"
appear 4 times causes avoidduplicateliterals
violation. replacing string commented out private final static
constant causes annotation have no effect , unusedformalparameter
violation.
i don't know internals of pmd. out-of-the-box user appears strange pmd both doesn't substitute variable value (even though that's context sensitive task should possible within scope of static code analysis, it's complex) , unusedformalparameter
doesn't exclude string values used suppress other checks , there because of pmd.
i'm using maven-pmd-plugin
3.8.
as issue, there 2 ways can work around this:
avoidduplicateliterals
allowsskipannotations
property can settrue
ignore literals in annotations. of course ignore literals in annotations, not@suppresswarnings
- all rules allow configure
violationsuppressxpath
property xpath expression of expressions ignore. instance, on case, can setavoidduplicateliterals
rule ignore literals on@suppresswarnings
. believe proper expression//annotation//name[@image = "suppresswarnings" or @image = "java.lang.suppresswarnings"]/..//literal
(you'd have test make sure)
i don't know internals of pmd. out-of-the-box user appears strange pmd both doesn't substitute variable value (even though that's context sensitive task should possible within scope of static code analysis, it's complex)
you right, behavior not supported. i've added issue on github track request. feel free chip in.
Comments
Post a Comment