diff --git a/src/java.base/share/classes/java/io/ObjectInputFilter.java b/src/java.base/share/classes/java/io/ObjectInputFilter.java index 3ec164f5aea..ab16c8cf1a3 100644 --- a/src/java.base/share/classes/java/io/ObjectInputFilter.java +++ b/src/java.base/share/classes/java/io/ObjectInputFilter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 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 @@ -80,14 +80,15 @@ import static java.lang.System.Logger.Level.ERROR; * *

For example, a filter that allows example classes, allows classes in the * {@code java.base} module, and rejects all other classes can be set: - * - *

{@code As a command line property:
- *     % java -Djdk.serialFilter="example.*;java.base/*;!*" ...}
- * - *
{@code Or programmatically:
+ * As a command line property:
+ * {@snippet :
+ *     % java -Djdk.serialFilter="example.*;java.base/*;!*" ...
+ * }
+ * Or programmatically:
+ * {@snippet lang="java":
  *     var filter = ObjectInputFilter.Config.createFilter("example.*;java.base/*;!*")
- *     ObjectInputFilter.Config.setSerialFilter(filter);}
- * + * ObjectInputFilter.Config.setSerialFilter(filter); + * } *

In an application with multiple execution contexts, the application can provide a * {@linkplain Config#setSerialFilterFactory(BinaryOperator) filter factory} to * protect individual contexts by providing a custom filter for each. When the stream @@ -191,7 +192,7 @@ import static java.lang.System.Logger.Level.ERROR; * The {@code doWithSerialFilter} method does the setup of the thread-specific filter * and invokes the application provided {@link Runnable Runnable}. * - *

{@code
+ * {@snippet lang="java":
  * public static final class FilterInThread implements BinaryOperator {
  *
  *     private final ThreadLocal filterThreadLocal = new ThreadLocal<>();
@@ -242,12 +243,12 @@ import static java.lang.System.Logger.Level.ERROR;
  *         }
  *     }
  * }
- * }
+ * } *

Using the Filter Factory

* To use {@code FilterInThread} utility create an instance and configure it as the * JVM-wide filter factory. The {@code doWithSerialFilter} method is invoked with a * filter allowing the example application and core classes: - *
{@code
+ * {@snippet lang="java":
  *        // Create a FilterInThread filter factory and set
  *        var filterInThread = new FilterInThread();
  *        ObjectInputFilter.Config.setSerialFilterFactory(filterInThread);
@@ -258,7 +259,7 @@ import static java.lang.System.Logger.Level.ERROR;
  *              byte[] bytes = ...;
  *              var o = deserializeObject(bytes);
  *        });
- * }
+ * } *

* Unless otherwise noted, passing a {@code null} argument to a * method in this interface and its nested classes will cause a @@ -310,11 +311,11 @@ public interface ObjectInputFilter { *

* Example, to create a filter that will allow any class loaded from the platform * or bootstrap classloaders. - *


+     * {@snippet lang="java":
      *     ObjectInputFilter f
      *         = allowFilter(cl -> cl.getClassLoader() == ClassLoader.getPlatformClassLoader() ||
      *                       cl.getClassLoader() == null, Status.UNDECIDED);
-     * 
+ * } * * @param predicate a predicate to test a non-null Class * @param otherStatus a Status to use if the predicate is {@code false} @@ -344,10 +345,10 @@ public interface ObjectInputFilter { * *

* Example, to create a filter that will reject any class loaded from the application classloader. - *


+     * {@snippet lang="java":
      *     ObjectInputFilter f = rejectFilter(cl ->
      *          cl.getClassLoader() == ClassLoader.ClassLoader.getSystemClassLoader(), Status.UNDECIDED);
-     * 
+ * } * * @param predicate a predicate to test a non-null Class * @param otherStatus a Status to use if the predicate is {@code false} diff --git a/src/java.base/share/classes/java/io/ObjectInputStream.java b/src/java.base/share/classes/java/io/ObjectInputStream.java index 6b5d5412402..8ddfd3fb329 100644 --- a/src/java.base/share/classes/java/io/ObjectInputStream.java +++ b/src/java.base/share/classes/java/io/ObjectInputStream.java @@ -116,18 +116,18 @@ import sun.security.action.GetIntegerAction; * the object's most specific class. * *

For example to read from a stream as written by the example in - * ObjectOutputStream: + * {@link ObjectOutputStream}: *
- *

- *      FileInputStream fis = new FileInputStream("t.tmp");
- *      ObjectInputStream ois = new ObjectInputStream(fis);
- *
- *      int i = ois.readInt();
- *      String today = (String) ois.readObject();
- *      Date date = (Date) ois.readObject();
- *
- *      ois.close();
- * 
+ * {@snippet lang="java" : + * try (FileInputStream fis = new FileInputStream("t.tmp"); + * ObjectInputStream ois = new ObjectInputStream(fis)) { + * String label = (String) ois.readObject(); + * LocalDateTime dateTime = (LocalDateTime) ois.readObject(); + * // Use label and dateTime + * } catch (Exception ex) { + * // handle exception + * } + * } * *

Classes control how they are serialized by implementing either the * java.io.Serializable or java.io.Externalizable interfaces. @@ -142,14 +142,14 @@ import sun.security.action.GetIntegerAction; * serialization and deserialization process should implement methods * with the following signatures: * - *

- * private void writeObject(java.io.ObjectOutputStream stream)
- *     throws IOException;
- * private void readObject(java.io.ObjectInputStream stream)
- *     throws IOException, ClassNotFoundException;
- * private void readObjectNoData()
- *     throws ObjectStreamException;
- * 
+ * {@snippet lang="java": + * private void writeObject(java.io.ObjectOutputStream stream) + * throws IOException; + * private void readObject(java.io.ObjectInputStream stream) + * throws IOException, ClassNotFoundException; + * private void readObjectNoData() + * throws ObjectStreamException; + * } * *

The method name, modifiers, return type, and number and type of * parameters must match exactly for the method to be used by @@ -771,9 +771,9 @@ public class ObjectInputStream * *

The default implementation of this method in * {@code ObjectInputStream} returns the result of calling - *

+     * {@snippet lang="java":
      *     Class.forName(desc.getName(), false, loader)
-     * 
+ * } * where {@code loader} is the first class loader on the current * thread's stack (starting from the currently executing method) that is * neither the {@linkplain ClassLoader#getPlatformClassLoader() platform @@ -833,9 +833,9 @@ public class ObjectInputStream * objects for the interfaces that are named in the {@code interfaces} * parameter. The {@code Class} object for each interface name * {@code i} is the value returned by calling - *
+     * {@snippet lang="java":
      *     Class.forName(i, false, loader)
-     * 
+ * } * where {@code loader} is the first class loader on the current * thread's stack (starting from the currently executing method) that is * neither the {@linkplain ClassLoader#getPlatformClassLoader() platform diff --git a/src/java.base/share/classes/java/io/ObjectOutputStream.java b/src/java.base/share/classes/java/io/ObjectOutputStream.java index fa954eea988..e2f426d9701 100644 --- a/src/java.base/share/classes/java/io/ObjectOutputStream.java +++ b/src/java.base/share/classes/java/io/ObjectOutputStream.java @@ -66,32 +66,29 @@ import sun.reflect.misc.ReflectUtil; * written. * *

For example to write an object that can be read by the example in - * ObjectInputStream: - *
- *

- *      FileOutputStream fos = new FileOutputStream("t.tmp");
- *      ObjectOutputStream oos = new ObjectOutputStream(fos);
- *
- *      oos.writeInt(12345);
- *      oos.writeObject("Today");
- *      oos.writeObject(new Date());
- *
- *      oos.close();
- * 
+ * {@link ObjectInputStream}: + * {@snippet lang="java": + * try (FileOutputStream fos = new FileOutputStream("t.tmp"); + * ObjectOutputStream oos = new ObjectOutputStream(fos)) { + * oos.writeObject("Today"); + * oos.writeObject(LocalDateTime.now()); + * } catch (Exception ex) { + * // handle exception + * } + * } * *

Serializable classes that require special handling during the * serialization and deserialization process should implement methods * with the following signatures: * - *
- *

- * private void readObject(java.io.ObjectInputStream stream)
- *     throws IOException, ClassNotFoundException;
- * private void writeObject(java.io.ObjectOutputStream stream)
- *     throws IOException
- * private void readObjectNoData()
- *     throws ObjectStreamException;
- * 
+ * {@snippet lang="java": + * private void readObject(java.io.ObjectInputStream stream) + * throws IOException, ClassNotFoundException; + * private void writeObject(java.io.ObjectOutputStream stream) + * throws IOException; + * private void readObjectNoData() + * throws ObjectStreamException; + * } * *

The method name, modifiers, return type, and number and type of * parameters must match exactly for the method to be used by