/JDK1.7-Java SE Development Kit 7u80/src/javax/sql/rowset/serial/SerialJavaObject.java

https://github.com/zxiaofan/JDK · Java · 173 lines · 58 code · 22 blank · 93 comment · 13 complexity · a389c7b15da7c4eecbf9a67e353e6537 MD5 · raw file

  1. /*
  2. * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  3. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. *
  5. *
  6. *
  7. *
  8. *
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *
  15. *
  16. *
  17. *
  18. *
  19. *
  20. *
  21. *
  22. *
  23. *
  24. */
  25. package javax.sql.rowset.serial;
  26. import java.io.*;
  27. import java.lang.reflect.*;
  28. import javax.sql.rowset.RowSetWarning;
  29. import sun.reflect.CallerSensitive;
  30. import sun.reflect.Reflection;
  31. import sun.reflect.misc.ReflectUtil;
  32. /**
  33. * A serializable mapping in the Java programming language of an SQL
  34. * <code>JAVA_OBJECT</code> value. Assuming the Java object
  35. * implements the <code>Serializable</code> interface, this class simply wraps the
  36. * serialization process.
  37. * <P>
  38. * If however, the serialization is not possible because
  39. * the Java object is not immediately serializable, this class will
  40. * attempt to serialize all non-static members to permit the object
  41. * state to be serialized.
  42. * Static or transient fields cannot be serialized; an attempt to serialize
  43. * them will result in a <code>SerialException</code> object being thrown.
  44. *
  45. * @author Jonathan Bruce
  46. */
  47. public class SerialJavaObject implements Serializable, Cloneable {
  48. /**
  49. * Placeholder for object to be serialized.
  50. */
  51. private final Object obj;
  52. /**
  53. * Placeholder for all fields in the <code>JavaObject</code> being serialized.
  54. */
  55. private transient Field[] fields;
  56. /**
  57. * Constructor for <code>SerialJavaObject</code> helper class.
  58. * <p>
  59. *
  60. * @param obj the Java <code>Object</code> to be serialized
  61. * @throws SerialException if the object is found not to be serializable
  62. */
  63. public SerialJavaObject(Object obj) throws SerialException {
  64. // if any static fields are found, an exception
  65. // should be thrown
  66. // get Class. Object instance should always be available
  67. Class<?> c = obj.getClass();
  68. // determine if object implements Serializable i/f
  69. if (!(obj instanceof java.io.Serializable)) {
  70. setWarning(new RowSetWarning("Warning, the object passed to the constructor does not implement Serializable"));
  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. boolean anyStaticFields = false;
  77. fields = c.getFields();
  78. for (int i = 0; i < fields.length; i++ ) {
  79. if ( fields[i].getModifiers() == Modifier.STATIC ) {
  80. anyStaticFields = true;
  81. }
  82. }
  83. if (anyStaticFields) {
  84. throw new SerialException("Located static fields in " +
  85. "object instance. Cannot serialize");
  86. }
  87. this.obj = obj;
  88. }
  89. /**
  90. * Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code>
  91. * object.
  92. *
  93. * @return a copy of this <code>SerialJavaObject</code> object as an
  94. * <code>Object</code> in the Java programming language
  95. * @throws SerialException if the instance is corrupt
  96. */
  97. public Object getObject() throws SerialException {
  98. return this.obj;
  99. }
  100. /**
  101. * Returns an array of <code>Field</code> objects that contains each
  102. * field of the object that this helper class is serializing.
  103. *
  104. * @return an array of <code>Field</code> objects
  105. * @throws SerialException if an error is encountered accessing
  106. * the serialized object
  107. * @see Class#getFields
  108. */
  109. @CallerSensitive
  110. public Field[] getFields() throws SerialException {
  111. if (fields != null) {
  112. Class<?> c = this.obj.getClass();
  113. SecurityManager sm = System.getSecurityManager();
  114. if (sm != null) {
  115. /*
  116. * Check if the caller is allowed to access the specified class's package.
  117. * If access is denied, throw a SecurityException.
  118. */
  119. Class<?> caller = sun.reflect.Reflection.getCallerClass();
  120. if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
  121. c.getClassLoader())) {
  122. ReflectUtil.checkPackageAccess(c);
  123. }
  124. }
  125. return c.getFields();
  126. } else {
  127. throw new SerialException("SerialJavaObject does not contain" +
  128. " a serialized object instance");
  129. }
  130. }
  131. /**
  132. * The identifier that assists in the serialization of this
  133. * <code>SerialJavaObject</code> object.
  134. */
  135. static final long serialVersionUID = -1465795139032831023L;
  136. /**
  137. * A container for the warnings issued on this <code>SerialJavaObject</code>
  138. * object. When there are multiple warnings, each warning is chained to the
  139. * previous warning.
  140. */
  141. java.util.Vector chain;
  142. /**
  143. * Registers the given warning.
  144. */
  145. private void setWarning(RowSetWarning e) {
  146. if (chain == null) {
  147. chain = new java.util.Vector();
  148. }
  149. chain.add(e);
  150. }
  151. }