Fixed bug 1581123 - False +: UnnecessaryWrapperObjectCreation

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4722 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2006-10-20 23:13:09 +00:00
parent d1f45bb555
commit 065681cfc7
3 changed files with 69 additions and 32 deletions

View File

@ -12,6 +12,7 @@ Fixed bug 1566547 - Annotations with an empty MemberValueArrayInitializer are no
Fixed bugs 1060761 / 1433119 & RFE 1196954 - CloseResource now takes an optional parameter to identify closure methods
Fixed bug 1579615 - OverrideBothEqualsAndHashcode no longer throws an Exception on equals methods that don't have Object as a parameter type.
Fixed bug 1580859 - AvoidDecimalLiteralsInBigDecimalConstructor now catches more cases.
Fixed bug 1581123 - False +: UnnecessaryWrapperObjectCreation.
Applied patch 1551189 - SingularField false + for initialization blocks
Applied patch 1573981 - false + in CloneMethodMustImplementCloneable
Applied patch 1574988 - false + in OverrideBothEqualsAndHashcode

View File

@ -283,41 +283,11 @@ public class Test {
<rule name="UnnecessaryWrapperObjectCreation"
message="Unnecessary wrapper object creation"
class="net.sourceforge.pmd.rules.XPathRule"
class="net.sourceforge.pmd.rules.optimization.UnnecessaryWrapperObjectCreation"
externalInfoUrl="http://pmd.sourceforge.net/rules/optimizations.html#UnnecessaryWrapperObjectCreation">
<description>
Parsing method should be called directy instead.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression[
PrimaryPrefix/Name[
@Image='Byte.valueOf' or
@Image='Short.valueOf' or
@Image='Integer.valueOf' or
@Image='Long.valueOf' or
@Image='Float.valueOf' or
@Image='Double.valueOf' or
@Image='Boolean.valueOf' or
@Image='Character.valueOf'
]
and
PrimarySuffix[
@Image='byteValue' or
@Image='shortValue' or
@Image='intValue' or
@Image='longValue' or
@Image='floatValue' or
@Image='doubleValue' or
@Image='booleanValue' or
@Image='charValue'
]]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
@ -354,4 +324,4 @@ public int convert(String s) {

View File

@ -0,0 +1,66 @@
package net.sourceforge.pmd.rules.optimization;
import java.util.Set;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.*;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.ast.Node;
import net.sourceforge.pmd.util.CollectionUtil;
public class UnnecessaryWrapperObjectCreation extends AbstractRule {
private static final Set prefixSet = CollectionUtil.asSet(new String[] {
"Byte.valueOf",
"Short.valueOf",
"Integer.valueOf",
"Long.valueOf",
"Float.valueOf",
"Double.valueOf",
"Character.valueOf"
});
private static final Set suffixSet = CollectionUtil.asSet(new String[] {
"byteValue",
"shortValue",
"intValue",
"longValue",
"floatValue",
"doubleValue",
"charValue"
});
public Object visit(ASTPrimaryPrefix node, Object data) {
if (node.jjtGetNumChildren() == 0 || !node.jjtGetChild(0).getClass().equals(ASTName.class)) {
return super.visit(node, data);
}
String image = ((ASTName) node.jjtGetChild(0)).getImage();
if (image.startsWith("java.lang.")) {
image = image.substring(10);
}
boolean checkBoolean = ((RuleContext) data).getSourceType().compareTo(SourceType.JAVA_15) >= 0;
if (prefixSet.contains(image)||(checkBoolean && "Boolean.valueOf".equals(image))) {
ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
if (parent.jjtGetNumChildren() >= 3) {
Node n = parent.jjtGetChild(2);
if (n instanceof ASTPrimarySuffix) {
ASTPrimarySuffix suffix = (ASTPrimarySuffix) n;
image = suffix.getImage();
if (suffixSet.contains(image)||(checkBoolean && "booleanValue".equals(image))) {
super.addViolation(data, node);
return data;
}
}
}
}
return super.visit(node, data);
}
}