redhat-developer / redhat-developer/vscode-java
@Nonnull fields are assumed to be non-null in the constructor
未关闭
还没有人认领这个 Issue。
bug
code analysis
upstream
- 主要语言
- TypeScript
- 星标
- 2.3k
- 派生
- 547
- 平均合并
- 20 小时 1 分钟
- 30 天内合并 PR
- 11
描述
Class fields annotated with @Nonnull are currently assumed to never be null in the class constructor.
@Nonnull only provides guarantees after construction has finished, and checking these fields in the constructor is often useful to avoid boilerplate variables.
A toy example illustrating the issue:
public class AnnotatedClass {
private @Nonnull String specialValue;
private @Nonnull Map<String, String> allValues = new HashMap<>();
/* this constructor incorrectly warns that specialValue might not have been initialized */
public AnnotatedClass(Iterable<String> values) {
for (String entry : values)
{
String[] split = entry.split("=", 1);
if (split.length != 2) throw new IllegalArgumentException("Value improperly formatted");
final String key = split[0];
final String value = split[1];
if (value == null) /* needed to avoid warning on the assignment to specialValue later */
continue; /* side note: ideally we wouldn't need this, and the IDE */
/* would recognize that entry.split returns array of non-null */
this.allValues.put(key, value);
if ("specialKey".equals(key))
this.specialValue = value;
}
if (this.specialValue == null) /* the next line incorrectly produces a dead code warning */
throw new IllegalArgumentException("The provided iterable does not have a special value!");
}
}
Environment
- Operating System: Windows 10 Enterprise
- JDK version: openjdk 11.0.12 2021-07-20
- Visual Studio Code version: 1.71.2
- Java extension version: v1.11.0
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,在 VS Code 中使用 Java 扩展和报告的 JDK 环境,通过提供的 AnnotatedClass 示例重现该警告。跟踪构造期间对 @Nonnull 字段的 null 分析行为;当 null 检查和后续赋值不再产生错误警告,同时真正的问题仍会被报告时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- java
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100