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("
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, 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: }