Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
269efa3
initial impl
Michael-Mc-Mahon Jan 31, 2025
466dab4
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Feb 3, 2025
690cdb1
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Feb 6, 2025
b941943
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Feb 10, 2025
3047d6d
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Feb 12, 2025
ccb31ea
update
Michael-Mc-Mahon Feb 20, 2025
55a43f7
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Feb 21, 2025
fce5f69
update
Michael-Mc-Mahon Feb 28, 2025
d94e95d
update
Michael-Mc-Mahon Mar 5, 2025
c6df8de
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Mar 5, 2025
23f9ee0
moved test
Michael-Mc-Mahon Mar 5, 2025
fa25ccc
whitespace
Michael-Mc-Mahon Mar 5, 2025
c441986
remove file added by mistake
Michael-Mc-Mahon Mar 5, 2025
074da25
doc + copyright update
Michael-Mc-Mahon Mar 6, 2025
41d1ef8
Apply suggestions from code review
Michael-Mc-Mahon Mar 6, 2025
33d1191
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Mar 26, 2025
f5501d1
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Apr 9, 2025
c41146b
update to minimise code changes
Michael-Mc-Mahon Apr 10, 2025
bd5438f
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Apr 15, 2025
da33863
review update
Michael-Mc-Mahon Apr 15, 2025
efabc48
Review update
Michael-Mc-Mahon Apr 24, 2025
9401d4c
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Apr 25, 2025
1a14f4b
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Apr 29, 2025
160d561
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon May 9, 2025
50a1bea
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon May 13, 2025
3b7861b
reduced number of new categories
Michael-Mc-Mahon May 16, 2025
dea5080
update
Michael-Mc-Mahon May 20, 2025
cc518c1
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon May 20, 2025
c723891
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon May 27, 2025
cf179f7
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon May 28, 2025
34f20c9
Additional callsites identified by Mark S.
Michael-Mc-Mahon May 29, 2025
cf5233c
Apply suggestions from code review
Michael-Mc-Mahon May 30, 2025
021b841
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon May 30, 2025
462ee01
typo in suggestions and other issues
Michael-Mc-Mahon May 30, 2025
3eec6c0
Fixed problem with j.n.HostPortRange
Michael-Mc-Mahon May 30, 2025
5970c2b
doc update to java.security
Michael-Mc-Mahon May 30, 2025
b582a0f
removed jmod/Handler change
Michael-Mc-Mahon May 30, 2025
4aad014
doc update to java.security
Michael-Mc-Mahon May 30, 2025
ab6387d
Merge branch 'master' into 8348986-exceptions
Michael-Mc-Mahon Jun 3, 2025
1a6f5af
uodate to JNDI
Michael-Mc-Mahon Jun 3, 2025
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
42 changes: 24 additions & 18 deletions src/java.base/share/classes/java/net/HostPortrange.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2025, 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 @@ -29,6 +29,9 @@
import java.util.Locale;
import sun.net.util.IPAddressUtil;

import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* Parses a string containing a host/domain name and port range
*/
Expand Down Expand Up @@ -56,7 +59,7 @@ public int hashCode() {
return hostname.hashCode() + portrange[0] + portrange[1];
}

HostPortrange(String scheme, String str) {
HostPortrange(String scheme, String host) {
// Parse the host name. A name has up to three components, the
// hostname, a port number, or two numbers representing a port
// range. "www.example.com:8080-9090" is a valid host name.
Expand All @@ -67,21 +70,23 @@ public int hashCode() {
// Refer to RFC 2732 for more information.

// first separate string into two fields: hoststr, portstr
String hoststr, portstr = null;
String hoststr = null, portstr = null;

Choose a reason for hiding this comment

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

you could take the opportunity to update the HostPortrange constructor signature refactor rename
the parameter str to hostname ;-)

this.scheme = scheme;

// check for IPv6 address
if (str.charAt(0) == '[') {
if (host.charAt(0) == '[') {
ipv6 = literal = true;
int rb = str.indexOf(']');
int rb = host.indexOf(']');
if (rb != -1) {
hoststr = str.substring(1, rb);
hoststr = host.substring(1, rb);
} else {
throw new IllegalArgumentException("invalid IPv6 address: " + str);
throw new IllegalArgumentException(
formatMsg("invalid IPv6 address%s",
filterNonSocketInfo(host).prefixWith(": ")));
}
int sep = str.indexOf(':', rb + 1);
if (sep != -1 && str.length() > sep) {
portstr = str.substring(sep + 1);
int sep = host.indexOf(':', rb + 1);
if (sep != -1 && host.length() > sep) {
portstr = host.substring(sep + 1);
}
// need to normalize hoststr now
byte[] ip = IPAddressUtil.textToNumericFormatV6(hoststr);
Expand All @@ -94,16 +99,16 @@ public int hashCode() {
+ "%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7], ip[8],
ip[9], ip[10], ip[11], ip[12], ip[13], ip[14], ip[15]);
hostname = sb.toString();
this.hostname = sb.toString();
} else {
// not IPv6 therefore ':' is the port separator

int sep = str.indexOf(':');
if (sep != -1 && str.length() > sep) {
hoststr = str.substring(0, sep);
portstr = str.substring(sep + 1);
int sep = host.indexOf(':');
if (sep != -1 && host.length() > sep) {
hoststr = host.substring(0, sep);
portstr = host.substring(sep + 1);
} else {
hoststr = sep == -1 ? str : str.substring(0, sep);
hoststr = sep == -1 ? host : host.substring(0, sep);
}
// is this a domain wildcard specification?
if (hoststr.lastIndexOf('*') > 0) {
Expand Down Expand Up @@ -150,13 +155,14 @@ public int hashCode() {
}
}
}
hostname = hoststr;
this.hostname = hoststr;
}

try {
portrange = parsePort(portstr);
} catch (Exception e) {
throw new IllegalArgumentException("invalid port range: " + portstr);
throw new IllegalArgumentException(
formatMsg("invalid port range%s", filterNonSocketInfo(portstr).prefixWith(": ")));
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/java.base/share/classes/java/net/Inet4AddressImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2025, 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 @@ -27,6 +27,8 @@
import java.net.spi.InetAddressResolver.LookupPolicy;

import static java.net.spi.InetAddressResolver.LookupPolicy.IPV4;
import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/*
* Package private implementation of InetAddressImpl for IPv4.
Expand All @@ -38,7 +40,7 @@ final class Inet4AddressImpl implements InetAddressImpl {
public InetAddress[] lookupAllHostAddr(String hostname, LookupPolicy lookupPolicy)
throws UnknownHostException {
if ((lookupPolicy.characteristics() & IPV4) == 0) {
throw new UnknownHostException(hostname);
throw new UnknownHostException(formatMsg("%s", filterNonSocketInfo(hostname)));
}
return lookupAllHostAddr(hostname);
}
Expand Down
8 changes: 6 additions & 2 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, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2025, 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 @@ -35,6 +35,8 @@
import java.util.Enumeration;
import java.util.Arrays;
import java.util.Objects;
import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents an Internet Protocol version 6 (IPv6) address.
Expand Down Expand Up @@ -581,7 +583,9 @@ static InetAddress parseAddressString(String addressLiteral, boolean removeSqBra
if (addrBytes.length == Inet4Address.INADDRSZ) {
if (numericZone != -1 || ifname != null) {
// IPv4-mapped address must not contain zone-id
throw new UnknownHostException(addressLiteral + ": invalid IPv4-mapped address");
throw new UnknownHostException(
formatMsg("%sinvalid IPv4-mapped address",
filterNonSocketInfo(addressLiteral).suffixWith(": ")));
}
return new Inet4Address(null, addrBytes);
}
Expand Down
56 changes: 36 additions & 20 deletions src/java.base/share/classes/java/net/InetAddress.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, 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 @@ -54,6 +54,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

import jdk.internal.util.Exceptions;
import jdk.internal.access.JavaNetInetAddressAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.Blocker;
Expand All @@ -68,6 +69,8 @@
import static java.net.spi.InetAddressResolver.LookupPolicy.IPV4_FIRST;
import static java.net.spi.InetAddressResolver.LookupPolicy.IPV6;
import static java.net.spi.InetAddressResolver.LookupPolicy.IPV6_FIRST;
import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents an Internet Protocol (IP) address.
Expand Down Expand Up @@ -382,6 +385,7 @@ public byte[] addressBytes(Inet6Address inet6Address) {
}
}
);
Exceptions.setup(); // needed for native exceptions
init();
}

Expand Down Expand Up @@ -902,7 +906,7 @@ private static class CachedLookup implements Addresses, Comparable<CachedLookup>
@Override
public InetAddress[] get() throws UnknownHostException {
if (inetAddresses == null) {
throw new UnknownHostException(host);
throw new UnknownHostException(formatMsg("%s", filterNonSocketInfo(host)));
}
return inetAddresses;
}
Expand Down Expand Up @@ -1095,7 +1099,9 @@ public InetAddress[] get() throws UnknownHostException {
}
}
if (inetAddresses == null || inetAddresses.length == 0) {
throw ex == null ? new UnknownHostException(host) : ex;
throw ex == null
? new UnknownHostException(formatMsg("%s", filterNonSocketInfo(host)))
: ex;
}
return inetAddresses;
}
Expand Down Expand Up @@ -1203,16 +1209,19 @@ public String lookupByAddress(byte[] addr) throws UnknownHostException {
}
}
} catch (IOException e) {
throw new UnknownHostException("Unable to resolve address "
+ Arrays.toString(addr) + " as hosts file " + hostsFile
+ " not found ");
throw new UnknownHostException(
formatMsg("Unable to resolve address %s as hosts file %s not found",
filterNonSocketInfo(Arrays.toString(addr)),
filterNonSocketInfo(hostsFile)
.replaceWith("from ${jdk.net.hosts.file} system property")));
}

if ((host == null) || (host.isEmpty()) || (host.equals(" "))) {
throw new UnknownHostException("Requested address "
+ Arrays.toString(addr)
+ " resolves to an invalid entry in hosts file "
+ hostsFile);
throw new UnknownHostException(
formatMsg("Requested address %s resolves to an invalid entry in hosts file %s",
filterNonSocketInfo(Arrays.toString(addr)),
filterNonSocketInfo(hostsFile)
.replaceWith("from ${jdk.net.hosts.file} system property")));
}
return host;
}
Expand Down Expand Up @@ -1273,8 +1282,11 @@ public Stream<InetAddress> lookupByName(String host, LookupPolicy lookupPolicy)
}
}
} catch (IOException e) {
throw new UnknownHostException("Unable to resolve host " + host
+ " as hosts file " + hostsFile + " not found ");
throw new UnknownHostException(
formatMsg("Unable to resolve host %s as hosts file %s not found",
filterNonSocketInfo(host), filterNonSocketInfo(hostsFile)
.replaceWith("from ${jdk.net.hosts.file} system property")));

}
// Check if only IPv4 addresses are requested
if (needIPv4 && !needIPv6) {
Expand Down Expand Up @@ -1305,8 +1317,10 @@ public Stream<InetAddress> lookupByName(String host, LookupPolicy lookupPolicy)
private void checkResultsList(List<InetAddress> addressesList, String hostName)
throws UnknownHostException {
if (addressesList.isEmpty()) {
throw new UnknownHostException("Unable to resolve host " + hostName
+ " in hosts file " + hostsFile);
throw new UnknownHostException(
formatMsg("Unable to resolve host %s in hosts file %s",
filterNonSocketInfo(hostName), filterNonSocketInfo(hostsFile)
.replaceWith("from ${jdk.net.hosts.file} system property")));
}
}

Expand Down Expand Up @@ -1543,7 +1557,7 @@ public static InetAddress[] getAllByName(String host)
// Here we check the address string for ambiguity only
inetAddress = Inet4Address.parseAddressString(host, false);
} catch (IllegalArgumentException iae) {
var uhe = new UnknownHostException(host);
var uhe = new UnknownHostException(formatMsg("%s", filterNonSocketInfo(host)));
uhe.initCause(iae);
throw uhe;
}
Expand All @@ -1570,7 +1584,8 @@ public static InetAddress[] getAllByName(String host)

private static UnknownHostException invalidIPv6LiteralException(String host, boolean wrapInBrackets) {
String hostString = wrapInBrackets ? "[" + host + "]" : host;
return new UnknownHostException(hostString + ": invalid IPv6 address literal");
return new UnknownHostException(formatMsg("%sinvalid IPv6 address literal",
filterNonSocketInfo(hostString).suffixWith(": ")));
}

/**
Expand Down Expand Up @@ -1708,7 +1723,8 @@ static InetAddress[] getAddressesFromNameService(String host)
InetAddress[] result = addresses == null ? null
: addresses.toArray(InetAddress[]::new);
if (result == null || result.length == 0) {
throw ex == null ? new UnknownHostException(host) : ex;
throw ex == null ? new UnknownHostException(formatMsg("%s", filterNonSocketInfo(host)))
: ex;
}
return result;
}
Expand Down Expand Up @@ -1780,9 +1796,9 @@ public static InetAddress getLocalHost() throws UnknownHostException {
localAddr = getAllByName0(local, false)[0];
} catch (UnknownHostException uhe) {
// Rethrow with a more informative error message.
UnknownHostException uhe2 =
new UnknownHostException(local + ": " +
uhe.getMessage());
UnknownHostException uhe2 =
new UnknownHostException(formatMsg(filterNonSocketInfo(local)
.suffixWith(": ") + uhe.getMessage()));
uhe2.initCause(uhe);
throw uhe2;
}
Expand Down
6 changes: 5 additions & 1 deletion src/java.base/share/classes/java/net/NetworkInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents a Network Interface.
Expand Down Expand Up @@ -323,7 +325,9 @@ public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketE
+ addr.holder.family);
}
} else {
throw new IllegalArgumentException("invalid address type: " + addr);
throw new IllegalArgumentException(
formatMsg("invalid address type%s",
filterNonSocketInfo(addr.toString()).prefixWith(": ")));
}
return getByInetAddress0(addr);
}
Expand Down
11 changes: 8 additions & 3 deletions src/java.base/share/classes/java/net/Proxy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, 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 All @@ -26,6 +26,8 @@
package java.net;

import java.util.Objects;
import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents a proxy setting, typically a type (http, socks) and
Expand Down Expand Up @@ -93,8 +95,11 @@ private Proxy() {
* incompatible
*/
public Proxy(Type type, SocketAddress sa) {
if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress))
throw new IllegalArgumentException("type " + type + " is not compatible with address " + sa);
if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress)) {
throw new IllegalArgumentException(
formatMsg("type " + type + " is not compatible with address %s",
filterNonSocketInfo(String.valueOf(sa))));
}
this.type = type;
this.sa = sa;
}
Expand Down
14 changes: 8 additions & 6 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, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2025, 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 @@ -46,6 +46,8 @@
import sun.security.util.SecurityConstants;
import sun.security.util.Debug;

import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents access to a network via sockets.
Expand Down Expand Up @@ -392,8 +394,8 @@ private void init(String host, int mask) {
if (rb != -1) {
host = host.substring(start, rb);
} else {
throw new
IllegalArgumentException("invalid host/port: "+host);
throw new IllegalArgumentException(
formatMsg("invalid host/port%s", filterNonSocketInfo(host).prefixWith(": ")));
}
sep = hostport.indexOf(':', rb+1);
} else {
Expand All @@ -410,8 +412,8 @@ private void init(String host, int mask) {
try {
portrange = parsePort(port);
} catch (Exception e) {
throw new
IllegalArgumentException("invalid port range: "+port);
throw new IllegalArgumentException(
formatMsg("invalid port range%s", filterNonSocketInfo(port).prefixWith(": ")));
}
} else {
portrange = new int[] { PORT_MIN, PORT_MAX };
Expand Down Expand Up @@ -784,7 +786,7 @@ void getIP()
throw uhe;
} catch (IndexOutOfBoundsException iobe) {
invalid = true;
throw new UnknownHostException(getName());
throw new UnknownHostException(formatMsg("%s", filterNonSocketInfo(getName())));
}
}

Expand Down
Loading