Top Description Inners Fields Constructors Methods
org.eclipse.persistence.internal.libraries.asm.commons

public Class SerialVersionUIDAdder

extends ClassVisitor
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .DataOutput, .DataOutputStream, .IOException, java.security.MessageDigest, .NoSuchAlgorithmException, java.util.ArrayList, .Arrays, .Collection, org.eclipse.persistence.internal.libraries.asm.ClassVisitor, .FieldVisitor, .MethodVisitor, .Opcodes

A 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:

  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, 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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField 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<SerialVersionUIDAdder.Item>
svuidConstructors

The constructors of the class that are needed to compute the SVUID.

private Collection<SerialVersionUIDAdder.Item>
svuidFields

The fields of the class that are needed to compute the SVUID.

private Collection<SerialVersionUIDAdder.Item>
svuidMethods

The methods of the class that are needed to compute the SVUID.

Inherited from org.eclipse.persistence.internal.libraries.asm.ClassVisitor:
apicv

Constructor Summary

AccessConstructor and Description
public
SerialVersionUIDAdder(final ClassVisitor
a ClassVisitor to which this visitor will delegate calls.
classVisitor
)

Constructs a new SerialVersionUIDAdder.

protected
SerialVersionUIDAdder(final int
the ASM API version implemented by this visitor. Must be one of the ASMx values in Opcodes.
api
,
final ClassVisitor
a ClassVisitor to which this visitor will delegate calls.
classVisitor
)

Constructs a new SerialVersionUIDAdder.

Method Summary

Modifier and TypeMethod 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.
computeSHAdigest
(final byte[]
the value whose SHA message digest must be computed.
value
)

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

protected long

Returns:

the serial version UID.
computeSVUID
()

Computes and returns the value of SVUID.

public boolean

Returns:

true if the class already has a SVUID field.
hasSVUID
()

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 Opcodes). This parameter also indicates if the class is deprecated Opcodes#ACC_DEPRECATED or a record Opcodes#ACC_RECORD.
access
,
final String
the internal name of the class (see Type#getInternalName()).
name
,
final String
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 Type#getInternalName()). For interfaces, the super class is Object. May be null, but only for the Object class.
superName
,
final String[]
the internal names of the class's interfaces (see Type#getInternalName()). May be null.
interfaces
)

Overrides org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visit.

Visits the header of the class.
public void
public FieldVisitor
visitField(final int
the field's access flags (see Opcodes). This parameter also indicates if the field is synthetic and/or deprecated.
access
,
final String
the field's name.
name
,
final String
the field's descriptor (see Type).
desc
,
final String
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 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.
value
)

Overrides org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitField.

Visits a field of the class.
public void
visitInnerClass(final String
the internal name of C (see Type#getInternalName()).
innerClassName
,
final String
the internal name of the class or interface C is a member of (see Type#getInternalName()). Must be null if C is not the member of a class or interface (e.g. for local or anonymous classes).
outerName
,
final String
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
)

Overrides org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitInnerClass.

Visits information about an inner class.
public MethodVisitor
visitMethod(final int
the method's access flags (see Opcodes). This parameter also indicates if the method is synthetic and/or deprecated.
access
,
final String
the method's name.
name
,
final String
the method's descriptor (see Type).
descriptor
,
final String
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 Type#getInternalName()). May be null.
exceptions
)

Overrides org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitMethod.

Visits a method of the class.
private static void
writeItems(final Collection<SerialVersionUIDAdder.Item>
a collection of items.
itemCollection
,
final DataOutput
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.

Inherited from org.eclipse.persistence.internal.libraries.asm.ClassVisitor:
getDelegatevisitAnnotationvisitAttributevisitModulevisitNestHostvisitNestMembervisitOuterClassvisitPermittedSubclassvisitRecordComponentvisitSourcevisitTypeAnnotation

Field Detail

accessback to summary
private int access

The class access flags.

CLINITback to summary
private static final String CLINIT

The JVM name of static initializer methods.

computeSvuidback to summary
private boolean computeSvuid

A flag that indicates if we need to compute SVUID.

hasStaticInitializerback to summary
private boolean hasStaticInitializer

Whether the class has a static initializer.

hasSvuidback to summary
private boolean hasSvuid

Whether the class already has a SVUID.

interfacesback to summary
private String[] interfaces

The interfaces implemented by the class.

nameback to summary
private String name

The internal name of the class.

svuidConstructorsback to summary
private Collection<SerialVersionUIDAdder.Item> svuidConstructors

The constructors of the class that are needed to compute the SVUID.

svuidFieldsback to summary
private Collection<SerialVersionUIDAdder.Item> svuidFields

The fields of the class that are needed to compute the SVUID.

svuidMethodsback to summary
private Collection<SerialVersionUIDAdder.Item> svuidMethods

The methods of the class that are needed to compute the SVUID.

Constructor Detail

SerialVersionUIDAdderback to summary
public SerialVersionUIDAdder(final ClassVisitor classVisitor)

Constructs a new SerialVersionUIDAdder. Subclasses must not use this constructor. Instead, they must use the SerialVersionUIDAdder(int, ClassVisitor) version.

Parameters
classVisitor:ClassVisitor

a ClassVisitor to which this visitor will delegate calls.

Exceptions
IllegalStateException:
If a subclass calls this constructor.
SerialVersionUIDAdderback to summary
protected SerialVersionUIDAdder(final int api, final ClassVisitor classVisitor)

Constructs a new SerialVersionUIDAdder.

Parameters
api:int

the ASM API version implemented by this visitor. Must be one of the ASMx values in Opcodes.

classVisitor:ClassVisitor

a ClassVisitor to which this visitor will delegate calls.

Method Detail

addSVUIDback to summary
protected void addSVUID(final long svuid)

Adds a final static serialVersionUID field to the class, with the given value.

Parameters
svuid:long

the serialVersionUID field value.

computeSHAdigestback to summary
protected byte[] computeSHAdigest(final byte[] value)

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

Parameters
value:byte[]

the value whose SHA message digest must be computed.

Returns:byte[]

the SHA-1 message digest of the given value.

computeSVUIDback to summary
protected long computeSVUID() throws IOException

Computes and returns the value of SVUID.

Returns:long

the serial version UID.

Exceptions
IOException:
if an I/O error occurs.
hasSVUIDback 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.

Returns:boolean

true if the class already has a SVUID field.

visitback 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.eclipse.persistence.internal.libraries.asm.ClassVisitor.visit.

Doc from org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visit.

Visits the header of the class.

Parameters
version: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.

access:int

the class's access flags (see Opcodes). This parameter also indicates if the class is deprecated Opcodes#ACC_DEPRECATED or a record Opcodes#ACC_RECORD.

name:String

the internal name of the class (see Type#getInternalName()).

signature:String

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:String

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:String[]

the internal names of the class's interfaces (see Type#getInternalName()). May be null.

Annotations
@Override
visitEndback to summary
public void visitEnd()

Overrides org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitEnd.

Doc from org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitEnd.

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.

Annotations
@Override
visitFieldback to summary
public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value)

Overrides org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitField.

Doc from org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitField.

Visits a field of the class.

Parameters
access:int

the field's access flags (see Opcodes). This parameter also indicates if the field is synthetic and/or deprecated.

name:String

the field's name.

desc:String

the field's descriptor (see Type).

signature:String

the field's signature. May be null if the field's type does not use generic types.

value:Object

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.

Returns:FieldVisitor

a visitor to visit field annotations and attributes, or null if this class visitor is not interested in visiting these annotations and attributes.

Annotations
@Override
visitInnerClassback to summary
public void visitInnerClass(final String innerClassName, final String outerName, final String innerName, final int innerClassAccess)

Overrides org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitInnerClass.

Doc from org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitInnerClass.

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.

Parameters
innerClassName:String

the internal name of C (see Type#getInternalName()).

outerName:String

the internal name of the class or interface C is a member of (see Type#getInternalName()). Must be null if C is not the member of a class or interface (e.g. for local or anonymous classes).

innerName:String

the (simple) name of C. Must be null for anonymous inner classes.

innerClassAccess:int

the access flags of C originally declared in the source code from which this class was compiled.

Annotations
@Override
visitMethodback to summary
public MethodVisitor visitMethod(final int access, final String name, final String descriptor, final String signature, final String[] exceptions)

Overrides org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitMethod.

Doc from org.eclipse.persistence.internal.libraries.asm.ClassVisitor.visitMethod.

Visits a method of the class. This method must return a new MethodVisitor instance (or null) each time it is called, i.e., it should not return a previously returned visitor.

Parameters
access:int

the method's access flags (see Opcodes). This parameter also indicates if the method is synthetic and/or deprecated.

name:String

the method's name.

descriptor:String

the method's descriptor (see Type).

signature:String

the method's signature. May be null if the method parameters, return type and exceptions do not use generic types.

exceptions:String[]

the internal names of the method's exception classes (see Type#getInternalName()). May be null.

Returns:MethodVisitor

an object to visit the byte code of the method, or null if this class visitor is not interested in visiting the code of this method.

Annotations
@Override
writeItemsback to summary
private static void writeItems(final Collection<SerialVersionUIDAdder.Item> itemCollection, final DataOutput dataOutputStream, final boolean dotted) throws IOException

Sorts the items in the collection and writes it to the given output stream.

Parameters
itemCollection:Collection<SerialVersionUIDAdder.Item>

a collection of items.

dataOutputStream:DataOutput

where the items must be written.

dotted:boolean

whether package names must use dots, instead of slashes.

Exceptions
IOException:
if an error occurs.
org.eclipse.persistence.internal.libraries.asm.commons back to summary

private final Class SerialVersionUIDAdder.Item

extends Object
implements Comparable<SerialVersionUIDAdder.Item>
Class Inheritance
  • java.lang.Object
  • org.eclipse.persistence.internal.libraries.asm.commons.SerialVersionUIDAdder.Item
All Implemented Interfaces
java.lang.Comparable

Field Summary

Modifier and TypeField and Description
pack-priv final int
pack-priv final String
pack-priv final String

Constructor Summary

AccessConstructor and Description
pack-priv
Item(final String name, final int access, final String descriptor)

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(final SerialVersionUIDAdder.Item
the object to be compared.
item
)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.
public boolean
equals(final Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

accessback to summary
pack-priv final int access
descriptorback to summary
pack-priv final String descriptor
nameback to summary
pack-priv final String name

Constructor Detail

Itemback to summary
pack-priv Item(final String name, final int access, final String descriptor)

Method Detail

compareToback to summary
public int compareTo(final SerialVersionUIDAdder.Item item)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

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 signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

Parameters
item:SerialVersionUIDAdder.Item

the object to be compared.

Returns:int

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Annotations
@Override
equalsback to summary
public boolean equals(final Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

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.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override