redhat-developer / redhat-developer/vscode-java
@Nonnull fields are assumed to be non-null in the constructor
Nessuno ha ancora preso questa issue.
- Lingua principale
- TypeScript
- Stelle
- 2.3k
- Fork
- 546
- Merge medio
- 20h 1m
- PR unite (30g)
- 11
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l’avviso con l’esempio AnnotatedClass fornito in VS Code, usando l’estensione Java e l’ambiente JDK indicato. Traccia il comportamento dell’analisi dei valori null per i campi @Nonnull durante la costruzione; il lavoro è completato quando il controllo dei valori null e l’assegnazione successiva non producono più avvisi errati, mentre i problemi effettivi continuano a essere segnalati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- java
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100