You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerialVersionUID.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist;
  17. import java.io.*;
  18. import java.lang.reflect.Modifier;
  19. import javassist.bytecode.*;
  20. import java.util.*;
  21. import java.security.*;
  22. /**
  23. * Utility for calculating serialVersionUIDs for Serializable classes.
  24. *
  25. * @author Bob Lee (crazybob@crazybob.org)
  26. * @author modified by Shigeru Chiba
  27. */
  28. public class SerialVersionUID {
  29. /**
  30. * Adds serialVersionUID if one does not already exist. Call this before
  31. * modifying a class to maintain serialization compatability.
  32. */
  33. public static void setSerialVersionUID(CtClass clazz)
  34. throws CannotCompileException, NotFoundException
  35. {
  36. // check for pre-existing field.
  37. try {
  38. clazz.getDeclaredField("serialVersionUID");
  39. return;
  40. }
  41. catch (NotFoundException e) {}
  42. // check if the class is serializable.
  43. if (!isSerializable(clazz))
  44. return;
  45. // add field with default value.
  46. CtField field = new CtField(CtClass.longType, "serialVersionUID",
  47. clazz);
  48. field.setModifiers(Modifier.PRIVATE | Modifier.STATIC |
  49. Modifier.FINAL);
  50. clazz.addField(field, calculateDefault(clazz) + "L");
  51. }
  52. /**
  53. * Does the class implement Serializable?
  54. */
  55. private static boolean isSerializable(CtClass clazz)
  56. throws NotFoundException
  57. {
  58. ClassPool pool = clazz.getClassPool();
  59. return clazz.subtypeOf(pool.get("java.io.Serializable"));
  60. }
  61. /**
  62. * Calculate default value. See Java Serialization Specification, Stream
  63. * Unique Identifiers.
  64. */
  65. static long calculateDefault(CtClass clazz)
  66. throws CannotCompileException
  67. {
  68. try {
  69. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  70. DataOutputStream out = new DataOutputStream(bout);
  71. ClassFile classFile = clazz.getClassFile();
  72. // class name.
  73. String javaName = javaName(clazz);
  74. out.writeUTF(javaName);
  75. CtMethod[] methods = clazz.getDeclaredMethods();
  76. // class modifiers.
  77. int classMods = clazz.getModifiers();
  78. if ((classMods & Modifier.INTERFACE) != 0)
  79. if (methods.length > 0)
  80. classMods = classMods | Modifier.ABSTRACT;
  81. else
  82. classMods = classMods & ~Modifier.ABSTRACT;
  83. out.writeInt(classMods);
  84. // interfaces.
  85. String[] interfaces = classFile.getInterfaces();
  86. for (int i = 0; i < interfaces.length; i++)
  87. interfaces[i] = javaName(interfaces[i]);
  88. Arrays.sort(interfaces);
  89. for (int i = 0; i < interfaces.length; i++)
  90. out.writeUTF(interfaces[i]);
  91. // fields.
  92. CtField[] fields = clazz.getDeclaredFields();
  93. Arrays.sort(fields, new Comparator() {
  94. public int compare(Object o1, Object o2) {
  95. CtField field1 = (CtField)o1;
  96. CtField field2 = (CtField)o2;
  97. return field1.getName().compareTo(field2.getName());
  98. }
  99. });
  100. for (int i = 0; i < fields.length; i++) {
  101. CtField field = (CtField) fields[i];
  102. int mods = field.getModifiers();
  103. if (((mods & Modifier.PRIVATE) == 0) ||
  104. ((mods & (Modifier.STATIC | Modifier.TRANSIENT)) == 0)) {
  105. out.writeUTF(field.getName());
  106. out.writeInt(mods);
  107. out.writeUTF(field.getFieldInfo2().getDescriptor());
  108. }
  109. }
  110. // static initializer.
  111. if (classFile.getStaticInitializer() != null) {
  112. out.writeUTF("<clinit>");
  113. out.writeInt(Modifier.STATIC);
  114. out.writeUTF("()V");
  115. }
  116. // constructors.
  117. CtConstructor[] constructors = clazz.getDeclaredConstructors();
  118. Arrays.sort(constructors, new Comparator() {
  119. public int compare(Object o1, Object o2) {
  120. CtConstructor c1 = (CtConstructor)o1;
  121. CtConstructor c2 = (CtConstructor)o2;
  122. return c1.getMethodInfo2().getDescriptor().compareTo(
  123. c2.getMethodInfo2().getDescriptor());
  124. }
  125. });
  126. for (int i = 0; i < constructors.length; i++) {
  127. CtConstructor constructor = constructors[i];
  128. int mods = constructor.getModifiers();
  129. if ((mods & Modifier.PRIVATE) == 0) {
  130. out.writeUTF("<init>");
  131. out.writeInt(mods);
  132. out.writeUTF(constructor.getMethodInfo2()
  133. .getDescriptor().replace('/', '.'));
  134. }
  135. }
  136. // methods.
  137. Arrays.sort(methods, new Comparator() {
  138. public int compare(Object o1, Object o2) {
  139. CtMethod m1 = (CtMethod)o1;
  140. CtMethod m2 = (CtMethod)o2;
  141. int value = m1.getName().compareTo(m2.getName());
  142. if (value == 0)
  143. value = m1.getMethodInfo2().getDescriptor()
  144. .compareTo(m2.getMethodInfo2().getDescriptor());
  145. return value;
  146. }
  147. });
  148. for (int i = 0; i < methods.length; i++) {
  149. CtMethod method = methods[i];
  150. int mods = method.getModifiers()
  151. & (Modifier.PUBLIC | Modifier.PRIVATE
  152. | Modifier.PROTECTED | Modifier.STATIC
  153. | Modifier.FINAL | Modifier.SYNCHRONIZED
  154. | Modifier.NATIVE | Modifier.ABSTRACT | Modifier.STRICT);
  155. if ((mods & Modifier.PRIVATE) == 0) {
  156. out.writeUTF(method.getName());
  157. out.writeInt(mods);
  158. out.writeUTF(method.getMethodInfo2()
  159. .getDescriptor().replace('/', '.'));
  160. }
  161. }
  162. // calculate hash.
  163. out.flush();
  164. MessageDigest digest = MessageDigest.getInstance("SHA");
  165. byte[] digested = digest.digest(bout.toByteArray());
  166. long hash = 0;
  167. for (int i = Math.min(digested.length, 8) - 1; i >= 0; i--)
  168. hash = (hash << 8) | (digested[i] & 0xFF);
  169. return hash;
  170. }
  171. catch (IOException e) {
  172. throw new CannotCompileException(e);
  173. }
  174. catch (NoSuchAlgorithmException e) {
  175. throw new CannotCompileException(e);
  176. }
  177. }
  178. private static String javaName(CtClass clazz) {
  179. return Descriptor.toJavaName(Descriptor.toJvmName(clazz));
  180. }
  181. private static String javaName(String name) {
  182. return Descriptor.toJavaName(Descriptor.toJvmName(name));
  183. }
  184. }