Java PMD规则校验
怪我咯
怪我咯 2017-04-18 10:23:53
[Java讨论组]

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规则是期望你的参数提供方法直接调用,而不使用方法返回的对象(这个对象不是在方法内部创建的),再调用对象的函数,可以参考一下上面的示例修改。
如果无法修改那就把这个规则排除掉吧。这个规则有点苛刻。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号