【问题标题】:issue about LCOM4 with Weld/CDI?关于 LCOM4 与 Weld/CDI 的问题?
【发布时间】:2011-06-27 09:46:46
【问题描述】:

我是 Sonar 和 Weld/CDI 的新手。我希望您能提供有关 Weld/CDI 的 LCOM4 分析结果的进一步建议。首先,我创建了一个简单的 java 类,如下所示:-

-------------来源---------------

interface MyInterface1 {
   String getName();
   void setName(String name);
}

interface MyInterface2 extends MyInterface1 {
   String getPhone();
   void setPhone();
}

public interface MyPublishedInterface extend MyInterface1, MyInterface2 {
   //There is no any definition, it just a collected capabilities 
   //which will be published to other package. Some capabilities 
   //may be hidden and use internally.
}

abstract class MyBean1 implements MyInterface1 {
   private String name;
   @Override
   public String getName() {
      return this.name;
   }
   @Override
   public void setName(String theName) {
      this.name = theName;
   }
}

abstract class MyBean2 extends MyBean1 implements MyInterface2 {
   private String phone;
   @Override
   public String getPhone() {
      return this.phone;
   }
   @Override
   public void setPhone(String thePhone) {
      this.phone= thePhone;
   }
}

public class MyPublishedBean extends MyBean2 implements MyPublishedInterface {
   //There is no any coding, it just a collected capabilities 
   //which will be published to other package. Some capabilities
   //may be hidden and use internally.
}

@Named
@RequestScope
public class MyBackingBean {
   @Inject
   private MyPublishedInterface myPublishedInterface;

   //-----the business method, setter and getter here.
}

-------------来源---------------

在我用声纳分析之后,它报告说 MyPublishedBean 有一个 LCOM4>1 作为

  1. getPhone()Ljava/lang/String;
  2. setName(Ljava/lang/String;)V
  3. setPhone(Ljava/lang/String;)V
  4. getName()Ljava/lang/String;

以前我曾经将所有方法标记为“最终”方法,没有任何关于 LCOM4 的提及。无论如何,系统向我显示了关于 Unproxyable 的异常,因为我的类包含最终方法。我已经删除了“final”,我遇到了 LCOM4 问题。

我不确定我是否对 Sonar、Weld/CDI、类/界面设计或所有这些感到困惑。你能帮忙提供进一步的建议吗?

【问题讨论】:

    标签: java oop metric


    【解决方案1】:

    Sonar 文档很好地解释了LCOM4。鉴于您在此处给出的示例,您看到的结果是完全正确的。

    这些接口看起来只是没有逻辑的数据持有者。只有属性的 getter 和 setter 的 bean 将完全期望 LCOM 值等于 bean 中的属性数。 LCOM4 是衡量一个类中逻辑 凝聚力的指标。纯数据 bean 的逻辑只是数据以某种方式相互关联。因此,在这种情况下,LCOM4 是一个不正确且具有误导性的指标。

    LCOM4 也应该完全独立于你的方法是否是最终的。

    请注意,LCOM4 > 1 表示可疑类。这并不意味着该类是错误的,也不应该用于将该类标记为坏的。一旦您发现可疑类没问题,最好以某种方式从指标中删除该类(以避免建立一长串您知道应该忽略的警告)。

    【讨论】:

    • 非常感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    相关资源
    最近更新 更多