public class SerialVersionUIDAdder extends ClassVisitor
ClassVisitor
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:
api, cv
Modifier | Constructor and Description |
---|---|
|
SerialVersionUIDAdder(ClassVisitor classVisitor)
Constructs a new
SerialVersionUIDAdder . |
protected |
SerialVersionUIDAdder(int api,
ClassVisitor classVisitor)
Constructs a new
SerialVersionUIDAdder . |
Modifier and Type | Method and 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,
String name,
String signature,
String superName,
String[] interfaces)
Visits the header of the class.
|
void |
visitEnd()
Visits the end of the class.
|
FieldVisitor |
visitField(int access,
String name,
String desc,
String signature,
Object value)
Visits a field of the class.
|
void |
visitInnerClass(String innerClassName,
String outerName,
String innerName,
int innerClassAccess)
Visits information about an inner class.
|
MethodVisitor |
visitMethod(int access,
String name,
String descriptor,
String signature,
String[] exceptions)
Visits a method of the class.
|
visitAnnotation, visitAttribute, visitModule, visitNestHost, visitNestMember, visitOuterClass, visitSource, visitTypeAnnotation
public SerialVersionUIDAdder(ClassVisitor classVisitor)
SerialVersionUIDAdder
. Subclasses must not use this
constructor. Instead, they must use the SerialVersionUIDAdder(int, ClassVisitor)
version.classVisitor
- a ClassVisitor
to which this visitor will delegate calls.IllegalStateException
- If a subclass calls this constructor.protected SerialVersionUIDAdder(int api, ClassVisitor classVisitor)
SerialVersionUIDAdder
.api
- the ASM API version implemented by this visitor. Must be one of Opcodes.ASM4
, Opcodes.ASM5
, Opcodes.ASM6
or Opcodes.ASM7
.classVisitor
- a ClassVisitor
to which this visitor will delegate calls.public void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
ClassVisitor
visit
in class ClassVisitor
version
- the class version. The minor version is stored in the 16 most significant bits,
and the major version in the 16 least significant bits.access
- the class's access flags (see Opcodes
). This parameter also indicates if
the class is deprecated.name
- the internal name of the class (see Type.getInternalName()
).signature
- the signature of this class. May be null if the class is not a
generic one, and does not extend or implement generic classes or interfaces.superName
- the internal of name of the super class (see Type.getInternalName()
).
For interfaces, the super class is Object
. May be null, but only for the
Object
class.interfaces
- the internal names of the class's interfaces (see Type.getInternalName()
). May be null.public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions)
ClassVisitor
MethodVisitor
instance (or null) each time it is called, i.e., it should not return a previously
returned visitor.visitMethod
in class ClassVisitor
access
- the method's access flags (see Opcodes
). This parameter also indicates if
the method is synthetic and/or deprecated.name
- the method's name.descriptor
- the method's descriptor (see Type
).signature
- the method's signature. May be null if the method parameters,
return type and exceptions do not use generic types.exceptions
- the internal names of the method's exception classes (see Type.getInternalName()
). May be null.public FieldVisitor visitField(int access, String name, String desc, String signature, Object value)
ClassVisitor
visitField
in class ClassVisitor
access
- the field's access flags (see Opcodes
). This parameter also indicates if
the field is synthetic and/or deprecated.name
- the field's name.desc
- the field's descriptor (see Type
).signature
- the field's signature. May be null if the field's type does not use
generic types.value
- the field's initial value. This parameter, which may be null if the
field does not have an initial value, must be an Integer
, a Float
, a Long
, a Double
or a String
(for int
, float
, long
or String
fields respectively). This parameter is only used for static
fields. Its value is ignored for non static fields, which must be initialized through
bytecode instructions in constructors or methods.public void visitInnerClass(String innerClassName, String outerName, String innerName, int innerClassAccess)
ClassVisitor
visitInnerClass
in class ClassVisitor
innerClassName
- the internal name of an inner class (see Type.getInternalName()
).outerName
- the internal name of the class to which the inner class belongs (see Type.getInternalName()
). May be null for not member classes.innerName
- the (simple) name of the inner class inside its enclosing class. May be
null for anonymous inner classes.innerClassAccess
- the access flags of the inner class as originally declared in the enclosing
class.public void visitEnd()
ClassVisitor
visitEnd
in class ClassVisitor
public boolean hasSVUID()
protected void addSVUID(long svuid)
svuid
- the serialVersionUID field value.protected long computeSVUID() throws IOException
IOException
- if an I/O error occurs.protected byte[] computeSHAdigest(byte[] value)
value
- the value whose SHA message digest must be computed.Copyright © 2022. All rights reserved.