eclipse-jdt / eclipse-jdt/eclipse.jdt.core
[feat] Add `@LateNonNull` annotation to declare late initializaiton of non-null fields
- 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
Assessment
This issue has not been assessed yet.