0N/A/*
6304N/A * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.sql.rowset.serial;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.lang.reflect.*;
0N/Aimport javax.sql.rowset.RowSetWarning;
6338N/Aimport sun.reflect.CallerSensitive;
6304N/Aimport sun.reflect.Reflection;
6338N/Aimport sun.reflect.misc.ReflectUtil;
0N/A
0N/A/**
0N/A * A serializable mapping in the Java programming language of an SQL
0N/A * <code>JAVA_OBJECT</code> value. Assuming the Java object
0N/A * implements the <code>Serializable</code> interface, this class simply wraps the
0N/A * serialization process.
0N/A * <P>
0N/A * If however, the serialization is not possible because
0N/A * the Java object is not immediately serializable, this class will
0N/A * attempt to serialize all non-static members to permit the object
0N/A * state to be serialized.
0N/A * Static or transient fields cannot be serialized; an attempt to serialize
0N/A * them will result in a <code>SerialException</code> object being thrown.
0N/A *
0N/A * @author Jonathan Bruce
0N/A */
0N/Apublic class SerialJavaObject implements Serializable, Cloneable {
0N/A
0N/A /**
0N/A * Placeholder for object to be serialized.
0N/A */
3383N/A private final Object obj;
0N/A
0N/A
0N/A /**
0N/A * Placeholder for all fields in the <code>JavaObject</code> being serialized.
0N/A */
0N/A private transient Field[] fields;
0N/A
0N/A /**
0N/A * Constructor for <code>SerialJavaObject</code> helper class.
0N/A * <p>
0N/A *
0N/A * @param obj the Java <code>Object</code> to be serialized
3383N/A * @throws SerialException if the object is found not to be serializable
0N/A */
0N/A public SerialJavaObject(Object obj) throws SerialException {
0N/A
0N/A // if any static fields are found, an exception
0N/A // should be thrown
0N/A
0N/A
0N/A // get Class. Object instance should always be available
3383N/A Class<?> c = obj.getClass();
0N/A
0N/A // determine if object implements Serializable i/f
3383N/A if (!(obj instanceof java.io.Serializable)) {
3383N/A setWarning(new RowSetWarning("Warning, the object passed to the constructor does not implement Serializable"));
0N/A }
0N/A
0N/A // can only determine public fields (obviously). If
0N/A // any of these are static, this should invalidate
0N/A // the action of attempting to persist these fields
0N/A // in a serialized form
0N/A
0N/A boolean anyStaticFields = false;
0N/A fields = c.getFields();
0N/A
0N/A for (int i = 0; i < fields.length; i++ ) {
0N/A if ( fields[i].getModifiers() == Modifier.STATIC ) {
0N/A anyStaticFields = true;
0N/A }
0N/A }
3383N/A
0N/A
0N/A if (anyStaticFields) {
0N/A throw new SerialException("Located static fields in " +
0N/A "object instance. Cannot serialize");
0N/A }
0N/A
0N/A this.obj = obj;
0N/A }
0N/A
0N/A /**
0N/A * Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code>
0N/A * object.
0N/A *
0N/A * @return a copy of this <code>SerialJavaObject</code> object as an
0N/A * <code>Object</code> in the Java programming language
0N/A * @throws SerialException if the instance is corrupt
0N/A */
0N/A public Object getObject() throws SerialException {
0N/A return this.obj;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of <code>Field</code> objects that contains each
0N/A * field of the object that this helper class is serializing.
0N/A *
0N/A * @return an array of <code>Field</code> objects
0N/A * @throws SerialException if an error is encountered accessing
0N/A * the serialized object
6304N/A * @see Class#getFields
0N/A */
6338N/A @CallerSensitive
0N/A public Field[] getFields() throws SerialException {
0N/A if (fields != null) {
3383N/A Class<?> c = this.obj.getClass();
6338N/A SecurityManager sm = System.getSecurityManager();
6338N/A if (sm != null) {
6338N/A /*
6338N/A * Check if the caller is allowed to access the specified class's package.
6338N/A * If access is denied, throw a SecurityException.
6338N/A */
6338N/A Class<?> caller = sun.reflect.Reflection.getCallerClass();
6338N/A if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
6338N/A c.getClassLoader())) {
6338N/A ReflectUtil.checkPackageAccess(c);
6338N/A }
6338N/A }
3383N/A return c.getFields();
0N/A } else {
0N/A throw new SerialException("SerialJavaObject does not contain" +
0N/A " a serialized object instance");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The identifier that assists in the serialization of this
0N/A * <code>SerialJavaObject</code> object.
0N/A */
0N/A static final long serialVersionUID = -1465795139032831023L;
0N/A
0N/A /**
0N/A * A container for the warnings issued on this <code>SerialJavaObject</code>
0N/A * object. When there are multiple warnings, each warning is chained to the
0N/A * previous warning.
0N/A */
0N/A java.util.Vector chain;
0N/A
0N/A /**
0N/A * Registers the given warning.
0N/A */
0N/A private void setWarning(RowSetWarning e) {
0N/A if (chain == null) {
0N/A chain = new java.util.Vector();
0N/A }
0N/A chain.add(e);
0N/A }
0N/A}