Skip to content

Commit

Permalink
Add missing @Nullable annotation in Socket, and annotate related …
Browse files Browse the repository at this point in the history
…classes. (typetools#71)
  • Loading branch information
cpovirk authored Sep 26, 2023
1 parent 52688e1 commit b6b6321
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
19 changes: 10 additions & 9 deletions src/java.base/share/classes/java/net/DatagramSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package java.net;

import org.checkerframework.checker.interning.qual.UsesObjectEquals;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.AnnotatedFor;

import java.io.IOException;
Expand Down Expand Up @@ -248,7 +249,7 @@
* @see java.nio.channels.DatagramChannel
* @since 1.0
*/
@AnnotatedFor({"interning"})
@AnnotatedFor({"interning", "nullness"})
public @UsesObjectEquals class DatagramSocket implements java.io.Closeable {

// An instance of DatagramSocketAdaptor, NetMulticastSocket, or null
Expand Down Expand Up @@ -330,7 +331,7 @@ protected DatagramSocket(DatagramSocketImpl impl) {
* @see SecurityManager#checkListen
* @since 1.4
*/
public DatagramSocket(SocketAddress bindaddr) throws SocketException {
public DatagramSocket(@Nullable SocketAddress bindaddr) throws SocketException {
this(createDelegate(bindaddr, DatagramSocket.class));
}

Expand Down Expand Up @@ -389,7 +390,7 @@ public DatagramSocket(int port) throws SocketException {
* @see SecurityManager#checkListen
* @since 1.1
*/
public DatagramSocket(int port, InetAddress laddr) throws SocketException {
public DatagramSocket(int port, @Nullable InetAddress laddr) throws SocketException {
this(new InetSocketAddress(laddr, port));
}

Expand All @@ -408,7 +409,7 @@ public DatagramSocket(int port, InetAddress laddr) throws SocketException {
* not supported by this socket.
* @since 1.4
*/
public void bind(SocketAddress addr) throws SocketException {
public void bind(@Nullable SocketAddress addr) throws SocketException {
delegate().bind(addr);
}

Expand Down Expand Up @@ -569,7 +570,7 @@ public boolean isConnected() {
* @return the address to which this socket is connected.
* @since 1.2
*/
public InetAddress getInetAddress() {
public @Nullable InetAddress getInetAddress() {
return delegate().getInetAddress();
}

Expand Down Expand Up @@ -604,7 +605,7 @@ public int getPort() {
* @see #connect(SocketAddress)
* @since 1.4
*/
public SocketAddress getRemoteSocketAddress() {
public @Nullable SocketAddress getRemoteSocketAddress() {
return delegate().getRemoteSocketAddress();
}

Expand All @@ -618,7 +619,7 @@ public SocketAddress getRemoteSocketAddress() {
* @see #bind(SocketAddress)
* @since 1.4
*/
public SocketAddress getLocalSocketAddress() {
public @Nullable SocketAddress getLocalSocketAddress() {
return delegate().getLocalSocketAddress();
}

Expand Down Expand Up @@ -722,7 +723,7 @@ public void receive(DatagramPacket p) throws IOException {
* method does not allow the operation
* @since 1.1
*/
public InetAddress getLocalAddress() {
public @Nullable InetAddress getLocalAddress() {
return delegate().getLocalAddress();
}

Expand Down Expand Up @@ -1102,7 +1103,7 @@ public boolean isClosed() {
*
* @since 1.4
*/
public DatagramChannel getChannel() {
public @Nullable DatagramChannel getChannel() {
return null;
}

Expand Down
13 changes: 10 additions & 3 deletions src/java.base/share/classes/java/net/MulticastSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

package java.net;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.AnnotatedFor;
import org.checkerframework.framework.qual.CFComment;

import java.io.IOException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MulticastChannel;
Expand Down Expand Up @@ -110,6 +114,7 @@
* @author Pavani Diwanji
* @since 1.1
*/
@AnnotatedFor({"nullness"})
public class MulticastSocket extends DatagramSocket {

@Override
Expand Down Expand Up @@ -208,7 +213,7 @@ public MulticastSocket(int port) throws IOException {
*
* @since 1.4
*/
public MulticastSocket(SocketAddress bindaddr) throws IOException {
public MulticastSocket(@Nullable SocketAddress bindaddr) throws IOException {
this(createDelegate(bindaddr, MulticastSocket.class));
}

Expand Down Expand Up @@ -366,7 +371,7 @@ public void leaveGroup(InetAddress mcastaddr) throws IOException {
* @since 1.4
*/
@Override
public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
public void joinGroup(SocketAddress mcastaddr, @Nullable NetworkInterface netIf)
throws IOException {
super.joinGroup(mcastaddr, netIf);
}
Expand All @@ -382,7 +387,7 @@ public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
* @since 1.4
*/
@Override
public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
public void leaveGroup(SocketAddress mcastaddr, @Nullable NetworkInterface netIf)
throws IOException {
super.leaveGroup(mcastaddr, netIf);
}
Expand All @@ -401,6 +406,7 @@ public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
* @see #getInterface()
*/
@Deprecated(since="14")
@CFComment("nullness: TODO: @Nullable parameter or not?")
public void setInterface(InetAddress inf) throws SocketException {
delegate().setInterface(inf);
}
Expand Down Expand Up @@ -440,6 +446,7 @@ public InetAddress getInterface() throws SocketException {
* @see StandardSocketOptions#IP_MULTICAST_IF
* @since 1.4
*/
@CFComment("nullness: TODO: @Nullable parameter or not?")
public void setNetworkInterface(NetworkInterface netIf)
throws SocketException {
delegate().setNetworkInterface(netIf);
Expand Down
15 changes: 8 additions & 7 deletions src/java.base/share/classes/java/net/ServerSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.checkerframework.checker.interning.qual.UsesObjectEquals;
import org.checkerframework.checker.mustcall.qual.CreatesMustCallFor;
import org.checkerframework.checker.mustcall.qual.MustCallAlias;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.AnnotatedFor;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -82,7 +83,7 @@
* @see java.nio.channels.ServerSocketChannel
* @since 1.0
*/
@AnnotatedFor({"calledmethods", "interning", "mustcall"})
@AnnotatedFor({"calledmethods", "interning", "mustcall", "nullness"})
public @UsesObjectEquals class ServerSocket implements java.io.Closeable {
/**
* Various states of this socket.
Expand Down Expand Up @@ -270,7 +271,7 @@ public ServerSocket(int port, int backlog) throws IOException {
* @see SecurityManager#checkListen
* @since 1.1
*/
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
public ServerSocket(int port, int backlog, @Nullable InetAddress bindAddr) throws IOException {
setImpl();
if (port < 0 || port > 0xFFFF)
throw new IllegalArgumentException(
Expand Down Expand Up @@ -346,7 +347,7 @@ void createImpl() throws SocketException {
* @since 1.4
*/
@CreatesMustCallFor
public void bind(SocketAddress endpoint) throws IOException {
public void bind(@Nullable SocketAddress endpoint) throws IOException {
bind(endpoint, 50);
}

Expand Down Expand Up @@ -376,7 +377,7 @@ public void bind(SocketAddress endpoint) throws IOException {
* @since 1.4
*/
@CreatesMustCallFor
public void bind(SocketAddress endpoint, int backlog) throws IOException {
public void bind(@Nullable SocketAddress endpoint, int backlog) throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (isBound())
Expand Down Expand Up @@ -424,7 +425,7 @@ public void bind(SocketAddress endpoint, int backlog) throws IOException {
*
* @see SecurityManager#checkConnect
*/
public InetAddress getInetAddress() {
public @Nullable InetAddress getInetAddress() {
if (!isBound())
return null;
try {
Expand Down Expand Up @@ -493,7 +494,7 @@ public int getLocalPort() {
* @since 1.4
*/

public SocketAddress getLocalSocketAddress() {
public @Nullable SocketAddress getLocalSocketAddress() {
if (!isBound())
return null;
return new InetSocketAddress(getInetAddress(), getLocalPort());
Expand Down Expand Up @@ -744,7 +745,7 @@ public void close() throws IOException {
*
* @since 1.4
*/
public @MustCallAlias ServerSocketChannel getChannel(@MustCallAlias ServerSocket this) {
public @MustCallAlias @Nullable ServerSocketChannel getChannel(@MustCallAlias ServerSocket this) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public Socket(Proxy proxy) {
*
* @since 1.1
*/
protected Socket(SocketImpl impl) throws SocketException {
protected Socket(@Nullable SocketImpl impl) throws SocketException {
checkPermission(impl);
this.impl = impl;
}
Expand Down
4 changes: 3 additions & 1 deletion src/java.base/share/classes/java/net/SocketOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

package java.net;

import org.checkerframework.framework.qual.AnnotatedFor;

/**
* A socket option associated with a socket.
*
Expand All @@ -40,7 +42,7 @@
*
* @see StandardSocketOptions
*/

@AnnotatedFor({"nullness"})
public interface SocketOption<T> {

/**
Expand Down

0 comments on commit b6b6321

Please sign in to comment.