rlm@10: /*** rlm@10: * ASM: a very small and fast Java bytecode manipulation framework rlm@10: * Copyright (c) 2000-2005 INRIA, France Telecom rlm@10: * All rights reserved. rlm@10: * rlm@10: * Redistribution and use in source and binary forms, with or without rlm@10: * modification, are permitted provided that the following conditions rlm@10: * are met: rlm@10: * 1. Redistributions of source code must retain the above copyright rlm@10: * notice, this list of conditions and the following disclaimer. rlm@10: * 2. Redistributions in binary form must reproduce the above copyright rlm@10: * notice, this list of conditions and the following disclaimer in the rlm@10: * documentation and/or other materials provided with the distribution. rlm@10: * 3. Neither the name of the copyright holders nor the names of its rlm@10: * contributors may be used to endorse or promote products derived from rlm@10: * this software without specific prior written permission. rlm@10: * rlm@10: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" rlm@10: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE rlm@10: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE rlm@10: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE rlm@10: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR rlm@10: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF rlm@10: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS rlm@10: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN rlm@10: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) rlm@10: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF rlm@10: * THE POSSIBILITY OF SUCH DAMAGE. rlm@10: */ rlm@10: package clojure.asm.commons; rlm@10: rlm@10: import java.io.ByteArrayOutputStream; rlm@10: import java.io.DataOutputStream; rlm@10: import java.io.IOException; rlm@10: import java.security.MessageDigest; rlm@10: import java.util.ArrayList; rlm@10: import java.util.Arrays; rlm@10: import java.util.Collection; rlm@10: rlm@10: import clojure.asm.ClassAdapter; rlm@10: import clojure.asm.ClassVisitor; rlm@10: import clojure.asm.FieldVisitor; rlm@10: import clojure.asm.MethodVisitor; rlm@10: import clojure.asm.Opcodes; rlm@10: rlm@10: /** rlm@10: * A {@link ClassAdapter} that adds a serial version unique identifier to a rlm@10: * class if missing. Here is typical usage of this class: rlm@10: *

rlm@10: *

rlm@10:  *   ClassWriter cw = new ClassWriter(...);
rlm@10:  *   ClassVisitor sv = new SerialVersionUIDAdder(cw);
rlm@10:  *   ClassVisitor ca = new MyClassAdapter(sv);
rlm@10:  *   new ClassReader(orginalClass).accept(ca, false);
rlm@10:  * 
rlm@10: *

rlm@10: * The SVUID algorithm can be found http://java.sun.com/j2se/1.4.2/docs/guide/serialization/spec/class.html: rlm@10: *

rlm@10: *

rlm@10:  * The serialVersionUID is computed using the signature of a stream of bytes
rlm@10:  * that reflect the class definition. The National Institute of Standards and
rlm@10:  * Technology (NIST) Secure Hash Algorithm (SHA-1) is used to compute a
rlm@10:  * signature for the stream. The first two 32-bit quantities are used to form a
rlm@10:  * 64-bit hash. A java.lang.DataOutputStream is used to convert primitive data
rlm@10:  * types to a sequence of bytes. The values input to the stream are defined by
rlm@10:  * the Java Virtual Machine (VM) specification for classes.
rlm@10:  * 

rlm@10: * The sequence of items in the stream is as follows: rlm@10: *

rlm@10: * 1. The class name written using UTF encoding. rlm@10: * 2. The class modifiers written as a 32-bit integer. rlm@10: * 3. The name of each interface sorted by name written using UTF encoding. rlm@10: * 4. For each field of the class sorted by field name (except private static rlm@10: * and private transient fields): rlm@10: * 1. The name of the field in UTF encoding. rlm@10: * 2. The modifiers of the field written as a 32-bit integer. rlm@10: * 3. The descriptor of the field in UTF encoding rlm@10: * 5. If a class initializer exists, write out the following: rlm@10: * 1. The name of the method, <clinit>, in UTF encoding. rlm@10: * 2. The modifier of the method, java.lang.reflect.Modifier.STATIC, rlm@10: * written as a 32-bit integer. rlm@10: * 3. The descriptor of the method, ()V, in UTF encoding. rlm@10: * 6. For each non-private constructor sorted by method name and signature: rlm@10: * 1. The name of the method, <init>, in UTF encoding. rlm@10: * 2. The modifiers of the method written as a 32-bit integer. rlm@10: * 3. The descriptor of the method in UTF encoding. rlm@10: * 7. For each non-private method sorted by method name and signature: rlm@10: * 1. The name of the method in UTF encoding. rlm@10: * 2. The modifiers of the method written as a 32-bit integer. rlm@10: * 3. The descriptor of the method in UTF encoding. rlm@10: * 8. The SHA-1 algorithm is executed on the stream of bytes produced by rlm@10: * DataOutputStream and produces five 32-bit values sha[0..4]. rlm@10: *

rlm@10: * 9. The hash value is assembled from the first and second 32-bit values of rlm@10: * the SHA-1 message digest. If the result of the message digest, the five rlm@10: * 32-bit words H0 H1 H2 H3 H4, is in an array of five int values named rlm@10: * sha, the hash value would be computed as follows: rlm@10: *

rlm@10: * long hash = ((sha[0] >>> 24) & 0xFF) | rlm@10: * ((sha[0] >>> 16) & 0xFF) << 8 | rlm@10: * ((sha[0] >>> 8) & 0xFF) << 16 | rlm@10: * ((sha[0] >>> 0) & 0xFF) << 24 | rlm@10: * ((sha[1] >>> 24) & 0xFF) << 32 | rlm@10: * ((sha[1] >>> 16) & 0xFF) << 40 | rlm@10: * ((sha[1] >>> 8) & 0xFF) << 48 | rlm@10: * ((sha[1] >>> 0) & 0xFF) << 56; rlm@10: *

rlm@10: * rlm@10: * @author Rajendra Inamdar, Vishal Vishnoi rlm@10: */ rlm@10: public class SerialVersionUIDAdder extends ClassAdapter{ rlm@10: rlm@10: /** rlm@10: * Flag that indicates if we need to compute SVUID. rlm@10: */ rlm@10: protected boolean computeSVUID; rlm@10: rlm@10: /** rlm@10: * Set to true if the class already has SVUID. rlm@10: */ rlm@10: protected boolean hasSVUID; rlm@10: rlm@10: /** rlm@10: * Classes access flags. rlm@10: */ rlm@10: protected int access; rlm@10: rlm@10: /** rlm@10: * Internal name of the class rlm@10: */ rlm@10: protected String name; rlm@10: rlm@10: /** rlm@10: * Interfaces implemented by the class. rlm@10: */ rlm@10: protected String[] interfaces; rlm@10: rlm@10: /** rlm@10: * Collection of fields. (except private static and private transient rlm@10: * fields) rlm@10: */ rlm@10: protected Collection svuidFields; rlm@10: rlm@10: /** rlm@10: * Set to true if the class has static initializer. rlm@10: */ rlm@10: protected boolean hasStaticInitializer; rlm@10: rlm@10: /** rlm@10: * Collection of non-private constructors. rlm@10: */ rlm@10: protected Collection svuidConstructors; rlm@10: rlm@10: /** rlm@10: * Collection of non-private methods. rlm@10: */ rlm@10: protected Collection svuidMethods; rlm@10: rlm@10: /** rlm@10: * Creates a new {@link SerialVersionUIDAdder}. rlm@10: * rlm@10: * @param cv a {@link ClassVisitor} to which this visitor will delegate rlm@10: * calls. rlm@10: */ rlm@10: public SerialVersionUIDAdder(final ClassVisitor cv){ rlm@10: super(cv); rlm@10: svuidFields = new ArrayList(); rlm@10: svuidConstructors = new ArrayList(); rlm@10: svuidMethods = new ArrayList(); rlm@10: } rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Overriden methods rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: /* rlm@10: * Visit class header and get class name, access , and intefraces rlm@10: * informatoin (step 1,2, and 3) for SVUID computation. rlm@10: */ rlm@10: rlm@10: public void visit( rlm@10: final int version, rlm@10: final int access, rlm@10: final String name, rlm@10: final String signature, rlm@10: final String superName, rlm@10: final String[] interfaces){ rlm@10: computeSVUID = (access & Opcodes.ACC_INTERFACE) == 0; rlm@10: rlm@10: if(computeSVUID) rlm@10: { rlm@10: this.name = name; rlm@10: this.access = access; rlm@10: this.interfaces = interfaces; rlm@10: } rlm@10: rlm@10: super.visit(version, access, name, signature, superName, interfaces); rlm@10: } rlm@10: rlm@10: /* rlm@10: * Visit the methods and get constructor and method information (step 5 and rlm@10: * 7). Also determince if there is a class initializer (step 6). rlm@10: */ rlm@10: public MethodVisitor visitMethod( rlm@10: final int access, rlm@10: final String name, rlm@10: final String desc, rlm@10: final String signature, rlm@10: final String[] exceptions){ rlm@10: if(computeSVUID) rlm@10: { rlm@10: if(name.equals("")) rlm@10: { rlm@10: hasStaticInitializer = true; rlm@10: } rlm@10: /* rlm@10: * Remembers non private constructors and methods for SVUID rlm@10: * computation For constructor and method modifiers, only the rlm@10: * ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, rlm@10: * ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT and ACC_STRICT flags rlm@10: * are used. rlm@10: */ rlm@10: int mods = access rlm@10: & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PRIVATE rlm@10: | Opcodes.ACC_PROTECTED | Opcodes.ACC_STATIC rlm@10: | Opcodes.ACC_FINAL | Opcodes.ACC_SYNCHRONIZED rlm@10: | Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_STRICT); rlm@10: rlm@10: // all non private methods rlm@10: if((access & Opcodes.ACC_PRIVATE) == 0) rlm@10: { rlm@10: if(name.equals("")) rlm@10: { rlm@10: svuidConstructors.add(new Item(name, mods, desc)); rlm@10: } rlm@10: else if(!name.equals("")) rlm@10: { rlm@10: svuidMethods.add(new Item(name, mods, desc)); rlm@10: } rlm@10: } rlm@10: } rlm@10: rlm@10: return cv.visitMethod(access, name, desc, signature, exceptions); rlm@10: } rlm@10: rlm@10: /* rlm@10: * Gets class field information for step 4 of the alogrithm. Also determines rlm@10: * if the class already has a SVUID. rlm@10: */ rlm@10: public FieldVisitor visitField( rlm@10: final int access, rlm@10: final String name, rlm@10: final String desc, rlm@10: final String signature, rlm@10: final Object value){ rlm@10: if(computeSVUID) rlm@10: { rlm@10: if(name.equals("serialVersionUID")) rlm@10: { rlm@10: // since the class already has SVUID, we won't be computing it. rlm@10: computeSVUID = false; rlm@10: hasSVUID = true; rlm@10: } rlm@10: /* rlm@10: * Remember field for SVUID computation For field modifiers, only rlm@10: * the ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, rlm@10: * ACC_FINAL, ACC_VOLATILE, and ACC_TRANSIENT flags are used when rlm@10: * computing serialVersionUID values. rlm@10: */ rlm@10: int mods = access rlm@10: & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PRIVATE rlm@10: | Opcodes.ACC_PROTECTED | Opcodes.ACC_STATIC rlm@10: | Opcodes.ACC_FINAL | Opcodes.ACC_VOLATILE | Opcodes.ACC_TRANSIENT); rlm@10: rlm@10: if((access & Opcodes.ACC_PRIVATE) == 0 rlm@10: || (access & (Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT)) == 0) rlm@10: { rlm@10: svuidFields.add(new Item(name, mods, desc)); rlm@10: } rlm@10: } rlm@10: rlm@10: return super.visitField(access, name, desc, signature, value); rlm@10: } rlm@10: rlm@10: /* rlm@10: * Add the SVUID if class doesn't have one rlm@10: */ rlm@10: public void visitEnd(){ rlm@10: // compute SVUID and add it to the class rlm@10: if(computeSVUID && !hasSVUID) rlm@10: { rlm@10: try rlm@10: { rlm@10: cv.visitField(Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, rlm@10: "serialVersionUID", rlm@10: "J", rlm@10: null, rlm@10: new Long(computeSVUID())); rlm@10: } rlm@10: catch(Throwable e) rlm@10: { rlm@10: throw new RuntimeException("Error while computing SVUID for " rlm@10: + name, e); rlm@10: } rlm@10: } rlm@10: rlm@10: super.visitEnd(); rlm@10: } rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Utility methods rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: /** rlm@10: * Returns the value of SVUID if the class doesn't have one already. Please rlm@10: * note that 0 is returned if the class already has SVUID, thus use rlm@10: * isHasSVUID to determine if the class already had an SVUID. rlm@10: * rlm@10: * @return Returns the serial version UID rlm@10: * @throws IOException rlm@10: */ rlm@10: protected long computeSVUID() throws IOException{ rlm@10: ByteArrayOutputStream bos = null; rlm@10: DataOutputStream dos = null; rlm@10: long svuid = 0; rlm@10: rlm@10: try rlm@10: { rlm@10: bos = new ByteArrayOutputStream(); rlm@10: dos = new DataOutputStream(bos); rlm@10: rlm@10: /* rlm@10: * 1. The class name written using UTF encoding. rlm@10: */ rlm@10: dos.writeUTF(name.replace('/', '.')); rlm@10: rlm@10: /* rlm@10: * 2. The class modifiers written as a 32-bit integer. rlm@10: */ rlm@10: dos.writeInt(access rlm@10: & (Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL rlm@10: | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT)); rlm@10: rlm@10: /* rlm@10: * 3. The name of each interface sorted by name written using UTF rlm@10: * encoding. rlm@10: */ rlm@10: Arrays.sort(interfaces); rlm@10: for(int i = 0; i < interfaces.length; i++) rlm@10: { rlm@10: dos.writeUTF(interfaces[i].replace('/', '.')); rlm@10: } rlm@10: rlm@10: /* rlm@10: * 4. For each field of the class sorted by field name (except rlm@10: * private static and private transient fields): rlm@10: * rlm@10: * 1. The name of the field in UTF encoding. 2. The modifiers of the rlm@10: * field written as a 32-bit integer. 3. The descriptor of the field rlm@10: * in UTF encoding rlm@10: * rlm@10: * Note that field signatutes are not dot separated. Method and rlm@10: * constructor signatures are dot separated. Go figure... rlm@10: */ rlm@10: writeItems(svuidFields, dos, false); rlm@10: rlm@10: /* rlm@10: * 5. If a class initializer exists, write out the following: 1. The rlm@10: * name of the method, , in UTF encoding. 2. The modifier of rlm@10: * the method, java.lang.reflect.Modifier.STATIC, written as a rlm@10: * 32-bit integer. 3. The descriptor of the method, ()V, in UTF rlm@10: * encoding. rlm@10: */ rlm@10: if(hasStaticInitializer) rlm@10: { rlm@10: dos.writeUTF(""); rlm@10: dos.writeInt(Opcodes.ACC_STATIC); rlm@10: dos.writeUTF("()V"); rlm@10: } // if.. rlm@10: rlm@10: /* rlm@10: * 6. For each non-private constructor sorted by method name and rlm@10: * signature: 1. The name of the method, , in UTF encoding. 2. rlm@10: * The modifiers of the method written as a 32-bit integer. 3. The rlm@10: * descriptor of the method in UTF encoding. rlm@10: */ rlm@10: writeItems(svuidConstructors, dos, true); rlm@10: rlm@10: /* rlm@10: * 7. For each non-private method sorted by method name and rlm@10: * signature: 1. The name of the method in UTF encoding. 2. The rlm@10: * modifiers of the method written as a 32-bit integer. 3. The rlm@10: * descriptor of the method in UTF encoding. rlm@10: */ rlm@10: writeItems(svuidMethods, dos, true); rlm@10: rlm@10: dos.flush(); rlm@10: rlm@10: /* rlm@10: * 8. The SHA-1 algorithm is executed on the stream of bytes rlm@10: * produced by DataOutputStream and produces five 32-bit values rlm@10: * sha[0..4]. rlm@10: */ rlm@10: byte[] hashBytes = computeSHAdigest(bos.toByteArray()); rlm@10: rlm@10: /* rlm@10: * 9. The hash value is assembled from the first and second 32-bit rlm@10: * values of the SHA-1 message digest. If the result of the message rlm@10: * digest, the five 32-bit words H0 H1 H2 H3 H4, is in an array of rlm@10: * five int values named sha, the hash value would be computed as rlm@10: * follows: rlm@10: * rlm@10: * long hash = ((sha[0] >>> 24) & 0xFF) | ((sha[0] >>> 16) & 0xFF) << rlm@10: * 8 | ((sha[0] >>> 8) & 0xFF) << 16 | ((sha[0] >>> 0) & 0xFF) << rlm@10: * 24 | ((sha[1] >>> 24) & 0xFF) << 32 | ((sha[1] >>> 16) & 0xFF) << rlm@10: * 40 | ((sha[1] >>> 8) & 0xFF) << 48 | ((sha[1] >>> 0) & 0xFF) << rlm@10: * 56; rlm@10: */ rlm@10: for(int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) rlm@10: { rlm@10: svuid = (svuid << 8) | (hashBytes[i] & 0xFF); rlm@10: } rlm@10: } rlm@10: finally rlm@10: { rlm@10: // close the stream (if open) rlm@10: if(dos != null) rlm@10: { rlm@10: dos.close(); rlm@10: } rlm@10: } rlm@10: rlm@10: return svuid; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Returns the SHA-1 message digest of the given value. rlm@10: * rlm@10: * @param value the value whose SHA message digest must be computed. rlm@10: * @return the SHA-1 message digest of the given value. rlm@10: */ rlm@10: protected byte[] computeSHAdigest(final byte[] value){ rlm@10: try rlm@10: { rlm@10: return MessageDigest.getInstance("SHA").digest(value); rlm@10: } rlm@10: catch(Exception e) rlm@10: { rlm@10: throw new UnsupportedOperationException(e); rlm@10: } rlm@10: } rlm@10: rlm@10: /** rlm@10: * Sorts the items in the collection and writes it to the data output stream rlm@10: * rlm@10: * @param itemCollection collection of items rlm@10: * @param dos a DataOutputStream value rlm@10: * @param dotted a boolean value rlm@10: * @throws IOException if an error occurs rlm@10: */ rlm@10: private void writeItems( rlm@10: final Collection itemCollection, rlm@10: final DataOutputStream dos, rlm@10: final boolean dotted) throws IOException{ rlm@10: int size = itemCollection.size(); rlm@10: Item items[] = (Item[]) itemCollection.toArray(new Item[size]); rlm@10: Arrays.sort(items); rlm@10: for(int i = 0; i < size; i++) rlm@10: { rlm@10: dos.writeUTF(items[i].name); rlm@10: dos.writeInt(items[i].access); rlm@10: dos.writeUTF(dotted rlm@10: ? items[i].desc.replace('/', '.') rlm@10: : items[i].desc); rlm@10: } rlm@10: } rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Inner classes rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: static class Item implements Comparable{ rlm@10: rlm@10: String name; rlm@10: rlm@10: int access; rlm@10: rlm@10: String desc; rlm@10: rlm@10: Item(final String name, final int access, final String desc){ rlm@10: this.name = name; rlm@10: this.access = access; rlm@10: this.desc = desc; rlm@10: } rlm@10: rlm@10: public int compareTo(final Object o){ rlm@10: Item other = (Item) o; rlm@10: int retVal = name.compareTo(other.name); rlm@10: if(retVal == 0) rlm@10: { rlm@10: retVal = desc.compareTo(other.desc); rlm@10: } rlm@10: return retVal; rlm@10: } rlm@10: } rlm@10: }