[java] Update rule UnnecessaryConversionTemporary
This commit is contained in:
@ -258,7 +258,7 @@
|
||||
<!-- <rule ref="category/java/errorprone.xml/UnconditionalIfStatement"/> -->
|
||||
<!-- <rule ref="category/java/errorprone.xml/UnnecessaryBooleanAssertion"/> -->
|
||||
<!-- <rule ref="category/java/errorprone.xml/UnnecessaryCaseChange"/> -->
|
||||
<!-- <rule ref="category/java/errorprone.xml/UnnecessaryConversionTemporary"/> -->
|
||||
<rule ref="category/java/errorprone.xml/UnnecessaryConversionTemporary"/>
|
||||
<rule ref="category/java/errorprone.xml/UnusedNullCheckInEquals"/>
|
||||
<!-- <rule ref="category/java/errorprone.xml/UseCorrectExceptionLogging"/> -->
|
||||
<rule ref="category/java/errorprone.xml/UseEqualsToCompareStrings"/>
|
||||
|
@ -1,65 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.rule.errorprone;
|
||||
|
||||
import static net.sourceforge.pmd.util.CollectionUtil.setOf;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
|
||||
public class UnnecessaryConversionTemporaryRule extends AbstractJavaRule {
|
||||
|
||||
private boolean inPrimaryExpressionContext;
|
||||
private ASTPrimaryExpression primary;
|
||||
private boolean usingPrimitiveWrapperAllocation;
|
||||
|
||||
private static final Set<String> PRIMITIVE_WRAPPERS = setOf("Integer", "Boolean", "Double", "Long", "Short", "Byte", "Float");
|
||||
|
||||
@Override
|
||||
public Object visit(ASTPrimaryExpression node, Object data) {
|
||||
if (node.getNumChildren() == 0 || node.getChild(0).getNumChildren() == 0
|
||||
|| !(node.getChild(0).getChild(0) instanceof ASTAllocationExpression)) {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
// TODO... hmmm... is this inPrimaryExpressionContext gibberish
|
||||
// necessary?
|
||||
inPrimaryExpressionContext = true;
|
||||
primary = node;
|
||||
super.visit(node, data);
|
||||
inPrimaryExpressionContext = false;
|
||||
usingPrimitiveWrapperAllocation = false;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTAllocationExpression node, Object data) {
|
||||
if (!inPrimaryExpressionContext || !(node.getChild(0) instanceof ASTClassOrInterfaceType)) {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
if (!PRIMITIVE_WRAPPERS.contains(node.getChild(0).getImage())) {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
usingPrimitiveWrapperAllocation = true;
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTPrimarySuffix node, Object data) {
|
||||
if (inPrimaryExpressionContext && usingPrimitiveWrapperAllocation) {
|
||||
if (node.hasImageEqualTo("toString")) {
|
||||
if (node.getParent() == primary) {
|
||||
addViolation(data, node);
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
}
|
@ -3163,13 +3163,32 @@ boolean answer2 = buz.toUpperCase().equalsIgnoreCase("baz"); // another unnec
|
||||
language="java"
|
||||
since="0.1"
|
||||
message="Avoid unnecessary temporaries when converting primitives to Strings"
|
||||
class="net.sourceforge.pmd.lang.java.rule.errorprone.UnnecessaryConversionTemporaryRule"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#unnecessaryconversiontemporary">
|
||||
<description>
|
||||
Avoid the use temporary objects when converting primitives to Strings. Use the static conversion methods
|
||||
on the wrapper classes instead.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//MethodCall[@MethodName = 'toString']
|
||||
[ConstructorCall[position() = 1]
|
||||
[
|
||||
pmd-java:typeIs('java.lang.Integer')
|
||||
or pmd-java:typeIs('java.lang.Long')
|
||||
or pmd-java:typeIs('java.lang.Float')
|
||||
or pmd-java:typeIs('java.lang.Byte')
|
||||
or pmd-java:typeIs('java.lang.Double')
|
||||
or pmd-java:typeIs('java.lang.Short')
|
||||
]
|
||||
]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public String convert(int x) {
|
||||
|
@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.errorprone;
|
||||
|
||||
import net.sourceforge.pmd.testframework.PmdRuleTst;
|
||||
|
||||
@org.junit.Ignore("Rule has not been updated yet")
|
||||
public class UnnecessaryConversionTemporaryTest extends PmdRuleTst {
|
||||
// no additional unit tests
|
||||
}
|
||||
|
@ -29,6 +29,18 @@ public class Foo {
|
||||
Long method (Foo foo) {
|
||||
return new Long(foo.get().toString());
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>Preferred approach - static toString</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public String convert(int x) {
|
||||
return Integer.toString(x); // preferred approach
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
Reference in New Issue
Block a user