jdk/src/share/classes/javax/sql/rowset/serial/SerialJavaObject.java
author lancea
Mon, 10 Jan 2011 14:43:50 -0500
changeset 7972 1d0e051daa24
parent 5506 202f599c92aa
child 11129 f9ad1aadf3fa
permissions -rw-r--r--
6544224: Remove the need of sun.reflect.misc Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.sql.rowset.RowSetWarning;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A serializable mapping in the Java programming language of an SQL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <code>JAVA_OBJECT</code> value. Assuming the Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * implements the <code>Serializable</code> interface, this class simply wraps the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * serialization process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * If however, the serialization is not possible because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the Java object is not immediately serializable, this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * attempt to serialize all non-static members to permit the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * state to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Static or transient fields cannot be serialized; an attempt to serialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * them will result in a <code>SerialException</code> object being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Jonathan Bruce
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class SerialJavaObject implements Serializable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Placeholder for object to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    52
    private final Object obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    * Placeholder for all fields in the <code>JavaObject</code> being serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private transient Field[] fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Constructor for <code>SerialJavaObject</code> helper class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @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
    65
     * @throws SerialException if the object is found not to be serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public SerialJavaObject(Object obj) throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // if any static fields are found, an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // should be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // get Class. Object instance should always be available
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    74
        Class<?> c = obj.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // determine if object implements Serializable i/f
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    77
        if (!(obj instanceof java.io.Serializable)) {
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    78
            setWarning(new RowSetWarning("Warning, the object passed to the constructor does not implement Serializable"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // can only determine public fields (obviously). If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // any of these are static, this should invalidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // the action of attempting to persist these fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // in a serialized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        boolean anyStaticFields = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        fields = c.getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (int i = 0; i < fields.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if ( fields[i].getModifiers() == Modifier.STATIC ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                anyStaticFields = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (anyStaticFields) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Field[] getFields() throws SerialException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (fields != null) {
7972
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
   126
            Class<?> c = this.obj.getClass();
1d0e051daa24 6544224: Remove the need of sun.reflect.misc
lancea
parents: 5506
diff changeset
   127
            return c.getFields();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new SerialException("SerialJavaObject does not contain" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                " a serialized object instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * The identifier that assists in the serialization of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <code>SerialJavaObject</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static final long serialVersionUID = -1465795139032831023L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * A container for the warnings issued on this <code>SerialJavaObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * object. When there are multiple warnings, each warning is chained to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * previous warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    java.util.Vector chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Registers the given warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private void setWarning(RowSetWarning e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (chain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            chain = new java.util.Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        chain.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}