Package org.objectweb.asm.commons
Class SerialVersionUIDAdder
- java.lang.Object
-
- org.objectweb.asm.ClassVisitor
-
- org.objectweb.asm.commons.SerialVersionUIDAdder
-
public class SerialVersionUIDAdder extends org.objectweb.asm.ClassVisitor
AClassVisitor
that adds a serial version unique identifier to a class if missing. A typical usage of this class is:ClassWriter classWriter = new ClassWriter(...); ClassVisitor svuidAdder = new SerialVersionUIDAdder(classWriter); ClassVisitor classVisitor = new MyClassAdapter(svuidAdder); new ClassReader(orginalClass).accept(classVisitor, 0);
The SVUID algorithm can be found at https://docs.oracle.com/javase/10/docs/specs/serialization/class.html#stream-unique-identifiers:
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:
- The class name written using UTF encoding.
- The class modifiers written as a 32-bit integer.
- The name of each interface sorted by name written using UTF encoding.
- For each field of the class sorted by field name (except private static and private
transient fields):
- The name of the field in UTF encoding.
- The modifiers of the field written as a 32-bit integer.
- The descriptor of the field in UTF encoding
- If a class initializer exists, write out the following:
- The name of the method, <clinit>, in UTF encoding.
- The modifier of the method, STATIC, written as a 32-bit integer.
- The descriptor of the method, ()V, in UTF encoding.
- For each non-private constructor sorted by method name and signature:
- The name of the method, <init>, in UTF encoding.
- The modifiers of the method written as a 32-bit integer.
- The descriptor of the method in UTF encoding.
- For each non-private method sorted by method name and signature:
- The name of the method in UTF encoding.
- The modifiers of the method written as a 32-bit integer.
- The descriptor of the method in UTF encoding.
- The SHA-1 algorithm is executed on the stream of bytes produced by DataOutputStream and produces five 32-bit values sha[0..4].
- 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;
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
SerialVersionUIDAdder(int api, org.objectweb.asm.ClassVisitor classVisitor)
Constructs a newSerialVersionUIDAdder
.SerialVersionUIDAdder(org.objectweb.asm.ClassVisitor classVisitor)
Constructs a newSerialVersionUIDAdder
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addSVUID(long svuid)
Adds a final static serialVersionUID field to the class, with the given value.protected byte[]
computeSHAdigest(byte[] value)
Returns the SHA-1 message digest of the given value.protected long
computeSVUID()
Computes and returns the value of SVUID.boolean
hasSVUID()
Returns true if the class already has a SVUID field.void
visit(int version, int access, java.lang.String name, java.lang.String signature, java.lang.String superName, java.lang.String[] interfaces)
void
visitEnd()
org.objectweb.asm.FieldVisitor
visitField(int access, java.lang.String name, java.lang.String desc, java.lang.String signature, java.lang.Object value)
void
visitInnerClass(java.lang.String innerClassName, java.lang.String outerName, java.lang.String innerName, int innerClassAccess)
org.objectweb.asm.MethodVisitor
visitMethod(int access, java.lang.String name, java.lang.String descriptor, java.lang.String signature, java.lang.String[] exceptions)
-
-
-
Constructor Detail
-
SerialVersionUIDAdder
public SerialVersionUIDAdder(org.objectweb.asm.ClassVisitor classVisitor)
Constructs a newSerialVersionUIDAdder
. Subclasses must not use this constructor. Instead, they must use theSerialVersionUIDAdder(int, ClassVisitor)
version.- Parameters:
classVisitor
- aClassVisitor
to which this visitor will delegate calls.- Throws:
java.lang.IllegalStateException
- If a subclass calls this constructor.
-
SerialVersionUIDAdder
protected SerialVersionUIDAdder(int api, org.objectweb.asm.ClassVisitor classVisitor)
Constructs a newSerialVersionUIDAdder
.- Parameters:
api
- the ASM API version implemented by this visitor. Must be one ofOpcodes.ASM4
,Opcodes.ASM5
,Opcodes.ASM6
,Opcodes.ASM7
,Opcodes.ASM8
orOpcodes.ASM9
.classVisitor
- aClassVisitor
to which this visitor will delegate calls.
-
-
Method Detail
-
visit
public void visit(int version, int access, java.lang.String name, java.lang.String signature, java.lang.String superName, java.lang.String[] interfaces)
- Overrides:
visit
in classorg.objectweb.asm.ClassVisitor
-
visitMethod
public org.objectweb.asm.MethodVisitor visitMethod(int access, java.lang.String name, java.lang.String descriptor, java.lang.String signature, java.lang.String[] exceptions)
- Overrides:
visitMethod
in classorg.objectweb.asm.ClassVisitor
-
visitField
public org.objectweb.asm.FieldVisitor visitField(int access, java.lang.String name, java.lang.String desc, java.lang.String signature, java.lang.Object value)
- Overrides:
visitField
in classorg.objectweb.asm.ClassVisitor
-
visitInnerClass
public void visitInnerClass(java.lang.String innerClassName, java.lang.String outerName, java.lang.String innerName, int innerClassAccess)
- Overrides:
visitInnerClass
in classorg.objectweb.asm.ClassVisitor
-
visitEnd
public void visitEnd()
- Overrides:
visitEnd
in classorg.objectweb.asm.ClassVisitor
-
hasSVUID
public boolean hasSVUID()
Returns true if the class already has a SVUID field. The result of this method is only valid when visitEnd has been called.- Returns:
- true if the class already has a SVUID field.
-
addSVUID
protected void addSVUID(long svuid)
Adds a final static serialVersionUID field to the class, with the given value.- Parameters:
svuid
- the serialVersionUID field value.
-
computeSVUID
protected long computeSVUID() throws java.io.IOException
Computes and returns the value of SVUID.- Returns:
- the serial version UID.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
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.
-
-