Skip to content

Commit

Permalink
p3c-pmd 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
caikang.ck committed Aug 22, 2020
1 parent 87ba608 commit 68924e3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
4 changes: 2 additions & 2 deletions p3c-pmd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
</parent>
<groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId>
<version>2.0.1</version>
<version>2.1.0</version>
<packaging>jar</packaging>
<name>p3c-pmd</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<pmd.version>6.15.0</pmd.version>
<maven.compiler.target>1.8</maven.compiler.target>
<annotation.version>1.3.2</annotation.version>
<kotlin.version>1.3.50</kotlin.version>
<kotlin.version>1.3.72</kotlin.version>
</properties>
<description>Alibaba Java Coding Guidelines PMD implementations</description>
<url>https://github.com/alibaba/p3c</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Object visit(ASTEqualityExpression node, Object data) {
final String unaryExpression = "UnaryExpression";
// null presents in either side of "==" or "!=" means no violation
if (node.hasDescendantMatchingXPath(literalPrefix)
|| node.hasDescendantMatchingXPath(unaryExpression)) {
|| node.hasDescendantMatchingXPath(unaryExpression)) {
return super.visit(node, data);
}

Expand All @@ -48,7 +48,10 @@ public Object visit(ASTEqualityExpression node, Object data) {
// PMD can not resolve array length type, but only the
ASTPrimaryExpression left = expressions.get(0);
ASTPrimaryExpression right = expressions.get(1);

// if left is complex expression, skip
if (left.jjtGetNumChildren() > 1) {
return super.visit(node, data);
}
boolean bothArrayLength = isArrayLength(left) && isArrayLength(right);
boolean bothWrapperType = NodeUtils.isWrapperType(left) && NodeUtils.isWrapperType(right);

Expand All @@ -61,7 +64,8 @@ public Object visit(ASTEqualityExpression node, Object data) {
}

private boolean isArrayLength(ASTPrimaryExpression expression) {
// assume expression like "x.length" is the length of array, field with name "length" may result in misrecognition
// assume expression like "x.length" is the length of array, field with name "length" may result in
// misrecognition
return "length".equals(expression.jjtGetLastToken().getImage())
&& ".".equals(expression.jjtGetFirstToken().getNext().getImage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<description>compare wrapper type objects without equals</description>
<expected-problems>4</expected-problems>
<expected-linenumbers>11,23,31,34</expected-linenumbers>
<code-ref id="wrap-type-not-use-equal" />
<code-ref id="wrap-type-not-use-equal"/>
</test-code>

<!-- ====================================================================== -->
Expand Down Expand Up @@ -148,7 +148,7 @@
<test-code>
<description>bugfix</description>
<expected-problems>0</expected-problems>
<code-ref id="wrong-result-fix" />
<code-ref id="wrong-result-fix"/>
</test-code>

<!-- ====================================================================== -->
Expand All @@ -171,7 +171,37 @@
<expected-problems>0</expected-problems>
<code-ref id="array-length-equals"/>
</test-code>
<code-fragment id="expression-primitive-equals">
<![CDATA[
public class Test {
public void foo(){
int a;
if (Integer.parseInt("0") == a) {
return;
}
if (a == Integer.parseInt("0")) {
return;
}
String s = "0";
if (Integer.parseInt("0") == Integer.parseInt(s)) {
return;
};
Integer b;
if (b == Integer.parseInt("0")) {
return;
}
}
}
]]>
</code-fragment>
<test-code>
<description>expression is primitive type</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>17</expected-linenumbers>
<code-ref id="expression-primitive-equals"/>
</test-code>
<!-- ====================================================================== -->

</test-data>

0 comments on commit 68924e3

Please sign in to comment.