Skip to content
This repository was archived by the owner on Apr 24, 2023. It is now read-only.
/ jdk20 Public archive

Commit d0a7679

Browse files
author
Roger Riggs
committed
4958969: ObjectOutputStream example leads to non-working code
Reviewed-by: lancea, naoto
1 parent f07acfc commit d0a7679

File tree

3 files changed

+58
-60
lines changed

3 files changed

+58
-60
lines changed

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

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -80,14 +80,15 @@
8080
*
8181
* <p>For example, a filter that allows example classes, allows classes in the
8282
* {@code java.base} module, and rejects all other classes can be set:
83-
*
84-
* <pre>{@code As a command line property:
85-
* % java -Djdk.serialFilter="example.*;java.base/*;!*" ...}</pre>
86-
*
87-
* <pre>{@code Or programmatically:
83+
* As a command line property:
84+
* {@snippet :
85+
* % java -Djdk.serialFilter="example.*;java.base/*;!*" ...
86+
* }
87+
* Or programmatically:
88+
* {@snippet lang="java":
8889
* var filter = ObjectInputFilter.Config.createFilter("example.*;java.base/*;!*")
89-
* ObjectInputFilter.Config.setSerialFilter(filter);}</pre>
90-
*
90+
* ObjectInputFilter.Config.setSerialFilter(filter);
91+
* }
9192
* <p>In an application with multiple execution contexts, the application can provide a
9293
* {@linkplain Config#setSerialFilterFactory(BinaryOperator) filter factory} to
9394
* protect individual contexts by providing a custom filter for each. When the stream
@@ -191,7 +192,7 @@
191192
* The {@code doWithSerialFilter} method does the setup of the thread-specific filter
192193
* and invokes the application provided {@link Runnable Runnable}.
193194
*
194-
* <pre>{@code
195+
* {@snippet lang="java":
195196
* public static final class FilterInThread implements BinaryOperator<ObjectInputFilter> {
196197
*
197198
* private final ThreadLocal<ObjectInputFilter> filterThreadLocal = new ThreadLocal<>();
@@ -242,12 +243,12 @@
242243
* }
243244
* }
244245
* }
245-
* }</pre>
246+
* }
246247
* <h3>Using the Filter Factory</h3>
247248
* To use {@code FilterInThread} utility create an instance and configure it as the
248249
* JVM-wide filter factory. The {@code doWithSerialFilter} method is invoked with a
249250
* filter allowing the example application and core classes:
250-
* <pre>{@code
251+
* {@snippet lang="java":
251252
* // Create a FilterInThread filter factory and set
252253
* var filterInThread = new FilterInThread();
253254
* ObjectInputFilter.Config.setSerialFilterFactory(filterInThread);
@@ -258,7 +259,7 @@
258259
* byte[] bytes = ...;
259260
* var o = deserializeObject(bytes);
260261
* });
261-
* }</pre>
262+
* }
262263
* <p>
263264
* Unless otherwise noted, passing a {@code null} argument to a
264265
* method in this interface and its nested classes will cause a
@@ -310,11 +311,11 @@ public interface ObjectInputFilter {
310311
* <p>
311312
* Example, to create a filter that will allow any class loaded from the platform
312313
* or bootstrap classloaders.
313-
* <pre><code>
314+
* {@snippet lang="java":
314315
* ObjectInputFilter f
315316
* = allowFilter(cl -> cl.getClassLoader() == ClassLoader.getPlatformClassLoader() ||
316317
* cl.getClassLoader() == null, Status.UNDECIDED);
317-
* </code></pre>
318+
* }
318319
*
319320
* @param predicate a predicate to test a non-null Class
320321
* @param otherStatus a Status to use if the predicate is {@code false}
@@ -344,10 +345,10 @@ static ObjectInputFilter allowFilter(Predicate<Class<?>> predicate, Status other
344345
* </ul>
345346
* <p>
346347
* Example, to create a filter that will reject any class loaded from the application classloader.
347-
* <pre><code>
348+
* {@snippet lang="java":
348349
* ObjectInputFilter f = rejectFilter(cl ->
349350
* cl.getClassLoader() == ClassLoader.ClassLoader.getSystemClassLoader(), Status.UNDECIDED);
350-
* </code></pre>
351+
* }
351352
*
352353
* @param predicate a predicate to test a non-null Class
353354
* @param otherStatus a Status to use if the predicate is {@code false}

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

+23-23
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,18 @@
116116
* the object's most specific class.
117117
*
118118
* <p>For example to read from a stream as written by the example in
119-
* ObjectOutputStream:
119+
* {@link ObjectOutputStream}:
120120
* <br>
121-
* <pre>
122-
* FileInputStream fis = new FileInputStream("t.tmp");
123-
* ObjectInputStream ois = new ObjectInputStream(fis);
124-
*
125-
* int i = ois.readInt();
126-
* String today = (String) ois.readObject();
127-
* Date date = (Date) ois.readObject();
128-
*
129-
* ois.close();
130-
* </pre>
121+
* {@snippet lang="java" :
122+
* try (FileInputStream fis = new FileInputStream("t.tmp");
123+
* ObjectInputStream ois = new ObjectInputStream(fis)) {
124+
* String label = (String) ois.readObject();
125+
* LocalDateTime dateTime = (LocalDateTime) ois.readObject();
126+
* // Use label and dateTime
127+
* } catch (Exception ex) {
128+
* // handle exception
129+
* }
130+
* }
131131
*
132132
* <p>Classes control how they are serialized by implementing either the
133133
* java.io.Serializable or java.io.Externalizable interfaces.
@@ -142,14 +142,14 @@
142142
* serialization and deserialization process should implement methods
143143
* with the following signatures:
144144
*
145-
* <pre>
146-
* private void writeObject(java.io.ObjectOutputStream stream)
147-
* throws IOException;
148-
* private void readObject(java.io.ObjectInputStream stream)
149-
* throws IOException, ClassNotFoundException;
150-
* private void readObjectNoData()
151-
* throws ObjectStreamException;
152-
* </pre>
145+
* {@snippet lang="java":
146+
* private void writeObject(java.io.ObjectOutputStream stream)
147+
* throws IOException;
148+
* private void readObject(java.io.ObjectInputStream stream)
149+
* throws IOException, ClassNotFoundException;
150+
* private void readObjectNoData()
151+
* throws ObjectStreamException;
152+
* }
153153
*
154154
* <p>The method name, modifiers, return type, and number and type of
155155
* parameters must match exactly for the method to be used by
@@ -771,9 +771,9 @@ public void registerValidation(ObjectInputValidation obj, int prio)
771771
*
772772
* <p>The default implementation of this method in
773773
* {@code ObjectInputStream} returns the result of calling
774-
* <pre>
774+
* {@snippet lang="java":
775775
* Class.forName(desc.getName(), false, loader)
776-
* </pre>
776+
* }
777777
* where {@code loader} is the first class loader on the current
778778
* thread's stack (starting from the currently executing method) that is
779779
* neither the {@linkplain ClassLoader#getPlatformClassLoader() platform
@@ -833,9 +833,9 @@ protected Class<?> resolveClass(ObjectStreamClass desc)
833833
* objects for the interfaces that are named in the {@code interfaces}
834834
* parameter. The {@code Class} object for each interface name
835835
* {@code i} is the value returned by calling
836-
* <pre>
836+
* {@snippet lang="java":
837837
* Class.forName(i, false, loader)
838-
* </pre>
838+
* }
839839
* where {@code loader} is the first class loader on the current
840840
* thread's stack (starting from the currently executing method) that is
841841
* neither the {@linkplain ClassLoader#getPlatformClassLoader() platform

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

+18-21
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,29 @@
6666
* written.
6767
*
6868
* <p>For example to write an object that can be read by the example in
69-
* ObjectInputStream:
70-
* <br>
71-
* <pre>
72-
* FileOutputStream fos = new FileOutputStream("t.tmp");
73-
* ObjectOutputStream oos = new ObjectOutputStream(fos);
74-
*
75-
* oos.writeInt(12345);
76-
* oos.writeObject("Today");
77-
* oos.writeObject(new Date());
78-
*
79-
* oos.close();
80-
* </pre>
69+
* {@link ObjectInputStream}:
70+
* {@snippet lang="java":
71+
* try (FileOutputStream fos = new FileOutputStream("t.tmp");
72+
* ObjectOutputStream oos = new ObjectOutputStream(fos)) {
73+
* oos.writeObject("Today");
74+
* oos.writeObject(LocalDateTime.now());
75+
* } catch (Exception ex) {
76+
* // handle exception
77+
* }
78+
* }
8179
*
8280
* <p>Serializable classes that require special handling during the
8381
* serialization and deserialization process should implement methods
8482
* with the following signatures:
8583
*
86-
* <br>
87-
* <pre>
88-
* private void readObject(java.io.ObjectInputStream stream)
89-
* throws IOException, ClassNotFoundException;
90-
* private void writeObject(java.io.ObjectOutputStream stream)
91-
* throws IOException
92-
* private void readObjectNoData()
93-
* throws ObjectStreamException;
94-
* </pre>
84+
* {@snippet lang="java":
85+
* private void readObject(java.io.ObjectInputStream stream)
86+
* throws IOException, ClassNotFoundException;
87+
* private void writeObject(java.io.ObjectOutputStream stream)
88+
* throws IOException;
89+
* private void readObjectNoData()
90+
* throws ObjectStreamException;
91+
* }
9592
*
9693
* <p>The method name, modifiers, return type, and number and type of
9794
* parameters must match exactly for the method to be used by

0 commit comments

Comments
 (0)