Java PMD规则校验
怪我咯
怪我咯 2017-04-18 10:23:53
0
1
478

1.final OuterDBLinkPara dbLinkPara = (OuterDBLinkPara)list.get(i);(OuterDBLinkPara是我自己写的一个实体类)
2.String dbip;
3.dbip = dbLinkPara.getDbIp();

PMD规则在序号为3的位置提示:
Potential violation of Law of Demeter (object not created locally)

不知道如何改正。

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(1)
巴扎黑

LawOfDemeter

public class Foo {
    /**
     * This example will result in two violations.
     */
    public void example(Bar b) {
        // this method call is ok, as b is a parameter of "example"
        C c = b.getC();
        
        // this method call is a violation, as we are using c, which we got from B.
        // We should ask b directly instead, e.g. "b.doItOnC();"
        c.doIt();
        
        // this is also a violation, just expressed differently as a method chain without temporary variables.
        b.getC().doIt();
        
        // a constructor call, not a method call.
        D d = new D();
        // this method call is ok, because we have create the new instance of D locally.
        d.doSomethingElse(); 
    }
}

你的list是一个参数吧,LawOfDemeter規則是期望你的參數提供方法直接調用,而不使用方法返回的對象(這個對像不是在方法內部創建的),再調用對象的函數,可以參考上面的示例修改。
如果無法修改那就把這個規則排除掉吧。這個規則有點苛刻。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!