Skip to content

Commit 91f7b36

Browse files
authored
Add some missing @Nullable annotations. (#129)
Add some missing `@Nullable` annotations. Most of these came to my attention during the testing of [RedundantNullCheck](google/error-prone#5121). Notes: - For `BufferedInputStream` and `FilterInputStream`, see #128. - For `URL`, the docs for `getAuthority()` don't mention `null`, but it's easy enough to demonstrate that `null` is a possible return value: ```java var u = new URL("mailto:foo@bar.com"); System.out.println("authority " + u.getAuthority()); ``` - `AlgorithmParameters.toString()` is gross but documented as such, as eamonnmcmanus points out. - `Matcher.group()` is unfortunate: At one point, it could not return `null`, but that changed, as documented since [JDK-8274663](https://bugs.openjdk.org/browse/JDK-8274663). The change is a consequence of [`usePattern`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/regex/Matcher.html#usePattern(java.util.regex.Pattern)), which was added all the way back in Java 5. I am a little tempted to lie here and leave the type as non-null. - In `Component`, I also removed an annotation on the package-private helper method used by `getName()`. - In `ResultSet`, `getBlob` and `getObject` have no documentation about `null` returns. Gemini [wrote me some code that demonstrates the possible null returns](https://g.co/gemini/share/776a7669ae47). - Also in `ResultSet`, I simplified some type-parameter declarations. This ensures that the `Class<T>` parameter has its type argument in bounds, and it makes no difference to the result type, which is always nullable, regardless of the type argument. Fixes #128
1 parent 942b950 commit 91f7b36

File tree

9 files changed

+20
-19
lines changed

9 files changed

+20
-19
lines changed

src/java.base/share/classes/java/io/BufferedInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class BufferedInputStream extends FilterInputStream {
9191
* indicator that this stream is closed. (The "in" field is also
9292
* nulled out on close.)
9393
*/
94-
protected volatile byte[] buf;
94+
protected volatile byte @Nullable [] buf;
9595

9696
/**
9797
* The index one greater than the index of the last valid byte in

src/java.base/share/classes/java/io/FilterInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class FilterInputStream extends InputStream {
4646
/**
4747
* The input stream to be filtered.
4848
*/
49-
protected volatile InputStream in;
49+
protected volatile @Nullable InputStream in;
5050

5151
/**
5252
* Creates a {@code FilterInputStream}

src/java.base/share/classes/java/lang/ClassLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ protected Package definePackage( String name, @Nullable String specTitle,
20912091
*
20922092
* @since 9
20932093
*/
2094-
public final Package getDefinedPackage(String name) {
2094+
public final @Nullable Package getDefinedPackage(String name) {
20952095
Objects.requireNonNull(name, "name cannot be null");
20962096

20972097
NamedPackage p = packages.get(name);

src/java.base/share/classes/java/net/URL.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ synchronized InetAddress getHostAddress() {
961961
* or <CODE>null</CODE> if one does not exist
962962
* @since 1.3
963963
*/
964-
public String getQuery() {
964+
public @Nullable String getQuery() {
965965
return query;
966966
}
967967

@@ -983,7 +983,7 @@ public String getPath() {
983983
* <CODE>null</CODE> if one does not exist
984984
* @since 1.3
985985
*/
986-
public String getUserInfo() {
986+
public @Nullable String getUserInfo() {
987987
return userInfo;
988988
}
989989

@@ -993,7 +993,7 @@ public String getUserInfo() {
993993
* @return the authority part of this {@code URL}
994994
* @since 1.3
995995
*/
996-
public String getAuthority() {
996+
public @Nullable String getAuthority() {
997997
return authority;
998998
}
999999

@@ -1062,7 +1062,7 @@ public String getFile() {
10621062
* @return the anchor (also known as the "reference") of this
10631063
* {@code URL}, or <CODE>null</CODE> if one does not exist
10641064
*/
1065-
public String getRef() {
1065+
public @Nullable String getRef() {
10661066
return ref;
10671067
}
10681068

src/java.base/share/classes/java/security/AlgorithmParameters.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.security.spec.AlgorithmParameterSpec;
3030
import java.security.spec.InvalidParameterSpecException;
3131
import java.util.Objects;
32+
import org.jspecify.annotations.Nullable;
3233

3334
/**
3435
* This class is used as an opaque representation of cryptographic parameters.
@@ -420,7 +421,7 @@ public final byte[] getEncoded(String format) throws IOException
420421
* @return a formatted string describing the parameters, or {@code null}
421422
* if this parameter object has not been initialized.
422423
*/
423-
public final String toString() {
424+
public final @Nullable String toString() {
424425
if (!this.initialized) {
425426
return null;
426427
}

src/java.base/share/classes/java/util/regex/Matcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ public int end(String name) {
644644
* or if the previous match operation failed
645645
*/
646646

647-
public String group() {
647+
public @Nullable String group() {
648648
return group(0);
649649
}
650650

src/java.desktop/share/classes/java/awt/Component.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ void initializeFocusTraversalKeys() {
991991
* Constructs a name for this component. Called by {@code getName}
992992
* when the name is {@code null}.
993993
*/
994-
@Nullable String constructComponentName() {
994+
String constructComponentName() {
995995
return null; // For strict compliance with prior platform versions, a Component
996996
// that doesn't set its name should return null from
997997
// getName()
@@ -1003,7 +1003,7 @@ void initializeFocusTraversalKeys() {
10031003
* @see #setName
10041004
* @since 1.1
10051005
*/
1006-
public String getName() {
1006+
public @Nullable String getName() {
10071007
if (name == null && !nameExplicitlySet) {
10081008
synchronized(getObjectLock()) {
10091009
if (name == null && !nameExplicitlySet)
@@ -1020,7 +1020,7 @@ public String getName() {
10201020
* @see #getName
10211021
* @since 1.1
10221022
*/
1023-
public void setName(String name) {
1023+
public void setName(@Nullable String name) {
10241024
String oldName;
10251025
synchronized(getObjectLock()) {
10261026
oldName = this.name;

src/java.desktop/share/classes/java/awt/Container.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,7 +2668,7 @@ boolean isSameOrAncestorOf(Component comp, boolean allowChildren) {
26682668
* @see #getComponentAt
26692669
* @since 1.2
26702670
*/
2671-
public Component findComponentAt(int x, int y) {
2671+
public @Nullable Component findComponentAt(int x, int y) {
26722672
return findComponentAt(x, y, true);
26732673
}
26742674

@@ -2763,7 +2763,7 @@ private static Component getChildAt(Component comp, int x, int y,
27632763
* @see #getComponentAt
27642764
* @since 1.2
27652765
*/
2766-
public Component findComponentAt(Point p) {
2766+
public @Nullable Component findComponentAt(Point p) {
27672767
return findComponentAt(p.x, p.y);
27682768
}
27692769

src/java.sql/share/classes/java/sql/ResultSet.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ public interface ResultSet extends Wrapper, AutoCloseable {
652652
* or {@code getBigDecimal(String columnLabel)}
653653
*/
654654
@Deprecated(since="1.2")
655-
BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException;
655+
@Nullable BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException;
656656

657657
/**
658658
* Retrieves the value of the designated column in the current row
@@ -795,7 +795,7 @@ public interface ResultSet extends Wrapper, AutoCloseable {
795795
* if a database access error occurs or this method is
796796
* called on a closed result set
797797
*/
798-
java.io.InputStream getBinaryStream(String columnLabel)
798+
java.io.@Nullable InputStream getBinaryStream(String columnLabel)
799799
throws SQLException;
800800

801801

@@ -2493,7 +2493,7 @@ void updateObject(String columnLabel, @Nullable Object x, int scaleOrLength)
24932493
* this method
24942494
* @since 1.2
24952495
*/
2496-
Blob getBlob(int columnIndex) throws SQLException;
2496+
@Nullable Blob getBlob(int columnIndex) throws SQLException;
24972497

24982498
/**
24992499
* Retrieves the value of the designated column in the current row
@@ -4115,7 +4115,7 @@ void updateCharacterStream(String columnLabel,
41154115
* this method
41164116
* @since 1.7
41174117
*/
4118-
public <T extends @Nullable Object> @Nullable T getObject(int columnIndex, Class<T> type) throws SQLException;
4118+
public <T> @Nullable T getObject(int columnIndex, Class<T> type) throws SQLException;
41194119

41204120

41214121
/**
@@ -4146,7 +4146,7 @@ void updateCharacterStream(String columnLabel,
41464146
* this method
41474147
* @since 1.7
41484148
*/
4149-
public <T extends @Nullable Object> @Nullable T getObject(String columnLabel, Class<T> type) throws SQLException;
4149+
public <T> @Nullable T getObject(String columnLabel, Class<T> type) throws SQLException;
41504150

41514151
//------------------------- JDBC 4.2 -----------------------------------
41524152

0 commit comments

Comments
 (0)