java 存在可覆盖的调用_java – PMD在对象构造期间调用的可覆盖方法

PMD

rule说:

Calling overridable methods during construction poses a risk of invoking methods on an incompletely constructed object and can be difficult to debug. 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(), this denotes a problem.

例:

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();

}

}

删除构造函数中对可覆盖方法的任何调用,或将final修饰符添加到该方法.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值