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:
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private int | access
The class access flags. |
| private static final String | CLINIT
The JVM name of static initializer methods. |
| private boolean | computeSvuid
A flag that indicates if we need to compute SVUID. |
| private boolean | hasStaticInitializer
Whether the class has a static initializer. |
| private boolean | hasSvuid
Whether the class already has a SVUID. |
| private String[] | interfaces
The interfaces implemented by the class. |
| private String | name
The internal name of the class. |
| private Collection | svuidConstructors
The constructors of the class that are needed to compute the SVUID. |
| private Collection | svuidFields
The fields of the class that are needed to compute the SVUID. |
| private Collection | svuidMethods
The methods of the class that are needed to compute the SVUID. |
| Access | Constructor and Description |
|---|---|
| public | SerialVersionUIDAdder(final ClassVisitor
a classVisitor)ClassVisitor to which this visitor will delegate calls.Constructs a new |
| protected | SerialVersionUIDAdder(final int
the ASM API version implemented by this visitor. Must be one of the api, final ClassVisitor ASMx values in Opcodes.a classVisitor)ClassVisitor to which this visitor will delegate calls.Constructs a new |
| Modifier and Type | Method and Description |
|---|---|
| protected void | addSVUID(final long
the serialVersionUID field value. svuid)Adds a final static serialVersionUID field to the class, with the given value. |
| protected byte[] | Returns: the SHA-1 message digest of the given value.the value whose SHA message digest must be computed. value)Returns the SHA-1 message digest of the given value. |
| protected long | |
| public boolean | Returns: true if the class already has a SVUID field.Returns true if the class already has a SVUID field. |
| public void | visit(final int
the class version. The minor version is stored in the 16 most significant bits,
and the major version in the 16 least significant bits. version, final int the class's access flags (see access, final String Opcodes). This parameter also indicates if
the class is deprecated Opcodes#ACC_DEPRECATED or a record Opcodes#ACC_RECORD.the internal name of the class (see name, final String Type#getInternalName()).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. signature, final String the internal of name of the super class (see superName, final String[] Type#getInternalName()).
For interfaces, the super class is Object. May be null, but only for the
Object class.the internal names of the class's interfaces (see interfaces)Type#getInternalName()). May be null.Overrides org. |
| public void | |
| public FieldVisitor | visitField(final int
the field's access flags (see access, final String Opcodes). This parameter also indicates if
the field is synthetic and/or deprecated.the field's name. name, final String the field's descriptor (see desc, final String Type).the field's signature. May be null if the field's type does not use
generic types. signature, final Object the field's initial value. This parameter, which may be null if the
field does not have an initial value, must be an value)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.Overrides org. |
| public void | visitInnerClass(final String
the internal name of C (see innerClassName, final String Type#getInternalName()).the internal name of the class or interface C is a member of (see outerName, final String Type#getInternalName()). Must be null if C is not the member of a class or
interface (e.g. for local or anonymous classes).the (simple) name of C. Must be null for anonymous inner classes. innerName, final int the access flags of C originally declared in the source code from which this
class was compiled. innerClassAccess)
Visits information about an inner class.
|
| public MethodVisitor | visitMethod(final int
the method's access flags (see access, final String Opcodes). This parameter also indicates if
the method is synthetic and/or deprecated.the method's name. name, final String the method's descriptor (see descriptor, final String Type).the method's signature. May be null if the method parameters,
return type and exceptions do not use generic types. signature, final String[] the internal names of the method's exception classes (see exceptions)Type#getInternalName()). May be null.Overrides org. |
| private static void | writeItems(final Collection<SerialVersionUIDAdder.
a collection of items. itemCollection,where the items must be written. dataOutputStream, final boolean whether package names must use dots, instead of slashes. dotted)Sorts the items in the collection and writes it to the given output stream. |
| access | back to summary |
|---|---|
| private int access The class access flags. | |
| CLINIT | back to summary |
|---|---|
| private static final String CLINIT The JVM name of static initializer methods. | |
| computeSvuid | back to summary |
|---|---|
| private boolean computeSvuid A flag that indicates if we need to compute SVUID. | |
| hasStaticInitializer | back to summary |
|---|---|
| private boolean hasStaticInitializer Whether the class has a static initializer. | |
| hasSvuid | back to summary |
|---|---|
| private boolean hasSvuid Whether the class already has a SVUID. | |
| interfaces | back to summary |
|---|---|
| private String[] interfaces The interfaces implemented by the class. | |
| name | back to summary |
|---|---|
| private String name The internal name of the class. | |
| svuidConstructors | back to summary |
|---|---|
| private Collection<SerialVersionUIDAdder. The constructors of the class that are needed to compute the SVUID. | |
| svuidFields | back to summary |
|---|---|
| private Collection<SerialVersionUIDAdder. The fields of the class that are needed to compute the SVUID. | |
| svuidMethods | back to summary |
|---|---|
| private Collection<SerialVersionUIDAdder. The methods of the class that are needed to compute the SVUID. | |
| SerialVersionUIDAdder | back to summary |
|---|---|
| public SerialVersionUIDAdder(final ClassVisitor classVisitor) Constructs a new
| |
| SerialVersionUIDAdder | back to summary |
|---|---|
| protected SerialVersionUIDAdder(final int api, final ClassVisitor classVisitor) Constructs a new
| |
| addSVUID | back to summary |
|---|---|
| protected void addSVUID(final long svuid) Adds a final static serialVersionUID field to the class, with the given value.
| |
| computeSHAdigest | back to summary |
|---|---|
| protected byte[] computeSHAdigest(final byte[] value) Returns the SHA-1 message digest of the given value.
| |
| computeSVUID | back to summary |
|---|---|
| protected long computeSVUID() throws IOException Computes and returns the value of SVUID.
| |
| hasSVUID | back to summary |
|---|---|
| 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.
| |
| visit | back to summary |
|---|---|
| public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) Overrides org. Doc from org. Visits the header of the class.
| |
| visitEnd | back to summary |
|---|---|
| public void visitEnd() Overrides org. Doc from org. Visits the end of the class. This method, which is the last one to be called, is used to inform the visitor that all the fields and methods of the class have been visited.
| |
| visitField | back to summary |
|---|---|
| public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) Overrides org. Doc from org. Visits a field of the class.
| |
| visitInnerClass | back to summary |
|---|---|
| public void visitInnerClass(final String innerClassName, final String outerName, final String innerName, final int innerClassAccess) Overrides org. Doc from org. Visits information about an inner class. This inner class is not necessarily a member of the class being visited. More precisely, every class or interface C which is referenced by this class and which is not a package member must be visited with this method. This class must reference its nested class or interface members, and its enclosing class, if any. See the JVMS 4.7.6 section for more details.
| |
| visitMethod | back to summary |
|---|---|
| public MethodVisitor visitMethod(final int access, final String name, final String descriptor, final String signature, final String[] exceptions) Overrides org. Doc from org. Visits a method of the class. This method must return a new
| |
| writeItems | back to summary |
|---|---|
| private static void writeItems(final Collection<SerialVersionUIDAdder. Sorts the items in the collection and writes it to the given output stream.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final int | |
| pack-priv final String | |
| pack-priv final String |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public int | compareTo(final SerialVersionUIDAdder.
the object to be compared. itemImplements java. |
| public boolean | equals(final Object
the reference object with which to compare. other)Overrides java. |
| public int |
| access | back to summary |
|---|---|
| pack-priv final int access | |
| descriptor | back to summary |
|---|---|
| pack-priv final String descriptor | |
| name | back to summary |
|---|---|
| pack-priv final String name | |
| Item | back to summary |
|---|---|
| pack-priv Item(final String name, final int access, final String descriptor) | |
| compareTo | back to summary |
|---|---|
| public int compareTo(final SerialVersionUIDAdder. Implements java. Doc from java. Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that
| |
| equals | back to summary |
|---|---|
| public boolean equals(final Object other) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |