Java - 不可实例化类中缺少静态方法

标签 java sonarqube

我有“builder”类,Sonar 发出以下警告:

Missing Static Method In Non Instantiatable Class
pmd : MissingStaticMethodInNonInstantiatableClass
A class that has private constructors and does not have any static methods or fields cannot be used

我将如何重构此类以满足上述检查? 我正在挠头,因为我确实使用了那个类(class)。

示例使用:

ViewBuilder vb = new ViewBuilder.Builder()
    .modelPart(CONTENT_PAGE, contentPageDao.get(id))
    .modelPart("navigation_sections", navigationSectionDao.list() )
    .modelPart("available_tags", tagDao.list() )
    .modelPart("available_albums", albumDao.list() )
    .section(CONTENT_PAGE)
    .page("index")
    .element("form")
    .action("/admin/content_page/save/" + id + ".html")
    .build();

类本身:

import java.util.HashMap;
import java.util.Map;


 public final class ViewBuilder {

    private static final String ADMIN_LAYOUT = "admin/layout";
    private String layout = ADMIN_LAYOUT;
    private String section = "";

  private static Map<String, Object> viewParts = new HashMap<String, Object>();

  /**
   * @return the layout
   */ public String getLayout() {
    return layout;
  }

  /**
   * @param layout the layout to set
   */ public void setLayout(String layout) {
    this.layout = layout;
  }

  /**
   * @return the section
   */ public String getSection() {
    return section;
  }

  /**
   * @param section the section to set
   */ public void setSection(String section) {
    this.section = section;
  }

  public static class Builder{

    // required params
    private String layout;
    private String section;

    // optional params
    private Map<String, Object > viewParts = new HashMap<String, Object >();

    public Builder(){

        this.layout = ADMIN_LAYOUT;
        viewParts.put("layout", ADMIN_LAYOUT);

    }
    public Builder( String layout ){

      if( layout != null ){
        this.layout = layout;
        viewParts.put("layout", layout );
      } else {
        this.layout = ADMIN_LAYOUT;
        viewParts.put("layout", ADMIN_LAYOUT);
      }

    }// constructor
    public Builder modelPart( String val, Object o ){
      this.viewParts.put(val, o );
      return this;
    }

    public Builder action( String val ){
      this.viewParts.put("action", val);
      return this;
    }
    public Builder element( String val ){
      this.viewParts.put("element", val);
      return this;
    }
    public Builder section( String val ){
      this.section = val;
      this.viewParts.put("section", val);
      return this;
    }
    public Builder page( String val ){
      this.viewParts.put("page", val);
      return this;
    }
    public Builder layout( String val ){
      this.layout = val;
      return this;
    }

    public ViewBuilder build( ){
      return new ViewBuilder( this );
    }


  }// Builder

    private ViewBuilder(Builder builder){
      this.section = builder.section;
      this.layout = builder.layout;

      viewParts = builder.viewParts;
    }

  /**
   * Get the value of viewParts
   *
   * @return the value of viewParts
   */
  public Map<String, Object> getViewParts() { return viewParts; }

  /**
   * Set the value of viewParts
   *
   * @param viewParts new value of viewParts
   */
  public void setViewParts(Map<String, Object> viewParts) { this.viewParts = viewParts; }
}

最佳答案

SonarQube PMD 规则需要更新以考虑静态嵌套构建器类中的访问器。

为此创建了一个 PMD 问题,但它看起来已被废弃: http://sourceforge.net/p/pmd/bugs/955/

我不同意通过“重构”来摆脱这种违反 Sonar 的做法,构建器模式是众所周知的,在创建复杂对象时非常有用。 请参阅“Effective Java”第 2 章第 2 项。 http://www.informit.com/articles/article.aspx?p=1216151&seqNum=2

关于Java - 不可实例化类中缺少静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14408914/

相关文章:

java - SonarQube:结果未显示

sonarqube - 为什么 SonarQube 6.0 临时文件夹中有大量旧数据(总共 36GB)?

maven-3 - Sonar Pitest 插件

java - Tomcat 服务器中的 Log4j FileAppender 问题

java - Spring Boot Web/Tomcat拒绝特定网络的连接(Vodafone)

java - 将 JavaFx 添加到小程序

java - MariaDB准备好的语句不转换where子句的字符集

java - 如何将网络书通知中的参数获取到我的 AWS Lambda 函数中?

svn - Sonar 单片机故障 1 行无故障数据

sonarqube - Sonar Maven插件:3. 1.1 :sonar analyze error after upgrade to SonarQube 6. 0