【问题标题】:Eclipse Formatter: How to avoid indentation of method declaration after annotation?Eclipse Formatter:如何避免注释后方法声明的缩进?
【发布时间】:2016-02-04 17:09:24
【问题描述】:

我有一个带有注释的方法声明,该注释由 Eclipse Mars 格式化如下:

@Override
        void addLoadShiftingConstraints() throws NoSuchDataException {
    //method body
}

我不想缩进方法声明:

@Override
void addLoadShiftingConstraints() throws NoSuchDataException {
    //method body
}

目前我有以下方法声明的格式化程序设置:

右键单击 Project =>Properties=>Java Code Style=>Formatter =>编辑活动配置文件 => LineWrapping => 方法声明 => 声明 => 必要时换行

如果我选择不换行选项,不需要的缩进就会消失。但是,我想保留长声明的包装。

这是一个 Eclipse 错误还是我必须更改注释的附加设置以避免缩进?

【问题讨论】:

    标签: java eclipse formatter method-declaration


    【解决方案1】:

    (因信誉不足无法评论,但通过搜索找到了这个答案,适用于第二种情况。)

    我在局部变量的缩进方面遇到了类似的问题,只有在注释后放置注释时才会生效。 @Stefan 的回答也适用于此。

     @SuppressWarnings("JavaReflectionMemberAccess") // implicit method Enum#valueOf(String)
     Method method = class_.getMethod("valueOf", String.class);
    

    正在重新格式化为

    @SuppressWarnings("JavaReflectionMemberAccess") // implicit method Enum#valueOf(String)
            Method method = class_.getMethod("valueOf", String.class);
    

    但已通过添加 final 修饰符修复:

    @SuppressWarnings("JavaReflectionMemberAccess") // implicit method Enum#valueOf(String)
    final Method method = class_.getMethod("valueOf", String.class);
    

    【讨论】:

      【解决方案2】:

      这确实是a bug (related)。解决方法是按照您的描述将设置设置为“不换行”或添加访问修饰符,如 private

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-16
        • 2011-11-28
        • 2015-11-05
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多