Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8263233: Update java.net and java.nio to use instanceof pattern variable #2890

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -80,10 +80,9 @@ public Void run() {
super(delegate);
this.socket = socket;
SocketAddress a = proxy.address();
if ( !(a instanceof InetSocketAddress) )
if ( !(a instanceof InetSocketAddress ad) )
throw new IllegalArgumentException("Unsupported address type");

InetSocketAddress ad = (InetSocketAddress) a;
server = ad.getHostString();
port = ad.getPort();
}
Expand Down
5 changes: 2 additions & 3 deletions src/java.base/share/classes/java/net/HttpCookie.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -715,9 +715,8 @@ public String toString() {
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof HttpCookie))
if (!(obj instanceof HttpCookie other))
return false;
HttpCookie other = (HttpCookie)obj;

// One http cookie is equal to another cookie (RFC 2965 sec. 3.3.3) if:
// 1. they come from same domain (case-insensitive),
Expand Down
8 changes: 3 additions & 5 deletions src/java.base/share/classes/java/net/Inet6Address.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -262,10 +262,9 @@ String getHostAddress() {
}

public boolean equals(Object o) {
if (! (o instanceof Inet6AddressHolder)) {
if (!(o instanceof Inet6AddressHolder that)) {
return false;
}
Inet6AddressHolder that = (Inet6AddressHolder)o;

return Arrays.equals(this.ipaddress, that.ipaddress);
}
Expand Down Expand Up @@ -525,10 +524,9 @@ private static int deriveNumericScope (byte[] thisAddr, NetworkInterface ifc) th
Enumeration<InetAddress> addresses = ifc.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if (!(addr instanceof Inet6Address)) {
if (!(addr instanceof Inet6Address ia6_addr)) {
continue;
}
Inet6Address ia6_addr = (Inet6Address)addr;
/* check if site or link local prefixes match */
if (!isDifferentLocalAddressType(thisAddr, ia6_addr.getAddress())){
/* type not the same, so carry on searching */
Expand Down
15 changes: 4 additions & 11 deletions src/java.base/share/classes/java/net/InterfaceAddress.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -101,17 +101,10 @@ public short getNetworkPrefixLength() {
* @see java.net.InterfaceAddress#hashCode()
*/
public boolean equals(Object obj) {
if (obj instanceof InterfaceAddress) {
InterfaceAddress cmp = (InterfaceAddress) obj;

if (Objects.equals(address, cmp.address) &&
return obj instanceof InterfaceAddress cmp &&
Objects.equals(address, cmp.address) &&
Objects.equals(broadcast, cmp.broadcast) &&
pconcannon marked this conversation as resolved.
Show resolved Hide resolved
pconcannon marked this conversation as resolved.
Show resolved Hide resolved
maskLength == cmp.maskLength)
{
return true;
}
}
return false;
maskLength == cmp.maskLength;
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/java.base/share/classes/java/net/NetMulticastSocket.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -216,9 +216,8 @@ public synchronized void bind(SocketAddress addr) throws SocketException {
throw new SocketException("already bound");
if (addr == null)
addr = new InetSocketAddress(0);
if (!(addr instanceof InetSocketAddress))
Copy link

@msheppar msheppar Mar 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the context of this type of change the negative logic is a little obtuse or disjoint in the new style variable declaration, and its subsequent use. I'd find it a more natural flow and easier to read with the instanceof pattern prefacing a block in which the variable is used
if (addr instanceof InetSocketAddress epoint) {
// a block which is using the epoint variable
if (epoint.isUnresolved)
throw new SocketException("Unresolved address");
InetAddress iaddr = epoint.getAddress;
etc, etc ...
} else {
throw new IllegalArgumentException("Unsupported address type!");
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The disadvantage is that it would add another level of braces.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe ... I find it would provide an easier flow to code ... an "easy on the eye" call flow linkage - the variable is declared and then used .... instead of the disconnect of throwing an exception or non immediate use of the variable.

but sure, isn't code somewhat idiosyncratic!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first few times I looked at this pattern, I did stumble over it somewhat. But since then, I find it natural enough and easy to follow. Maybe the concern is transient in nature, until the instanceof pattern matching feature is more widely used.

if (!(addr instanceof InetSocketAddress epoint))
throw new IllegalArgumentException("Unsupported address type!");
InetSocketAddress epoint = (InetSocketAddress) addr;
if (epoint.isUnresolved())
throw new SocketException("Unresolved address");
InetAddress iaddr = epoint.getAddress();
Expand Down Expand Up @@ -259,9 +258,8 @@ public void connect(InetAddress address, int port) {
public void connect(SocketAddress addr) throws SocketException {
if (addr == null)
throw new IllegalArgumentException("Address can't be null");
if (!(addr instanceof InetSocketAddress))
if (!(addr instanceof InetSocketAddress epoint))
throw new IllegalArgumentException("Unsupported address type");
InetSocketAddress epoint = (InetSocketAddress) addr;
if (epoint.isUnresolved())
throw new SocketException("Unresolved address");
connectInternal(epoint.getAddress(), epoint.getPort());
Expand Down
5 changes: 2 additions & 3 deletions src/java.base/share/classes/java/net/NetworkInterface.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -590,10 +590,9 @@ public boolean isVirtual() {
* @see java.net.InetAddress#getAddress()
*/
public boolean equals(Object obj) {
if (!(obj instanceof NetworkInterface)) {
if (!(obj instanceof NetworkInterface that)) {
return false;
}
NetworkInterface that = (NetworkInterface)obj;
if (this.name != null ) {
if (!this.name.equals(that.name)) {
return false;
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/net/ServerSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,8 @@ public void bind(SocketAddress endpoint, int backlog) throws IOException {
throw new SocketException("Already bound");
if (endpoint == null)
endpoint = new InetSocketAddress(0);
if (!(endpoint instanceof InetSocketAddress))
if (!(endpoint instanceof InetSocketAddress epoint))
throw new IllegalArgumentException("Unsupported address type");
InetSocketAddress epoint = (InetSocketAddress) endpoint;
if (epoint.isUnresolved())
throw new SocketException("Unresolved address");
if (backlog < 1)
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/net/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,9 @@ public void connect(SocketAddress endpoint, int timeout) throws IOException {
if (isConnected())
throw new SocketException("already connected");

if (!(endpoint instanceof InetSocketAddress))
if (!(endpoint instanceof InetSocketAddress epoint))
throw new IllegalArgumentException("Unsupported address type");

InetSocketAddress epoint = (InetSocketAddress) endpoint;
InetAddress addr = epoint.getAddress ();
int port = epoint.getPort();
checkAddress(addr, "connect");
Expand Down
18 changes: 5 additions & 13 deletions src/java.base/share/classes/java/net/SocketPermission.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -860,14 +860,12 @@ void getIP()
public boolean implies(Permission p) {
int i,j;

if (!(p instanceof SocketPermission))
if (!(p instanceof SocketPermission that))
return false;

if (p == this)
return true;

SocketPermission that = (SocketPermission) p;

return ((this.mask & that.mask) == that.mask) &&
impliesIgnoreMask(that);
}
Expand Down Expand Up @@ -1040,11 +1038,9 @@ public boolean equals(Object obj) {
if (obj == this)
return true;

if (! (obj instanceof SocketPermission))
if (! (obj instanceof SocketPermission that))
return false;

SocketPermission that = (SocketPermission) obj;

//this is (overly?) complex!!!

// check the mask first
Expand Down Expand Up @@ -1379,15 +1375,13 @@ public SocketPermissionCollection() {
*/
@Override
public void add(Permission permission) {
if (! (permission instanceof SocketPermission))
if (! (permission instanceof SocketPermission sp))
throw new IllegalArgumentException("invalid permission: "+
permission);
if (isReadOnly())
throw new SecurityException(
"attempt to add a Permission to a readonly PermissionCollection");

SocketPermission sp = (SocketPermission)permission;

// Add permission to map if it is absent, or replace with new
// permission if applicable. NOTE: cannot use lambda for
// remappingFunction parameter until JDK-8076596 is fixed.
Expand Down Expand Up @@ -1426,11 +1420,9 @@ public SocketPermission apply(SocketPermission existingVal,
@Override
public boolean implies(Permission permission)
{
if (! (permission instanceof SocketPermission))
if (! (permission instanceof SocketPermission np))
return false;

SocketPermission np = (SocketPermission) permission;

int desired = np.getMask();
int effective = 0;
int needed = desired;
Expand Down
5 changes: 2 additions & 3 deletions src/java.base/share/classes/java/net/SocksSocketImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -57,8 +57,7 @@ class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
SocksSocketImpl(Proxy proxy, SocketImpl delegate) {
super(delegate);
SocketAddress a = proxy.address();
if (a instanceof InetSocketAddress) {
InetSocketAddress ad = (InetSocketAddress) a;
if (a instanceof InetSocketAddress ad) {
// Use getHostString() to avoid reverse lookups
server = ad.getHostString();
serverPort = ad.getPort();
Expand Down
5 changes: 2 additions & 3 deletions src/java.base/share/classes/java/net/URI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -1501,9 +1501,8 @@ public String getFragment() {
public boolean equals(Object ob) {
if (ob == this)
return true;
if (!(ob instanceof URI))
if (!(ob instanceof URI that))
return false;
URI that = (URI)ob;
if (this.isOpaque() != that.isOpaque()) return false;
if (!equalIgnoringCase(this.scheme, that.scheme)) return false;
if (!equal(this.fragment, that.fragment)) return false;
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/net/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,8 @@ public String getRef() {
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (!(obj instanceof URL))
if (!(obj instanceof URL u2))
return false;
URL u2 = (URL)obj;

return handler.equals(this, u2);
}
Expand Down
5 changes: 2 additions & 3 deletions src/java.base/share/classes/java/net/URLClassLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -288,8 +288,7 @@ public InputStream getResourceAsStream(String name) {
}
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream();
if (urlc instanceof JarURLConnection) {
JarURLConnection juc = (JarURLConnection)urlc;
if (urlc instanceof JarURLConnection juc) {
JarFile jar = juc.getJarFile();
synchronized (closeables) {
if (!closeables.containsKey(jar)) {
Expand Down
9 changes: 3 additions & 6 deletions src/java.base/share/classes/java/net/URLPermission.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -298,12 +298,10 @@ public String getActions() {
* </table>
*/
public boolean implies(Permission p) {
if (! (p instanceof URLPermission)) {
if (! (p instanceof URLPermission that)) {
return false;
}

URLPermission that = (URLPermission)p;

if (this.methods.isEmpty() && !that.methods.isEmpty()) {
return false;
}
Expand Down Expand Up @@ -374,10 +372,9 @@ public boolean implies(Permission p) {
* and p's url equals this's url. Returns false otherwise.
*/
public boolean equals(Object p) {
if (!(p instanceof URLPermission)) {
if (!(p instanceof URLPermission that)) {
return false;
}
URLPermission that = (URLPermission)p;
if (!this.scheme.equals(that.scheme)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -194,9 +194,8 @@ public int hashCode() {
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof UnixDomainSocketAddress))
if (!(o instanceof UnixDomainSocketAddress that))
return false;
UnixDomainSocketAddress that = (UnixDomainSocketAddress)o;
return this.path.equals(that.path);
}

Expand Down
5 changes: 2 additions & 3 deletions src/java.base/share/classes/java/nio/StringCharBuffer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -152,9 +152,8 @@ boolean isAddressable() {
public boolean equals(Object ob) {
if (this == ob)
return true;
if (!(ob instanceof CharBuffer))
if (!(ob instanceof CharBuffer that))
return false;
CharBuffer that = (CharBuffer)ob;
int thisPos = this.position();
int thisRem = this.limit() - thisPos;
int thatPos = that.position();
Expand Down
5 changes: 2 additions & 3 deletions src/java.base/share/classes/java/nio/channels/Channels.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -86,8 +86,7 @@ private static void writeFullyImpl(WritableByteChannel ch, ByteBuffer bb)
private static void writeFully(WritableByteChannel ch, ByteBuffer bb)
throws IOException
{
if (ch instanceof SelectableChannel) {
SelectableChannel sc = (SelectableChannel) ch;
if (ch instanceof SelectableChannel sc) {
synchronized (sc.blockingLock()) {
if (!sc.isBlocking())
throw new IllegalBlockingModeException();
Expand Down