test/jdk/java/io/Serializable/subclass/XObjectOutputStream.java
changeset 58565 baa5969ecf34
parent 47216 71c04702a3d5
equal deleted inserted replaced
58564:218a1a642c6f 58565:baa5969ecf34
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   114         for (int i= 0; i < fields.length; i++) {
   114         for (int i= 0; i < fields.length; i++) {
   115             //Skip non-Serializable fields.
   115             //Skip non-Serializable fields.
   116             int mods = fields[i].getModifiers();
   116             int mods = fields[i].getModifiers();
   117             if (Modifier.isStatic(mods) || Modifier.isTransient(mods))
   117             if (Modifier.isStatic(mods) || Modifier.isTransient(mods))
   118                 continue;
   118                 continue;
   119             Class FieldType = fields[i].getType();
   119             Class<?> FieldType = fields[i].getType();
   120             if (FieldType.isPrimitive()) {
   120             if (FieldType.isPrimitive()) {
   121                 System.out.println("Field " + fields[i].getName() +
   121                 System.out.println("Field " + fields[i].getName() +
   122                                     " has primitive type " + FieldType.toString());
   122                                     " has primitive type " + FieldType.toString());
   123             } else {
   123             } else {
   124                 System.out.println("**Field " + fields[i].getName() +
   124                 System.out.println("**Field " + fields[i].getName() +
   125                                    " is an Object of type " + FieldType.toString());
   125                                    " is an Object of type " + FieldType.toString());
   126                 try {
   126                 try {
   127                     writeObject(fields[i].get(obj));
   127                     writeObject(fields[i].get(obj));
   128                     if (FieldType.isArray()) {
   128                     if (FieldType.isArray()) {
   129                         Object[] array = ((Object[]) fields[i].get(obj));
   129                         Object[] array = ((Object[]) fields[i].get(obj));
   130                         Class componentType = FieldType.getComponentType();
   130                         Class<?> componentType = FieldType.getComponentType();
   131                         if (componentType.isPrimitive())
   131                         if (componentType.isPrimitive())
   132                             System.out.println("Output " + array.length + " primitive elements of" +
   132                             System.out.println("Output " + array.length + " primitive elements of" +
   133                                                componentType.toString());
   133                                                componentType.toString());
   134                         else {
   134                         else {
   135                             System.out.println("Output " + array.length + " of Object elements of" +
   135                             System.out.println("Output " + array.length + " of Object elements of" +
   225         }
   225         }
   226 
   226 
   227         /**
   227         /**
   228          * Write the data and fields to the specified ObjectOutput stream.
   228          * Write the data and fields to the specified ObjectOutput stream.
   229          */
   229          */
       
   230         @SuppressWarnings("deprecation")
   230         public void write(ObjectOutput out) throws IOException {
   231         public void write(ObjectOutput out) throws IOException {
   231             for (int i = 0; i < next; i++)
   232             for (int i = 0; i < next; i++)
   232                 System.out.println(fieldName[i] + "=" + intValue[i]);
   233                 System.out.println(fieldName[i] + "=" + intValue[i]);
   233         }
   234         }
   234     };
   235     };
   298      *
   299      *
   299      * Look for the writeObject method
   300      * Look for the writeObject method
   300      * Set the accessible flag on it here.
   301      * Set the accessible flag on it here.
   301      * Subclass of AbstractObjectOutputStream will call it as necessary.
   302      * Subclass of AbstractObjectOutputStream will call it as necessary.
   302      */
   303      */
   303     public static Method getWriteObjectMethod(final Class cl) {
   304     public static Method getWriteObjectMethod(final Class<?> cl) {
   304 
   305 
   305         Method writeObjectMethod = (Method)
   306         Method writeObjectMethod =
   306             java.security.AccessController.doPrivileged
   307             java.security.AccessController.doPrivileged
   307             (new java.security.PrivilegedAction() {
   308             (new java.security.PrivilegedAction<Method>() {
   308                 public Object run() {
   309                 public Method run() {
   309                     Method m = null;
   310                     Method m = null;
   310                     try {
   311                     try {
   311                         Class[] args = {ObjectOutputStream.class};
   312                         Class<?>[] args = {ObjectOutputStream.class};
   312                         m = cl.getDeclaredMethod("writeObject", args);
   313                         m = cl.getDeclaredMethod("writeObject", args);
   313                         int mods = m.getModifiers();
   314                         int mods = m.getModifiers();
   314                         // Method must be private and non-static
   315                         // Method must be private and non-static
   315                         if (!Modifier.isPrivate(mods) ||
   316                         if (!Modifier.isPrivate(mods) ||
   316                             Modifier.isStatic(mods)) {
   317                             Modifier.isStatic(mods)) {
   334                                         final Object[] argList)
   335                                         final Object[] argList)
   335         throws IOException
   336         throws IOException
   336     {
   337     {
   337         try {
   338         try {
   338             java.security.AccessController.doPrivileged
   339             java.security.AccessController.doPrivileged
   339                 (new java.security.PrivilegedExceptionAction() {
   340                 (new java.security.PrivilegedExceptionAction<Void>() {
   340                     public Object run() throws InvocationTargetException,
   341                     public Void run() throws InvocationTargetException,
   341                                         java.lang.IllegalAccessException {
   342                                         java.lang.IllegalAccessException {
   342                         m.invoke(obj, argList);
   343                         m.invoke(obj, argList);
   343                         return null;
   344                         return null;
   344                     }
   345                     }
   345                 });
   346                 });