Java接口 - 自动生成方法实现(可选,仅限于Eclipse)

时间:2023-02-05 13:00:27

I have an interface defined as:

我有一个接口定义为:

public interface IDomainEntity {
    /**
     * @return
     *   A serialized form of the class Object.
     *   Follows the format:<br/><br/>
     *   <pre class="brush: java">
     *   {@code
     *  public String getPropertiesString() {
     *      String resultString = new StringBuilder()
     *          .append( String.valueOf(this.getClass().getSimpleName()) + "@" + Integer.toHexString(this.hashCode()) + " {" )
     *          .append( " ${property}: '" + String.valueOf(this.${property}) + "'," )
     *          //... above line repeated N-1 times for N properties (${property})...
     *          .append( " }" );
     *          .toString();
     *      
     *      resultString = resultString.replace(", }", " }");
     *      return resultString;
     *  }
     *   </pre>
     */
    public String getPropertiesString();
}

Is there something I can do to cause Eclipse to auto-generate the contents of the method to what I have in my javadoc comment, expanding all direct properties for the class (I don't care about parent properties for inheritance).

我可以做些什么来使Eclipse自动生成方法的内容到我在javadoc注释中的内容,扩展类的所有直接属性(我不关心继承的父属性)。

Currently, Eclipse will give me:

目前,Eclipse将给我:

@Override
public String getPropertiesString() {
    // TODO Auto-generated method stub
    return null;
}

From Java Code Style >> Code Templates >> Code >> Method body (JDT), but I'm looking for something specific to 1 interface and the resulting domain class that implements it.

从Java代码样式>>代码模板>>代码>>方法体(JDT),但我正在寻找特定于1接口的东西以及实现它的结果域类。

Fyi, I am hoping to avoid any solutions revolving around using aspects or reflection to get all of the properties for the class.

Fyi,我希望避免任何解决方案围绕使用方面或反射来获得该类的所有属性。

Thanks!

1 个解决方案

#1


If you are using Java 8 you could add a default implementation to your interface

如果您使用的是Java 8,则可以在界面中添加默认实现

#1


If you are using Java 8 you could add a default implementation to your interface

如果您使用的是Java 8,则可以在界面中添加默认实现