【问题标题】:Configure generated comment of overrided method配置重写方法的生成注释
【发布时间】:2017-09-20 06:44:30
【问题描述】:

有没有办法,如何改变生成的被覆盖方法的注释 '// ${todo} 自动生成的方法存根' 到另一个文本仅适用于一个方法。

如果有人重写方法,我需要显示警告和提示。

举例:

public class DateHolder{
    public Date date;

    @SomeOverridingAnnotation(generateComment= "WARNING: if override and not use super then add 1 month to result!")
    public int getMonth(){
        return date.getMonth() + 1;
    }
}

public class DateHolder2 extends DateHolder{
    @Override
    public int getMonth() {
        // WARNING: if override and not use super then add 1 month to result!
        return super.getMonth();
    }
}

【问题讨论】:

  • 在我看来是个 XY 问题。

标签: java eclipse


【解决方案1】:

这在很大程度上取决于您使用的是什么编辑器,因为生成的 cmets 是由这些编辑器生成的。您的编辑器或 IDE 是否支持可以访问您的类内容的脚本化 cmets 生成器?

如果不是这样。我怀疑是否有这样一位编辑。愿有另一种方式来达到你想要的。您可以在自己的班级中管理它,例如:

public class DateHolder {
public Date date;

private int dummyInt = -42;

/**
 * Will try to call override method getMonthPlusOne, if there is not such override, then it returns month from date + 1.
 */
public final int getMonth() {
    int res = getMonthPlusOne();
    if ( res == dummyInt )
        return date.getMonth() + 1;
    return res;
}

protected int getMonthPlusOne() {
    return dummyInt;
}

}

public class DateHolder2 extends DateHolder {
    @Override
    protected int getMonthPlusOne() {
        // it will return dummyInt from DateHolder and that will add 1 to month
        return super.getMonthPlusOne();
    }
}

【讨论】:

  • 我知道,但不想要。我正在寻找好的 Eclipse 插件,它允许它(代码模板)
  • 但是如果你找到了,你必须将它与代码一起分发。而类中的解决方案适用于多个编辑器。
  • 你写了普通的java解决方案。我想要 Eclipse 解决方案(Window -> Preferences -> Java -> Cody Style -> Code Templates -> Code - Method body... 可能有一些表达式写在那里,这将对一些方法样式作出反应。Maybye Custom annotation。Maybye Plugin CommentTemplate , Java 编码模板
猜你喜欢
  • 2016-10-27
  • 2012-06-23
  • 1970-01-01
  • 2021-09-29
  • 2012-09-02
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2012-05-09
相关资源
最近更新 更多