KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > sql > rowset > serial > SerialJavaObject


1 /*
2  * @(#)SerialJavaObject.java 1.7 05/10/31
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.sql.rowset.serial;
9
10 import java.sql.*;
11 import java.io.*;
12 import java.util.Map JavaDoc;
13 import java.lang.reflect.*;
14 import javax.sql.rowset.RowSetWarning JavaDoc;
15
16 /**
17  * A serializable mapping in the Java programming language of an SQL
18  * <code>JAVA_OBJECT</code> value. Assuming the Java object
19  * implements the <code>Serializable</code> interface, this class simply wraps the
20  * serialization process.
21  * <P>
22  * If however, the serialization is not possible because
23  * the Java object is not immediately serializable, this class will
24  * attempt to serialize all non-static members to permit the object
25  * state to be serialized.
26  * Static or transient fields cannot be serialized; an attempt to serialize
27  * them will result in a <code>SerialException</code> object being thrown.
28  *
29  * @version 0.1
30  * @author Jonathan Bruce
31  */

32 public class SerialJavaObject implements Serializable, Cloneable JavaDoc {
33
34     /**
35      * Placeholder for object to be serialized.
36      */

37     private Object JavaDoc obj;
38
39
40    /**
41     * Placeholder for all fields in the <code>JavaObject</code> being serialized.
42     */

43     private transient Field[] fields;
44
45     /**
46      * Constructor for <code>SerialJavaObject</code> helper class.
47      * <p>
48      *
49      * @param obj the Java <code>Object</code> to be serialized
50      * @throws SerialException if the object is found
51      * to be unserializable
52      */

53     public SerialJavaObject(Object JavaDoc obj) throws SerialException JavaDoc {
54
55     // if any static fields are found, an exception
56
// should be thrown
57

58
59     // get Class. Object instance should always be available
60
Class JavaDoc c = obj.getClass();
61
62     // determine if object implements Serializable i/f
63
boolean serializableImpl = false;
64     Class JavaDoc[] theIf = c.getInterfaces();
65     for (int i = 0; i < theIf.length; i++) {
66         String JavaDoc ifName = theIf[i].getName();
67         if (ifName == "java.io.Serializable") {
68         serializableImpl = true;
69         }
70     }
71
72     // can only determine public fields (obviously). If
73
// any of these are static, this should invalidate
74
// the action of attempting to persist these fields
75
// in a serialized form
76

77     boolean anyStaticFields = false;
78     fields = c.getFields();
79         //fields = new Object[field.length];
80

81     for (int i = 0; i < fields.length; i++ ) {
82         if ( fields[i].getModifiers() == Modifier.STATIC ) {
83         anyStaticFields = true;
84         }
85             //fields[i] = field[i].get(obj);
86
}
87         try {
88             if (!(serializableImpl)) {
89                throw new RowSetWarning JavaDoc("Test");
90             }
91         } catch (RowSetWarning JavaDoc w) {
92             setWarning(w);
93         }
94         
95     if (anyStaticFields) {
96         throw new SerialException JavaDoc("Located static fields in " +
97         "object instance. Cannot serialize");
98     }
99
100     this.obj = obj;
101     }
102
103     /**
104      * Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code>
105      * object.
106      *
107      * @return a copy of this <code>SerialJavaObject</code> object as an
108      * <code>Object</code> in the Java programming language
109      * @throws SerialException if the instance is corrupt
110      */

111     public Object JavaDoc getObject() throws SerialException JavaDoc {
112         return this.obj;
113     }
114
115     /**
116      * Returns an array of <code>Field</code> objects that contains each
117      * field of the object that this helper class is serializing.
118      *
119      * @return an array of <code>Field</code> objects
120      * @throws SerialException if an error is encountered accessing
121      * the serialized object
122      */

123     public Field[] getFields() throws SerialException JavaDoc {
124     if (fields != null) {
125             Class JavaDoc c = this.obj.getClass();
126         return sun.reflect.misc.FieldUtil.getFields(c);
127     } else {
128         throw new SerialException JavaDoc("SerialJavaObject does not contain" +
129         " a serialized object instance");
130     }
131     }
132     
133     /**
134      * The identifier that assists in the serialization of this
135      * <code>SerialJavaObject</code> object.
136      */

137     static final long serialVersionUID = -1465795139032831023L;
138     
139     /**
140      * A container for the warnings issued on this <code>SerialJavaObject</code>
141      * object. When there are multiple warnings, each warning is chained to the
142      * previous warning.
143      */

144     java.util.Vector JavaDoc chain;
145     
146     /**
147      * Registers the given warning.
148      */

149     private void setWarning(RowSetWarning JavaDoc e) {
150         if (chain == null) {
151             chain = new java.util.Vector JavaDoc();
152         }
153         chain.add(e);
154     }
155 }
156
Popular Tags