8282536: java.net.InetAddress should be a sealed class

Reviewed-by: dfuchs, jpai, rriggs, michaelm
This commit is contained in:
Aleksei Efimov 2022-03-23 14:57:36 +00:00
parent dc45b0ac58
commit 2b291d837e
2 changed files with 5 additions and 25 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2022, 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
@ -593,10 +593,6 @@ class Inet6Address extends InetAddress {
throws IOException, ClassNotFoundException {
NetworkInterface scope_ifname = null;
if (getClass().getClassLoader() != null) {
throw new SecurityException ("invalid address type");
}
ObjectInputStream.GetField gf = s.readFields();
byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]);
int scope_id = gf.get("scope_id", -1);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2022, 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
@ -44,6 +44,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectInputStream.GetField;
import java.io.ObjectOutputStream;
import java.io.ObjectOutputStream.PutField;
import java.io.Serializable;
import java.lang.annotation.Native;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
@ -223,7 +224,7 @@ import static java.net.spi.InetAddressResolver.LookupPolicy.IPV6_FIRST;
* @see java.net.InetAddress#getLocalHost()
* @since 1.0
*/
public class InetAddress implements java.io.Serializable {
public sealed class InetAddress implements Serializable permits Inet4Address, Inet6Address {
/**
* Specify the address family: Internet Protocol, Version 4
@ -1769,16 +1770,6 @@ public class InetAddress implements java.io.Serializable {
return impl.anyLocalAddress();
}
/**
* Initializes an empty InetAddress.
*/
@java.io.Serial
private void readObjectNoData () {
if (getClass().getClassLoader() != null) {
throw new SecurityException ("invalid address type");
}
}
private static final jdk.internal.misc.Unsafe UNSAFE
= jdk.internal.misc.Unsafe.getUnsafe();
private static final long FIELDS_OFFSET
@ -1794,9 +1785,6 @@ public class InetAddress implements java.io.Serializable {
@java.io.Serial
private void readObject (ObjectInputStream s) throws
IOException, ClassNotFoundException {
if (getClass().getClassLoader() != null) {
throw new SecurityException ("invalid address type");
}
GetField gf = s.readFields();
String host = (String)gf.get("hostName", null);
int address = gf.get("address", 0);
@ -1830,11 +1818,7 @@ public class InetAddress implements java.io.Serializable {
* @throws IOException if an I/O error occurs
*/
@java.io.Serial
private void writeObject (ObjectOutputStream s) throws
IOException {
if (getClass().getClassLoader() != null) {
throw new SecurityException ("invalid address type");
}
private void writeObject (ObjectOutputStream s) throws IOException {
PutField pf = s.putFields();
pf.put("hostName", holder().getHostName());
pf.put("address", holder().getAddress());