1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/* * @(#)SerialJavaObject.java 1.8 06/08/29 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.sql.rowset.serial; import java.sql.*; import java.io.*; import java.util.Map; import java.lang.reflect.*; import javax.sql.rowset.RowSetWarning; /** * A serializable mapping in the Java programming language of an SQL * <code>JAVA_OBJECT</code> value. Assuming the Java object * implements the <code>Serializable</code> interface, this class simply wraps the * serialization process. * <P> * If however, the serialization is not possible because * the Java object is not immediately serializable, this class will * attempt to serialize all non-static members to permit the object * state to be serialized. * Static or transient fields cannot be serialized; an attempt to serialize * them will result in a <code>SerialException</code> object being thrown. * * @version 0.1 * @author Jonathan Bruce */ public class SerialJavaObject implements Serializable, Cloneable { /** * Placeholder for object to be serialized. */ private Object obj; /** * Placeholder for all fields in the <code>JavaObject</code> being serialized. */ private transient Field[] fields; /** * Constructor for <code>SerialJavaObject</code> helper class. * <p> * * @param obj the Java <code>Object</code> to be serialized * @throws SerialException if the object is found * to be unserializable */ public SerialJavaObject(Object obj) throws SerialException { // if any static fields are found, an exception // should be thrown // get Class. Object instance should always be available Class c = obj.getClass(); // determine if object implements Serializable i/f boolean serializableImpl = false; Class[] theIf = c.getInterfaces(); for (int i = 0; i < theIf.length; i++) { String ifName = theIf[i].getName(); if (ifName == "java.io.Serializable") { serializableImpl = true; } } // can only determine public fields (obviously). If // any of these are static, this should invalidate // the action of attempting to persist these fields // in a serialized form boolean anyStaticFields = false; fields = c.getFields(); //fields = new Object[field.length]; for (int i = 0; i < fields.length; i++ ) { if ( fields[i].getModifiers() == Modifier.STATIC ) { anyStaticFields = true; } //fields[i] = field[i].get(obj); } try { if (!(serializableImpl)) { throw new RowSetWarning("Test"); } } catch (RowSetWarning w) { setWarning(w); } if (anyStaticFields) { throw new SerialException("Located static fields in " + "object instance. Cannot serialize"); } this.obj = obj; } /** * Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code> * object. * * @return a copy of this <code>SerialJavaObject</code> object as an * <code>Object</code> in the Java programming language * @throws SerialException if the instance is corrupt */ public Object getObject() throws SerialException { return this.obj; } /** * Returns an array of <code>Field</code> objects that contains each * field of the object that this helper class is serializing. * * @return an array of <code>Field</code> objects * @throws SerialException if an error is encountered accessing * the serialized object */ public Field[] getFields() throws SerialException { if (fields != null) { Class c = this.obj.getClass(); //the following has to be commented before mustang integration //return c.getFields(); //the following has to be uncommented before mustang integration return sun.reflect.misc.FieldUtil.getFields(c); } else { throw new SerialException("SerialJavaObject does not contain" + " a serialized object instance"); } } /** * The identifier that assists in the serialization of this * <code>SerialJavaObject</code> object. */ static final long serialVersionUID = -1465795139032831023L; /** * A container for the warnings issued on this <code>SerialJavaObject</code> * object. When there are multiple warnings, each warning is chained to the * previous warning. */ java.util.Vector chain; /** * Registers the given warning. */ private void setWarning(RowSetWarning e) { if (chain == null) { chain = new java.util.Vector(); } chain.add(e); } }