-
I working on update rules to the new version of PMD and faced with deprecation of FieldDeclaration/@variablename. I got the message in log
I have previous rule with <rule name="AvoidDirectAccessToStaticFields" message="Static fields should be accessed in a static way [CLASS_NAME.FIELD_NAME]." language="java" class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
Avoid accessing static fields directly.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value><![CDATA[
//Name[@Image = //FieldDeclaration[@Static='true']/@VariableName]
]]></value>
</property>
</properties>
<example>
<![CDATA[
public final class DirectAccessToStaticFields {
private static int num = 1;
public static int number() {
return num; // violation
}
public static int correct() {
return DirectAccessToStaticFields.num; // correct
}
}
]]>
</example>
</rule> And it works fine (but with warning), but I can not rewrite this XPath to work without warnings using the message (or any other way I found). |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You can try this (didn't test it):
Keep in mind |
Beta Was this translation helpful? Give feedback.
-
@oowekyala unfortunately this variant doesn't work :( |
Beta Was this translation helpful? Give feedback.
-
Ok, here is the updated rule for various PMD versions: PMD 6.55.0 - XPath 1.0:
Note: Replaced PMD 6.55.0 - XPath 2.0:
Note: The static attribute is now a real boolean value and not a string anymore. PMD 7.0.0-rc4:
Note: You don't need to set PMD 7.0.0:
Note: AST changed again: VariableDeclaratorId -> VariableId I guess, the deprecation notice here Could have been more explicit by saying replaceWith = "VariableDeclarator/VariableDeclaratorId/@Name" .
Tipp: Use the designer to develop rules: https://docs.pmd-code.org/latest/pmd_userdocs_extending_designer_reference.html |
Beta Was this translation helpful? Give feedback.
Ok, here is the updated rule for various PMD versions:
PMD 6.55.0 - XPath 1.0:
Note: Replaced
@VariableName
withVariableDeclarator/VariableDeclaratorId/@Name
PMD 6.55.0 - XPath 2.0:
Note: The static attribute is now a real boolean value and not a string anymore.
Note 2: You also need to set
<property name="version" value="2.0"/>
.Note 3: This is the prerequisite for update to PMD 7
PMD 7.0.0-rc4: