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