2008-11-11 36 views

回答

11

看看Commons4E

它已經有一段時間沒有更新了,但當時我並不想這需要多大改變?

更新:剛剛檢查3.4.1,它工作正常。

+1

因爲這個插件的最後一次更新是2006年,它肯定不會支持的Apache Commons Lang中3 BTW此鏈接可能更爲有用:http://wiki.jiayun.org/Commons4E – 2012-04-24 14:31:52

2

您可以使用Eclipse中的代碼模板來做到這一點。

這是我用HashCodeBuilder和EqualsBuilder的例子找到的solution

模板EqualsBuilder:

public boolean equals(Object o) { 
     boolean result = false; 

     if (this == o) { 
      result = true; 
     } else if (o instanceof $CLASSNAME$) { 
      $CLASSNAME$ other = ($CLASSNAME$) o; 

      result = new org.apache.commons.lang.builder.EqualsBuilder() 
        .append($END$ 
        .isEquals(); 
     } 

     return result; 
    } 

模板HashCodeBuilder:

public int hashCode() { 
     return new org.apache.commons.lang.builder.HashCodeBuilder() 
       .append($END$) 
       .toHashCode(); 
    } 
+0

哪個代碼模板是爲了日食,或者我如何製作一個新的? – daveb 2008-11-11 15:35:22

+0

請參閱有關代碼模板(現有和新的)的帖子:http://eclipse.dzone.com/news/visual-guide-templates-eclipse – 2008-11-11 15:56:42

+0

安德,你對現有素數的權利... – 2008-11-11 15:59:40

3

我使用所謂的 「Commonclipse」

安裝後,你會看到一個新的上下文菜單項 「commonclipse」 Eclipse插件當你在一個java源文件中右擊時。它可以生成基於Apache公共庫的equals,hashcode,toString和compareTo方法。

要安裝它,使用從Eclipse更新中:http://commonclipse.sourceforge.net

-1

的Eclipse Java代碼模板日食3.5.0,布魯諾孔德的模板衍生:

@Override 
public boolean equals(Object obj) { 
    if (obj == null) { 
     return false; 
    } else if (obj == this) { 
     return true; 
    } else if (obj.getClass() != this.getClass()) { 
     return false; 
    } 

    ${enclosing_type} other = (${enclosing_type}) obj; 
    return new EqualsBuilder()// 
      .appendSuper(super.equals(other))// 
      .append(${cursor})// 
       .isEquals(); 
} 

@Override 
public int hashCode() { 
    return new HashCodeBuilder(${cursor})// 
      .append()// 
      .toHashCode(); 
} 
1

我使這個模板檢查了幾個答案,網站並在Eclipse Luna上測試它。轉到Windows->首選項,然後到Java-> Editor-> Templates並將其添加到那裏。

${:import(org.apache.commons.lang3.builder.HashCodeBuilder, org.apache.commons.lang3.builder.EqualsBuilder)} 
@Override 
public int hashCode() { 
    HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(); 
    hashCodeBuilder.append(${field1:field}); 
    hashCodeBuilder.append(${field2:field}); 
    hashCodeBuilder.append(${field3:field}); 
    hashCodeBuilder.append(${field4:field}); 
    hashCodeBuilder.append(${field5:field}); 
    return hashCodeBuilder.toHashCode(); 
} 

@Override 
public boolean equals(Object obj) { 
    if (this == obj) { 
     return true; 
    } 
    if (obj == null) { 
     return false; 
    } 
    if (getClass() != obj.getClass()) { 
     return false; 
    } 
    ${enclosing_type} rhs = (${enclosing_type}) obj; 
    EqualsBuilder equalsBuilder = new EqualsBuilder(); 
    equalsBuilder.append(${field1}, rhs.${field1}); 
    equalsBuilder.append(${field2}, rhs.${field2}); 
    equalsBuilder.append(${field3}, rhs.${field3}); 
    equalsBuilder.append(${field4}, rhs.${field4}); 
    equalsBuilder.append(${field5}, rhs.${field5});${cursor} 
    return equalsBuilder.isEquals(); 
} 
相關問題