eclipse-jdt / eclipse-jdt/eclipse.jdt.core

[feat] Add `@LateNonNull` annotation to declare late initializaiton of non-null fields

Open
#2,829 3 comments 0 reactions 0 assignees View on GitHub
null
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

I suggest the introduction of `@LateNonNull` or similar that marks a class field as non null but does not require assignment of a value during object instantiation. Dart address with the `late` keyword, Kotlin with `lateinit`, and the checker framework has `@MonotonicNonNull`.

I am currently using this workaround which can result in unexpected side effects when class inheritance comes into play:

```java
public class NullSafetyHelper {
@SuppressWarnings("null")
public static @NonNull T lateNonNull() {
return (@NonNull T) null;
}
}
```

```java
package org.eclipse.tm4e.ui.internal.wizards;

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.*;

class SelectGrammarWizardPage extends AbstractWizardPage {

private @NonNull Text grammarFileText = lateNonNull(); // initialize a non-null field with null

@Override
protected void createBody(Composite ancestor) {
var parent = new Composite(ancestor, SWT.NONE);
parent.setLayout(new GridLayout(2, false));

grammarFileText = createText(parent, TMUIMessages.SelectGrammarWizardPage_file_label);
grammarFileText.addListener(SWT.Modify, this);
// ...
}

@Override
protected IStatus validatePage(Event event) {
String path = grammarFileText.getText();
// ...
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.