【问题标题】:Lombok + Intellij: cannot resolve method of super classLombok + Intellij:无法解析超类的方法
【发布时间】:2018-05-28 15:23:50
【问题描述】:

我有一个超类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ErrorResponse {
    @JsonProperty
    private String message;
}

我有一个孩子

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(builderMethodName = "_builder") // add custom builder name to avoid compilation issue 'return type is not compatible...'
@EqualsAndHashCode(callSuper=true)
public class AskQuestionErrorResponse extends ErrorResponse {
    @JsonProperty
    private String status;

    @Builder(builderMethodName = "_builder") // add custom builder name to avoid compilation issue 'return type is not compatible...'
    private AskQuestionErrorResponse(String status, String message){
        super(message);
        this.status = status;
    }
}

当我使用构建器创建这样的对象时

AskQuestionErrorResponse._builder()
   .status(1)
   .message("my message here").build()

Intellij 以红色显示 message 并且存在问题 cannot resolve method 'message(java.lang.String)' 无论如何,即使出现此错误,项目也会编译并运行。

我已经启用了注释处理。

如果我像这样评论超类中的字段

AskQuestionErrorResponse._builder()
                .status(ex.getStatus().getValue()
                //.message(ex.getMessage()
                ).build()

它有效。似乎它没有看到超类成员。我也试过 maven clean and install,rebuild project。

更新 龙目岛插件已安装

注释处理器在PreferencesDefault preferences 中启用

【问题讨论】:

    标签: intellij-idea lombok


    【解决方案1】:

    我找到了。如果您查看我的课程,您会看到两个@Builder 注释。我删除了第一个,魔法发生了。现在我的班级看起来像这样,没有任何警告

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode(callSuper=true)
    public class AskQuestionErrorResponse extends ErrorResponse {
        @JsonProperty
        private String status;
    
        @Builder(builderMethodName = "_builder") // add custom builder name to avoid compilation issue 'return type is not compatible...'
        public AskQuestionErrorResponse(String status, String message){
            super(message);
            this.status = status;
        }
    }
    

    希望对你有帮助:)

    【讨论】:

      【解决方案2】:

      您需要安装 Intellij Lombok 插件,以便它可以在编译为字节码之前理解注释。 https://projectlombok.org/setup/intellij

      【讨论】:

      • 已经安装好了。 AskQuestionErrorResponse._builder().status(ex.getStatus().getValue()).build() 没有超类领域的完美作品
      • 这里有一些额外的信息可能会有所帮助stackoverflow.com/questions/24006937/…
      • @Builder 注释似乎确实存在问题。此处讨论了一种可能(但不完美)的解决方法reinhard.codes/2015/09/16/…
      猜你喜欢
      • 2021-06-21
      • 1970-01-01
      • 2017-03-02
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      相关资源
      最近更新 更多