org.objectweb.asm.commons

Class SerialVersionUIDAdder

public class SerialVersionUIDAdder extends ClassAdapter

A {@link ClassAdapter} that adds a serial version unique identifier to a class if missing. Here is typical usage of this class:
   ClassWriter cw = new ClassWriter(...);
   ClassVisitor sv = new SerialVersionUIDAdder(cw);
   ClassVisitor ca = new MyClassAdapter(sv);
   new ClassReader(orginalClass).accept(ca, false);
 
The SVUID algorithm can be found http://java.sun.com/j2se/1.4.2/docs/guide/serialization/spec/class.html:
 The serialVersionUID is computed using the signature of a stream of bytes
 that reflect the class definition. The National Institute of Standards and
 Technology (NIST) Secure Hash Algorithm (SHA-1) is used to compute a
 signature for the stream. The first two 32-bit quantities are used to form a
 64-bit hash. A java.lang.DataOutputStream is used to convert primitive data
 types to a sequence of bytes. The values input to the stream are defined by
 the Java Virtual Machine (VM) specification for classes.

 The sequence of items in the stream is as follows:

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

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

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

Author: Rajendra Inamdar, Vishal Vishnoi

Field Summary
protected intaccess
Classes access flags.
protected booleancomputeSVUID
Flag that indicates if we need to compute SVUID.
protected booleanhasStaticInitializer
Set to true if the class has static initializer.
protected booleanhasSVUID
Set to true if the class already has SVUID.
protected String[]interfaces
Interfaces implemented by the class.
protected Stringname
Internal name of the class
protected CollectionsvuidConstructors
Collection of non-private constructors.
protected CollectionsvuidFields
Collection of fields. (except private static and private transient fields)
protected CollectionsvuidMethods
Collection of non-private methods.
Constructor Summary
SerialVersionUIDAdder(ClassVisitor cv)
Creates a new {@link SerialVersionUIDAdder}.
Method Summary
protected byte[]computeSHAdigest(byte[] value)
Returns the SHA-1 message digest of the given value.
protected longcomputeSVUID()
Returns the value of SVUID if the class doesn't have one already.
voidvisit(int version, int access, String name, String signature, String superName, String[] interfaces)
voidvisitEnd()
FieldVisitorvisitField(int access, String name, String desc, String signature, Object value)
MethodVisitorvisitMethod(int access, String name, String desc, String signature, String[] exceptions)

Field Detail

access

protected int access
Classes access flags.

computeSVUID

protected boolean computeSVUID
Flag that indicates if we need to compute SVUID.

hasStaticInitializer

protected boolean hasStaticInitializer
Set to true if the class has static initializer.

hasSVUID

protected boolean hasSVUID
Set to true if the class already has SVUID.

interfaces

protected String[] interfaces
Interfaces implemented by the class.

name

protected String name
Internal name of the class

svuidConstructors

protected Collection svuidConstructors
Collection of non-private constructors.

svuidFields

protected Collection svuidFields
Collection of fields. (except private static and private transient fields)

svuidMethods

protected Collection svuidMethods
Collection of non-private methods.

Constructor Detail

SerialVersionUIDAdder

public SerialVersionUIDAdder(ClassVisitor cv)
Creates a new {@link SerialVersionUIDAdder}.

Parameters: cv a {@link ClassVisitor} to which this visitor will delegate calls.

Method Detail

computeSHAdigest

protected byte[] computeSHAdigest(byte[] value)
Returns the SHA-1 message digest of the given value.

Parameters: value the value whose SHA message digest must be computed.

Returns: the SHA-1 message digest of the given value.

computeSVUID

protected long computeSVUID()
Returns the value of SVUID if the class doesn't have one already. Please note that 0 is returned if the class already has SVUID, thus use isHasSVUID to determine if the class already had an SVUID.

Returns: Returns the serial version UID

Throws: IOException

visit

public void visit(int version, int access, String name, String signature, String superName, String[] interfaces)

visitEnd

public void visitEnd()

visitField

public FieldVisitor visitField(int access, String name, String desc, String signature, Object value)

visitMethod

public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions)