jdk/src/share/classes/javax/sql/rowset/serial/SerialJavaObject.java
author lancea
Thu, 28 Mar 2013 06:55:42 -0400
changeset 18211 74aeb4741e3d
parent 14781 701d0765f75f
child 18235 5cef881c0a28
permissions -rw-r--r--
8009554: Improve SerialJavaObject.getFields Reviewed-by: alanb, skoivu, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18211
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
     2
 * Copyright (c) 2003, 2013, 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;
18211
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
    33
import sun.reflect.Reflection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A serializable mapping in the Java programming language of an SQL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>JAVA_OBJECT</code> value. Assuming the Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * implements the <code>Serializable</code> interface, this class simply wraps the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * serialization process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * If however, the serialization is not possible because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the Java object is not immediately serializable, this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * attempt to serialize all non-static members to permit the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * state to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Static or transient fields cannot be serialized; an attempt to serialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * them will result in a <code>SerialException</code> object being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
14781
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14409
diff changeset
    48
 * <h4> Thread safety </h4>
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14409
diff changeset
    49
 *
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14409
diff changeset
    50
 * A SerialJavaObject is not safe for use by multiple concurrent threads.  If a
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14409
diff changeset
    51
 * SerialJavaObject is to be used by more than one thread then access to the
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14409
diff changeset
    52
 * SerialJavaObject should be controlled by appropriate synchronization.
701d0765f75f 8004357: Implement various methods in SerialBlob/Clob/Array and specify Thread Safety
lancea
parents: 14409
diff changeset
    53
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Jonathan Bruce
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class SerialJavaObject implements Serializable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Placeholder for object to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
    61
    private Object obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    * Placeholder for all fields in the <code>JavaObject</code> being serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private transient Field[] fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Constructor for <code>SerialJavaObject</code> helper class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @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
    74
     * @throws SerialException if the object is found not to be serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public SerialJavaObject(Object obj) throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // if any static fields are found, an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // should be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // get Class. Object instance should always be available
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    83
        Class<?> c = obj.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // determine if object implements Serializable i/f
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    86
        if (!(obj instanceof java.io.Serializable)) {
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    87
            setWarning(new RowSetWarning("Warning, the object passed to the constructor does not implement Serializable"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // can only determine public fields (obviously). If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // any of these are static, this should invalidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // the action of attempting to persist these fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // in a serialized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        fields = c.getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
    96
        if (hasStaticFields(fields)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new SerialException("Located static fields in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                "object instance. Cannot serialize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.obj = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return a copy of this <code>SerialJavaObject</code> object as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *         <code>Object</code> in the Java programming language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @throws SerialException if the instance is corrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public Object getObject() throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return this.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns an array of <code>Field</code> objects that contains each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * field of the object that this helper class is serializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return an array of <code>Field</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws SerialException if an error is encountered accessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * the serialized object
18211
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   123
     * @throws  SecurityException  If a security manager, <i>s</i>, is present
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   124
     * and the caller's class loader is not the same as or an
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   125
     * ancestor of the class loader for the class of the
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   126
     * {@linkplain #getObject object} being serialized
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   127
     * and invocation of {@link SecurityManager#checkPackageAccess
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   128
     * s.checkPackageAccess()} denies access to the package
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   129
     * of that class.
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   130
     * @see Class#getFields
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public Field[] getFields() throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (fields != null) {
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
   134
            Class<?> c = this.obj.getClass();
18211
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   135
            checkPackageAccess(c);
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
   136
            return c.getFields();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new SerialException("SerialJavaObject does not contain" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                " a serialized object instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   144
     * The identifier that assists in the serialization of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <code>SerialJavaObject</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static final long serialVersionUID = -1465795139032831023L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * A container for the warnings issued on this <code>SerialJavaObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * object. When there are multiple warnings, each warning is chained to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * previous warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   154
    Vector<RowSetWarning> chain;
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
    /**
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   157
     * Compares this SerialJavaObject to the specified object.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   158
     * 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
   159
     * is not {@code null} and is a {@code SerialJavaObject}
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   160
     * object that is identical to this object
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
     * @param  o The object to compare this {@code SerialJavaObject} against
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
     * @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
   165
     *          equivalent to this SerialJavaObject, {@code false} otherwise
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   166
     *
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
    public boolean equals(Object o) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   169
        if (this == o) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   170
            return true;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   171
        }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   172
        if (o instanceof SerialJavaObject) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   173
            SerialJavaObject sjo = (SerialJavaObject) o;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   174
            return obj.equals(sjo.obj);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   175
        }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   176
        return false;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   177
    }
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
     * 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
   181
     * {@code SerialJavaObject} object is taken as the hash code of
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   182
     * the {@code Object} it stores
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   183
     *
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   184
     * @return  a hash code value for this object.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   185
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   186
    public int hashCode() {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   187
        return 31 + obj.hashCode();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   188
    }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   189
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
     * Returns a clone of this {@code SerialJavaObject}.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   192
     *
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   193
     * @return  a clone of this SerialJavaObject
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   194
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   195
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   196
    public Object clone() {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   197
        try {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   198
            SerialJavaObject sjo = (SerialJavaObject) super.clone();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   199
            sjo.fields = Arrays.copyOf(fields, fields.length);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   200
            if (chain != null)
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   201
                sjo.chain = new Vector<>(chain);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   202
            return sjo;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   203
        } catch (CloneNotSupportedException ex) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   204
            // this shouldn't happen, since we are Cloneable
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   205
            throw new InternalError();
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
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Registers the given warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private void setWarning(RowSetWarning e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (chain == null) {
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   214
            chain = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        chain.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
14409
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   218
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   219
    /**
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   220
     * 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
   221
     * from a stream.
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   222
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   223
    private void readObject(ObjectInputStream s)
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   224
            throws IOException, ClassNotFoundException {
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
        ObjectInputStream.GetField fields1 = s.readFields();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   227
        @SuppressWarnings("unchecked")
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   228
        Vector<RowSetWarning> tmp = (Vector<RowSetWarning>)fields1.get("chain", null);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   229
        if (tmp != null)
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   230
            chain = new Vector<>(tmp);
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
        obj = fields1.get("obj", null);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   233
        if (obj != null) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   234
            fields = obj.getClass().getFields();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   235
            if(hasStaticFields(fields))
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   236
                throw new IOException("Located static fields in " +
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   237
                "object instance. Cannot serialize");
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   238
        } else {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   239
            throw new IOException("Object cannot be null!");
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
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
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   244
    /**
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   245
     * 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
   246
     * to a stream.
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
    private void writeObject(ObjectOutputStream s)
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   249
            throws IOException {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   250
        ObjectOutputStream.PutField fields = s.putFields();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   251
        fields.put("obj", obj);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   252
        fields.put("chain", chain);
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   253
        s.writeFields();
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   254
    }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   255
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   256
    /*
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   257
     * 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
   258
     */
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   259
    private static boolean hasStaticFields(Field[] fields) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   260
        for (Field field : fields) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   261
            if ( field.getModifiers() == Modifier.STATIC) {
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   262
                return true;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   263
            }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   264
        }
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   265
        return false;
d879c92507ec 8002212: adding read/writeObject to additional SerialXXX classes
lancea
parents: 11129
diff changeset
   266
    }
18211
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   267
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   268
    /*
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   269
     * Check if the caller is allowed to access the specified class's package.  If access is denied,
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   270
     * throw a SecurityException.
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   271
     *
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   272
     */
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   273
    private void checkPackageAccess(Class<?> clz) {
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   274
        SecurityManager s = System.getSecurityManager();
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   275
        if (s != null) {
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   276
            if (sun.reflect.misc.ReflectUtil.needsPackageAccessCheck(
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   277
                    getCallerClassLoader(), clz.getClassLoader())) {
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   278
                String name = clz.getName();
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   279
                int i = name.lastIndexOf('.');
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   280
                if (i != -1) {
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   281
                    s.checkPackageAccess(name.substring(0, i));
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   282
                }
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   283
            }
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   284
        }
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   285
    }
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   286
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   287
    /* Internal method used to get the caller's caller class loader.
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   288
     * Caution is required if you attempt to make changes as this method assumes
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   289
     * the following stack frame count:
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   290
     * 0: Reflection
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   291
     * 1: getCallerClassLoader
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   292
     * 2: checkPackageAccess
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   293
     * 3: getFields
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   294
     * 4: caller of getFields
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   295
     */
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   296
    private static ClassLoader getCallerClassLoader() {
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   297
        Class<?> cc = Reflection.getCallerClass(4);
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   298
        ClassLoader cl = (cc != null) ? cc.getClassLoader() : null;
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   299
        return cl;
74aeb4741e3d 8009554: Improve SerialJavaObject.getFields
lancea
parents: 14781
diff changeset
   300
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
}