Source Code Cross Referenced for SerialVersionUID.java in  » Byte-Code » Javassist » javassist » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Byte Code » Javassist » javassist 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Javassist, a Java-bytecode translator toolkit.
003:         * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004:         *
005:         * The contents of this file are subject to the Mozilla Public License Version
006:         * 1.1 (the "License"); you may not use this file except in compliance with
007:         * the License.  Alternatively, the contents of this file may be used under
008:         * the terms of the GNU Lesser General Public License Version 2.1 or later.
009:         *
010:         * Software distributed under the License is distributed on an "AS IS" basis,
011:         * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012:         * for the specific language governing rights and limitations under the
013:         * License.
014:         */
015:
016:        package javassist;
017:
018:        import java.io.*;
019:        import javassist.bytecode.*;
020:        import java.util.*;
021:        import java.security.*;
022:
023:        /**
024:         * Utility for calculating serialVersionUIDs for Serializable classes.
025:         *
026:         * @author Bob Lee (crazybob@crazybob.org)
027:         * @author modified by Shigeru Chiba
028:         */
029:        public class SerialVersionUID {
030:
031:            /**
032:             * Adds serialVersionUID if one does not already exist. Call this before
033:             * modifying a class to maintain serialization compatability.
034:             */
035:            public static void setSerialVersionUID(CtClass clazz)
036:                    throws CannotCompileException, NotFoundException {
037:                // check for pre-existing field.
038:                try {
039:                    clazz.getDeclaredField("serialVersionUID");
040:                    return;
041:                } catch (NotFoundException e) {
042:                }
043:
044:                // check if the class is serializable.
045:                if (!isSerializable(clazz))
046:                    return;
047:
048:                // add field with default value.
049:                CtField field = new CtField(CtClass.longType,
050:                        "serialVersionUID", clazz);
051:                field.setModifiers(Modifier.PRIVATE | Modifier.STATIC
052:                        | Modifier.FINAL);
053:                clazz.addField(field, calculateDefault(clazz) + "L");
054:            }
055:
056:            /**
057:             * Does the class implement Serializable?
058:             */
059:            private static boolean isSerializable(CtClass clazz)
060:                    throws NotFoundException {
061:                ClassPool pool = clazz.getClassPool();
062:                return clazz.subtypeOf(pool.get("java.io.Serializable"));
063:            }
064:
065:            /**
066:             * Calculate default value. See Java Serialization Specification, Stream
067:             * Unique Identifiers.
068:             */
069:            static long calculateDefault(CtClass clazz)
070:                    throws CannotCompileException {
071:                try {
072:                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
073:                    DataOutputStream out = new DataOutputStream(bout);
074:                    ClassFile classFile = clazz.getClassFile();
075:
076:                    // class name.
077:                    String javaName = javaName(clazz);
078:                    out.writeUTF(javaName);
079:
080:                    // class modifiers.
081:                    out.writeInt(clazz.getModifiers()
082:                            & (Modifier.PUBLIC | Modifier.FINAL
083:                                    | Modifier.INTERFACE | Modifier.ABSTRACT));
084:
085:                    // interfaces.
086:                    String[] interfaces = classFile.getInterfaces();
087:                    for (int i = 0; i < interfaces.length; i++)
088:                        interfaces[i] = javaName(interfaces[i]);
089:
090:                    Arrays.sort(interfaces);
091:                    for (int i = 0; i < interfaces.length; i++)
092:                        out.writeUTF(interfaces[i]);
093:
094:                    // fields.
095:                    CtField[] fields = clazz.getDeclaredFields();
096:                    Arrays.sort(fields, new Comparator() {
097:                        public int compare(Object o1, Object o2) {
098:                            CtField field1 = (CtField) o1;
099:                            CtField field2 = (CtField) o2;
100:                            return field1.getName().compareTo(field2.getName());
101:                        }
102:                    });
103:
104:                    for (int i = 0; i < fields.length; i++) {
105:                        CtField field = (CtField) fields[i];
106:                        int mods = field.getModifiers();
107:                        if (((mods & Modifier.PRIVATE) == 0)
108:                                || ((mods & (Modifier.STATIC | Modifier.TRANSIENT)) == 0)) {
109:                            out.writeUTF(field.getName());
110:                            out.writeInt(mods);
111:                            out.writeUTF(field.getFieldInfo2().getDescriptor());
112:                        }
113:                    }
114:
115:                    // static initializer.
116:                    if (classFile.getStaticInitializer() != null) {
117:                        out.writeUTF("<clinit>");
118:                        out.writeInt(Modifier.STATIC);
119:                        out.writeUTF("()V");
120:                    }
121:
122:                    // constructors.
123:                    CtConstructor[] constructors = clazz
124:                            .getDeclaredConstructors();
125:                    Arrays.sort(constructors, new Comparator() {
126:                        public int compare(Object o1, Object o2) {
127:                            CtConstructor c1 = (CtConstructor) o1;
128:                            CtConstructor c2 = (CtConstructor) o2;
129:                            return c1
130:                                    .getMethodInfo2()
131:                                    .getDescriptor()
132:                                    .compareTo(
133:                                            c2.getMethodInfo2().getDescriptor());
134:                        }
135:                    });
136:
137:                    for (int i = 0; i < constructors.length; i++) {
138:                        CtConstructor constructor = constructors[i];
139:                        int mods = constructor.getModifiers();
140:                        if ((mods & Modifier.PRIVATE) == 0) {
141:                            out.writeUTF("<init>");
142:                            out.writeInt(mods);
143:                            out.writeUTF(constructor.getMethodInfo2()
144:                                    .getDescriptor().replace('/', '.'));
145:                        }
146:                    }
147:
148:                    // methods.
149:                    CtMethod[] methods = clazz.getDeclaredMethods();
150:                    Arrays.sort(methods, new Comparator() {
151:                        public int compare(Object o1, Object o2) {
152:                            CtMethod m1 = (CtMethod) o1;
153:                            CtMethod m2 = (CtMethod) o2;
154:                            int value = m1.getName().compareTo(m2.getName());
155:                            if (value == 0)
156:                                value = m1.getMethodInfo2().getDescriptor()
157:                                        .compareTo(
158:                                                m2.getMethodInfo2()
159:                                                        .getDescriptor());
160:
161:                            return value;
162:                        }
163:                    });
164:
165:                    for (int i = 0; i < methods.length; i++) {
166:                        CtMethod method = methods[i];
167:                        int mods = method.getModifiers();
168:                        if ((mods & Modifier.PRIVATE) == 0) {
169:                            out.writeUTF(method.getName());
170:                            out.writeInt(mods);
171:                            out.writeUTF(method.getMethodInfo2()
172:                                    .getDescriptor().replace('/', '.'));
173:                        }
174:                    }
175:
176:                    // calculate hash.
177:                    out.flush();
178:                    MessageDigest digest = MessageDigest.getInstance("SHA");
179:                    byte[] digested = digest.digest(bout.toByteArray());
180:                    long hash = 0;
181:                    for (int i = Math.min(digested.length, 8) - 1; i >= 0; i--)
182:                        hash = (hash << 8) | (digested[i] & 0xFF);
183:
184:                    return hash;
185:                } catch (IOException e) {
186:                    throw new CannotCompileException(e);
187:                } catch (NoSuchAlgorithmException e) {
188:                    throw new CannotCompileException(e);
189:                }
190:            }
191:
192:            private static String javaName(CtClass clazz) {
193:                return Descriptor.toJavaName(Descriptor.toJvmName(clazz));
194:            }
195:
196:            private static String javaName(String name) {
197:                return Descriptor.toJavaName(Descriptor.toJvmName(name));
198:            }
199:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.