Implemented RFE 994338 - The msg produced by ConstructorCallsOverridableMethod now includes the offending method name.
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3482 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -16,6 +16,7 @@ Fixed bug 1196238 - UnusedImports no longer reports false positives for various
|
||||
Implemented RFE 1193979 - BooleanInstantiation now catches cases like Boolean.valueOf(true)
|
||||
Implemented RFE 1171095 - LabeledStatement nodes now contain the image of the label.
|
||||
Implemented RFE 1176401 - UnusedFormalParameter now flags public methods.
|
||||
Implemented RFE 994338 - The msg produced by ConstructorCallsOverridableMethod now includes the offending method name.
|
||||
Modified command line parameters; removed -jdk15 and -jdk13 parameters and added a -'targetjdk [1.3|1.4|1.5]' parameter.
|
||||
Modified CSVRenderer to include more columns.
|
||||
Optimized rules: FinalFieldCouldBeStatic (115 seconds to 7 seconds), SuspiciousConstantFieldName (48 seconds to 14 seconds), UnusedModifer (49 seconds to 4 seconds)
|
||||
|
@ -213,7 +213,7 @@ public class Foo {
|
||||
</rule>
|
||||
|
||||
<rule name="ConstructorCallsOverridableMethod"
|
||||
message="Avoid calls to overridable methods during construction"
|
||||
message="Overridable method ''{0}'' called during construction"
|
||||
class="net.sourceforge.pmd.rules.ConstructorCallsOverridableMethod">
|
||||
<description>
|
||||
Calling overridable methods during construction poses a risk of invoking methods on an
|
||||
|
@ -6,6 +6,45 @@
|
||||
These are new rules that are still in progress
|
||||
</description>
|
||||
|
||||
<rule name="ConstructorCallsOverridableMethod"
|
||||
message="Overridable method ''{0}'' called during construction"
|
||||
class="net.sourceforge.pmd.rules.ConstructorCallsOverridableMethod">
|
||||
<description>
|
||||
Calling overridable methods during construction poses a risk of invoking methods on an
|
||||
incompletely constructed object. This situation can be difficult to discern.
|
||||
It may leave the sub-class unable to construct its superclass or forced to
|
||||
replicate the construction process completely within itself, losing the ability to call
|
||||
super(). If the default constructor contains a call to an overridable method,
|
||||
the subclass may be completely uninstantiable. Note that this includes method calls
|
||||
throughout the control flow graph - i.e., if a constructor Foo() calls a private method
|
||||
bar() that calls a public method buz(), there's a problem.
|
||||
</description>
|
||||
<priority>1</priority>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class SeniorClass {
|
||||
public SeniorClass(){
|
||||
toString(); //may throw NullPointerException if overridden
|
||||
}
|
||||
public String toString(){
|
||||
return "IAmSeniorClass";
|
||||
}
|
||||
}
|
||||
public class JuniorClass extends SeniorClass {
|
||||
private String name;
|
||||
public JuniorClass(){
|
||||
super(); //Automatic call leads to NullPointerException
|
||||
name = "JuniorClass";
|
||||
}
|
||||
public String toString(){
|
||||
return name.toUpperCase();
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<!--
|
||||
<rule name="FooRule"
|
||||
message="foo?" symboltable="true"
|
||||
class="net.sourceforge.pmd.rules.FooRule">
|
||||
@ -27,7 +66,7 @@ These are new rules that are still in progress
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
-->
|
||||
|
||||
|
||||
<!--
|
||||
|
@ -27,6 +27,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
/**
|
||||
* Searches through all methods and constructors called from constructors. It
|
||||
@ -563,7 +564,8 @@ public final class ConstructorCallsOverridableMethod extends AbstractRule {
|
||||
int count = h.getASTMethodDeclarator().getParameterCount();
|
||||
if (meth.getName().equals(methName) && (meth.getArgumentCount() == count)) {
|
||||
RuleContext ctx = (RuleContext) data;
|
||||
ctx.getReport().addRuleViolation(createRuleViolation(ctx, meth.getASTPrimaryExpression()));
|
||||
String msg = MessageFormat.format(getMessage(), new Object[]{meth.getName()});
|
||||
ctx.getReport().addRuleViolation(createRuleViolation(ctx, meth.getASTPrimaryExpression(), msg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user