jdk/src/share/classes/javax/sql/rowset/serial/SerialJavaObject.java
author lancea
Tue, 06 Nov 2012 14:59:22 -0500
changeset 14409 d879c92507ec
parent 11129 f9ad1aadf3fa
child 14781 701d0765f75f
permissions -rw-r--r--
8002212: adding read/writeObject to additional SerialXXX classes Reviewed-by: naoto, forax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.sql.rowset.serial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.*;
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
    30
import java.util.Arrays;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
    31
import java.util.Vector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.sql.rowset.RowSetWarning;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A serializable mapping in the Java programming language of an SQL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>JAVA_OBJECT</code> value. Assuming the Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * implements the <code>Serializable</code> interface, this class simply wraps the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * serialization process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * If however, the serialization is not possible because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the Java object is not immediately serializable, this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * attempt to serialize all non-static members to permit the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * state to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Static or transient fields cannot be serialized; an attempt to serialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * them will result in a <code>SerialException</code> object being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Jonathan Bruce
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class SerialJavaObject implements Serializable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Placeholder for object to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
    54
    private Object obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    * Placeholder for all fields in the <code>JavaObject</code> being serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private transient Field[] fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Constructor for <code>SerialJavaObject</code> helper class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param obj the Java <code>Object</code> to be serialized
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    67
     * @throws SerialException if the object is found not to be serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public SerialJavaObject(Object obj) throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // if any static fields are found, an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // should be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // get Class. Object instance should always be available
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    76
        Class<?> c = obj.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // determine if object implements Serializable i/f
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    79
        if (!(obj instanceof java.io.Serializable)) {
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    80
            setWarning(new RowSetWarning("Warning, the object passed to the constructor does not implement Serializable"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // can only determine public fields (obviously). If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // any of these are static, this should invalidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // the action of attempting to persist these fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // in a serialized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        fields = c.getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
    89
        if (hasStaticFields(fields)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new SerialException("Located static fields in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                "object instance. Cannot serialize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.obj = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return a copy of this <code>SerialJavaObject</code> object as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *         <code>Object</code> in the Java programming language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws SerialException if the instance is corrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public Object getObject() throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return this.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns an array of <code>Field</code> objects that contains each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * field of the object that this helper class is serializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return an array of <code>Field</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @throws SerialException if an error is encountered accessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * the serialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public Field[] getFields() throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (fields != null) {
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
   119
            Class<?> c = this.obj.getClass();
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
   120
            return c.getFields();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new SerialException("SerialJavaObject does not contain" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                " a serialized object instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   128
     * The identifier that assists in the serialization of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>SerialJavaObject</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static final long serialVersionUID = -1465795139032831023L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * A container for the warnings issued on this <code>SerialJavaObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * object. When there are multiple warnings, each warning is chained to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * previous warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   138
    Vector<RowSetWarning> chain;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   139
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   140
    /**
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   141
     * Compares this SerialJavaObject to the specified object.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   142
     * The result is {@code true} if and only if the argument
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   143
     * is not {@code null} and is a {@code SerialJavaObject}
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   144
     * object that is identical to this object
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   145
     *
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   146
     * @param  o The object to compare this {@code SerialJavaObject} against
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   147
     *
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   148
     * @return  {@code true} if the given object represents a {@code SerialJavaObject}
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   149
     *          equivalent to this SerialJavaObject, {@code false} otherwise
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   150
     *
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   151
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   152
    public boolean equals(Object o) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   153
        if (this == o) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   154
            return true;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   155
        }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   156
        if (o instanceof SerialJavaObject) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   157
            SerialJavaObject sjo = (SerialJavaObject) o;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   158
            return obj.equals(sjo.obj);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   159
        }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   160
        return false;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   161
    }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   162
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   163
    /**
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   164
     * Returns a hash code for this SerialJavaObject. The hash code for a
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   165
     * {@code SerialJavaObject} object is taken as the hash code of
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   166
     * the {@code Object} it stores
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   167
     *
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   168
     * @return  a hash code value for this object.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   169
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   170
    public int hashCode() {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   171
        return 31 + obj.hashCode();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   172
    }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   173
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   174
    /**
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   175
     * Returns a clone of this {@code SerialJavaObject}.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   176
     *
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   177
     * @return  a clone of this SerialJavaObject
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   178
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   179
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   180
    public Object clone() {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   181
        try {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   182
            SerialJavaObject sjo = (SerialJavaObject) super.clone();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   183
            sjo.fields = Arrays.copyOf(fields, fields.length);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   184
            if (chain != null)
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   185
                sjo.chain = new Vector<>(chain);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   186
            return sjo;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   187
        } catch (CloneNotSupportedException ex) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   188
            // this shouldn't happen, since we are Cloneable
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   189
            throw new InternalError();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   190
        }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   191
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Registers the given warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private void setWarning(RowSetWarning e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (chain == null) {
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   198
            chain = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        chain.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   202
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   203
    /**
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   204
     * readObject is called to restore the state of the {@code SerialJavaObject}
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   205
     * from a stream.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   206
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   207
    private void readObject(ObjectInputStream s)
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   208
            throws IOException, ClassNotFoundException {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   209
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   210
        ObjectInputStream.GetField fields1 = s.readFields();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   211
        @SuppressWarnings("unchecked")
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   212
        Vector<RowSetWarning> tmp = (Vector<RowSetWarning>)fields1.get("chain", null);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   213
        if (tmp != null)
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   214
            chain = new Vector<>(tmp);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   215
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   216
        obj = fields1.get("obj", null);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   217
        if (obj != null) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   218
            fields = obj.getClass().getFields();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   219
            if(hasStaticFields(fields))
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   220
                throw new IOException("Located static fields in " +
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   221
                "object instance. Cannot serialize");
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   222
        } else {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   223
            throw new IOException("Object cannot be null!");
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   224
        }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   225
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   226
    }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   227
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   228
    /**
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   229
     * writeObject is called to save the state of the {@code SerialJavaObject}
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   230
     * to a stream.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   231
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   232
    private void writeObject(ObjectOutputStream s)
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   233
            throws IOException {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   234
        ObjectOutputStream.PutField fields = s.putFields();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   235
        fields.put("obj", obj);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   236
        fields.put("chain", chain);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   237
        s.writeFields();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   238
    }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   239
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   240
    /*
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   241
     * Check to see if there are any Static Fields in this object
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   242
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   243
    private static boolean hasStaticFields(Field[] fields) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   244
        for (Field field : fields) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   245
            if ( field.getModifiers() == Modifier.STATIC) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   246
                return true;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   247
            }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   248
        }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   249
        return false;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   250
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
}