redhat-developer / redhat-developer/vscode-java
@Nonnull fields are assumed to be non-null in the constructor
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 2.3k
- Forks
- 546
- Ø Merge
- 20 Std. 1 Min.
- Gemergte PRs (30 T.)
- 11
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die Warnung mit dem bereitgestellten AnnotatedClass-Beispiel in VS Code unter Verwendung der Java-Erweiterung und der gemeldeten JDK-Umgebung zu reproduzieren. Verfolge das Verhalten der Nullanalyse für @Nonnull-Felder während der Konstruktion; abgeschlossen ist die Aufgabe, wenn die Nullprüfung und die spätere Zuweisung keine falschen Warnungen mehr erzeugen, während echte Probleme weiterhin gemeldet werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- java
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100