weixin_39810856
2021-01-07 14:43 阅读 2

False Positive? initializer method does not guarantee @NonNull field

maybe I'm doing something wrong, don't understand something, and the error message/documentation needs to be improved needs to be improved?


/Users/calebcushing/IdeaProjects/ppm/scaf/src/main/java/com/xenoterracide/scaf/SkeletonConfiguration.java:19: error: [NullAway] initializer method does not guarantee  field after (line 16) is initialized along all control-flow paths (remember to check for exceptions or early returns).
  public SkeletonConfiguration(
         ^
    (see http://t.uber.com/nullaway )
java
/* Copyright © 2020 Caleb Cushing. All rights reserved */
package com.xenoterracide.scaf;

import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Objects;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

class SkeletonConfiguration {

  private final SourceType templateSourceType;
  private final String source;
  private final String destination;
  private final Pattern after;


  SkeletonConfiguration(
     String source,
     String destination,
     Pattern after,
     SourceType templateSourceType
  ) {
    this.templateSourceType = templateSourceType == null ? SourceType.FILE : templateSourceType;
    this.source = Objects.requireNonNull(source);
    this.destination = Objects.requireNonNull(destination);
    this.after = after;
  }

  public SourceType getTemplateSourceType() {
    return templateSourceType;
  }

  public String getSource() {
    return source;
  }

  public String getDestination() {
    return destination;
  }

  public Pattern getAfter() {
    return after;
  }
}

该提问来源于开源项目:uber/NullAway

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • weixin_39944542 weixin_39944542 2021-01-07 14:43

    thanks for the report! I think the issue here is that the after parameter to the constructor is , so doing `this.after = after` does not guarantee that the `after` field is at the end of the constructor.

    That said, this is a bad error message. We should really report an error at this.after = after indicating that you cannot assign to a location. Do you get any error at this line, or do you only get the initializer error?

    点赞 评论 复制链接分享
  • weixin_39661353 weixin_39661353 2021-01-07 14:43

    initializer method does not guarantee field after (line 16) is initialized along all control-flow paths

    Means the constructor doesn't guarantee field after is non-null.

    Since the field after is not marked , it's assumed to be non-null, but the constructor just assigns parameter `after` to it, which is.

    Two ways to fix it:

    1) Make the parameter non-null:

    
      SkeletonConfiguration(
        String source,
        String destination,
        Pattern after,
         SourceType templateSourceType
      ) {
        this.templateSourceType = templateSourceType == null ? SourceType.FILE : templateSourceType;
        this.source = Objects.requireNonNull(source);
        this.destination = Objects.requireNonNull(destination);
        this.after = after;
      }
    

    (NullAway generally doesn't care about ``)

    2) Make the field ``:

    
      private final SourceType templateSourceType;
      private final String source;
      private final String destination;
       private final Pattern after;
    
    点赞 评论 复制链接分享
  • weixin_39810856 weixin_39810856 2021-01-07 14:43

    hmm... ok, guess I have to decorate the getter too... Is there anyway this exception could be improved to include the field name? at least if -parameters was enabled for compilation? or maybe further examples be provided in the documentation related to this?

    note: I went with #2

    点赞 评论 复制链接分享
  • weixin_39661353 weixin_39661353 2021-01-07 14:43

    Hey , the error includes the field name: "initializer method does not guarantee field after (line 16) is initialized along all control-flow paths". Unfortunately, the name of the field in question makes that message very confusing.

    I think the right fix is what mentioned:

    That said, this is a bad error message. We should really report an error at this.after = after indicating that you cannot assign to a location. Do you get any error at this line, or do you only get the initializer error?

    I assume there was no error at that line?

    As a super short term hack, we could add quotes around the parameter name(s) in the existing error message for clarity.

    点赞 评论 复制链接分享
  • weixin_39810856 weixin_39810856 2021-01-07 14:43

    if I counted right.... this is line 19

    
    //     String source,
    

    so no, no error at that line, it basically told me there was an error somewhere in the constructor, not on the field itself or the parameter.

    I've shared the error it gave... that was the only error I got.

    点赞 评论 复制链接分享

相关推荐