1

我是 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 的异常,因为我的类包含最终方法。我已经删除了“决赛”,我遇到了 LCOM4 问题。

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

4

1 回答 1

4

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

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

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

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

于 2011-06-27T10:03:41.357 回答