通过XPath的PMD PrimaryPrefix问题
我花了大约4个小时在这个问题上,但事实证明,在开发相当原始的自定义规则时,使用XPath非常痛苦 对于我的问题的一部分,我需要PMD XPath能够区分以下几行: int var=this.nocall; int-var=nocall 从AST中,我看到了PrimaryPrefix:this 但是,我无法选择第一个带有//PrimaryPrefix['this']或//PrimaryExpression/PrimaryPrefix['this]]或//PrimaryPrefix[@image='this']的语句 我想知道是否有人对如何通过XPath检索赋值部分中带有“this.”的所有表达式有任何提示 尝试分析的示例代码:通过XPath的PMD PrimaryPrefix问题,xpath,pmd,Xpath,Pmd,我花了大约4个小时在这个问题上,但事实证明,在开发相当原始的自定义规则时,使用XPath非常痛苦 对于我的问题的一部分,我需要PMD XPath能够区分以下几行: int var=this.nocall; int-var=nocall 从AST中,我看到了PrimaryPrefix:this 但是,我无法选择第一个带有//PrimaryPrefix['this']或//PrimaryExpression/PrimaryPrefix['this]]或//PrimaryPrefix[@image='
class testRuleOne {
private int nocall;
public void myMethod() {
int var = this.nocall;
var = this.getNoCall();
}
}
以下是AST的原始XML:
<MethodDeclaration Abstract="false" BeginColumn="16" BeginLine="3" EndColumn="9" EndLine="6" Final="false" Image="" InterfaceMember="false" Label="" MethodName="myMethod" Modifiers="1" Native="false" PackagePrivate="false" Private="false" Protected="false" Public="true" Static="false" Strictfp="false" Synchronized="false" SyntacticallyAbstract="false" SyntacticallyPublic="true" Transient="false" Void="true" Volatile="false">
<ResultType BeginColumn="16" BeginLine="3" EndColumn="19" EndLine="3" Image="" Label="" Void="true" returnsArray="false"/>
<MethodDeclarator BeginColumn="21" BeginLine="3" EndColumn="30" EndLine="3" Image="myMethod" Label="" ParameterCount="0">
<FormalParameters BeginColumn="29" BeginLine="3" EndColumn="30" EndLine="3" Image="" Label="" ParameterCount="0"/>
</MethodDeclarator>
<Block BeginColumn="32" BeginLine="3" EndColumn="9" EndLine="6" Image="" Label="" containsComment="false">
<BlockStatement Allocation="false" BeginColumn="17" BeginLine="4" EndColumn="38" EndLine="4" Image="" Label="">
<LocalVariableDeclaration Abstract="false" Array="false" ArrayDepth="0" BeginColumn="17" BeginLine="4" EndColumn="37" EndLine="4" Final="false" Image="" Label="" Modifiers="0" Native="false" PackagePrivate="true" Private="false" Protected="false" Public="false" Static="false" Strictfp="false" Synchronized="false" Transient="false" VariableName="var" Volatile="false">
<Type Array="false" ArrayDepth="0" BeginColumn="17" BeginLine="4" EndColumn="19" EndLine="4" Image="" Label="" TypeImage="int">
<PrimitiveType Array="false" ArrayDepth="0" BeginColumn="17" BeginLine="4" Boolean="false" EndColumn="19" EndLine="4" Image="int" Label=""/>
</Type>
<VariableDeclarator BeginColumn="21" BeginLine="4" EndColumn="37" EndLine="4" Image="" Label="">
<VariableDeclaratorId Array="false" ArrayDepth="0" BeginColumn="21" BeginLine="4" EndColumn="23" EndLine="4" ExceptionBlockParameter="false" Image="var" Label=""/>
<VariableInitializer BeginColumn="27" BeginLine="4" EndColumn="37" EndLine="4" Image="" Label="">
<Expression BeginColumn="27" BeginLine="4" EndColumn="37" EndLine="4" Image="" Label="">
<PrimaryExpression BeginColumn="27" BeginLine="4" EndColumn="37" EndLine="4" Image="" Label="">
<PrimaryPrefix BeginColumn="27" BeginLine="4" EndColumn="30" EndLine="4" Image="" Label="this" SuperModifier="false" ThisModifier="true"/>
<PrimarySuffix ArgumentCount="" Arguments="false" ArrayDereference="false" BeginColumn="31" BeginLine="4" EndColumn="37" EndLine="4" Image="nocall" Label=""/>
</PrimaryExpression>
</Expression>
</VariableInitializer>
</VariableDeclarator>
</LocalVariableDeclaration>
</BlockStatement>
<BlockStatement Allocation="false" BeginColumn="17" BeginLine="5" EndColumn="39" EndLine="5" Image="" Label="">
使用:
//PrimaryExpression[PrimaryPrefix/@Label='this']
<PrimaryExpression BeginColumn="27"
BeginLine="4" EndColumn="37"
EndLine="4" Image="" Label="">
<PrimaryPrefix BeginColumn="27"
BeginLine="4" EndColumn="30" EndLine="4" Image=""
Label="this" SuperModifier="false"
ThisModifier="true"/>
<PrimarySuffix ArgumentCount="" Arguments="false"
ArrayDereference="false" BeginColumn="31" BeginLine="4"
EndColumn="37" EndLine="4" Image="nocall" Label=""/>
</PrimaryExpression>
在提供的XML上计算此XPath表达式时(格式严重错误,但我已更正):
所需元素已被选中:
//PrimaryExpression[PrimaryPrefix/@Label='this']
<PrimaryExpression BeginColumn="27"
BeginLine="4" EndColumn="37"
EndLine="4" Image="" Label="">
<PrimaryPrefix BeginColumn="27"
BeginLine="4" EndColumn="30" EndLine="4" Image=""
Label="this" SuperModifier="false"
ThisModifier="true"/>
<PrimarySuffix ArgumentCount="" Arguments="false"
ArrayDereference="false" BeginColumn="31" BeginLine="4"
EndColumn="37" EndLine="4" Image="nocall" Label=""/>
</PrimaryExpression>
我将使用此修饰符代替标签-原因是,该标签在PMD 5中不再可用。标签只是一个字符串表示形式(类似于toString() 这将同时适用于PMD 4.x和未来的PMD 5-因此您不需要修改规则
另请参见:这是一些我不知道的新信息。您知道如何使用count(节点集)来判断任何PrimaryPrefix/名称是否为FieldDelcaration之一吗?我已经搜索了一天,没有结果。我希望我不必处理Java PMD规则,因为代码看起来会很疯狂。