]> source.dussan.org Git - jgit.git/commit
Introduce a checkNotNull helper 39/139339/1
authorJonathan Nieder <jrn@google.com>
Fri, 22 Mar 2019 16:07:03 +0000 (09:07 -0700)
committerJonathan Nieder <jrn@google.com>
Fri, 22 Mar 2019 16:10:24 +0000 (09:10 -0700)
commit3551e443fc0a2a587911ee88ed905501cad8e59b
treeb80fc3fb8a33ac0d38a36e4b4d7e7f67936f2ff0
parent5bcee1ef6920e056223543183e58b21299269435
Introduce a checkNotNull helper

When using @NonNull annotations in new code, if I write

public void setFrobber(@NonNull frobber) {
this.frobber = frobber;
}

then consumers of the JGit library that do not have nullness checking
enabled can easily pass in null by mistake.  On the other hand, if I
write

public void setFrobber(@NonNull frobber) {
if (frobber == null) {
throw new NullPointerException();
}
this.frobber = frobber;
}

then Eclipse JDT complains:

Null comparison always yields false: The variable frobber is specified as @NonNull

Add a checkNotNull helper that offers the best of both worlds:

public void setFrobber(@NonNull frobber) {
this.frobber = checkNotNull(frobber);
}

Briefer code, null check is intact, and no warning.

Inspired by Guava's com.google.common.base.Preconditions.checkNotNull.

Change-Id: If59588d13a1119e899657ed2296931ea18ed0e2a
org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java