java.lang.Object
org.eclipse.persistence.internal.libraries.asm.ClassVisitor
org.eclipse.persistence.internal.libraries.asm.commons.SerialVersionUIDAdder

public class SerialVersionUIDAdder extends ClassVisitor
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;
  • Constructor Details

  • Method Details

    • visit

      public void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
      Description copied from class: ClassVisitor
      Visits the header of the class.
      Overrides:
      visit in class ClassVisitor
      Parameters:
      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 Opcodes.ACC_DEPRECATED or a record Opcodes.ACC_RECORD.
      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.
    • visitMethod

      public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions)
      Description copied from class: ClassVisitor
      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.
      Overrides:
      visitMethod in class ClassVisitor
      Parameters:
      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.
      Returns:
      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.
    • visitField

      public FieldVisitor visitField(int access, String name, String desc, String signature, Object value)
      Description copied from class: ClassVisitor
      Visits a field of the class.
      Overrides:
      visitField in class ClassVisitor
      Parameters:
      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.
      Returns:
      a visitor to visit field annotations and attributes, or null if this class visitor is not interested in visiting these annotations and attributes.
    • visitInnerClass

      public void visitInnerClass(String innerClassName, String outerName, String innerName, int innerClassAccess)
      Description copied from class: ClassVisitor
      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.
      Overrides:
      visitInnerClass in class ClassVisitor
      Parameters:
      innerClassName - the internal name of C (see Type.getInternalName()).
      outerName - 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 - the (simple) name of C. Must be null for anonymous inner classes.
      innerClassAccess - the access flags of C originally declared in the source code from which this class was compiled.
    • visitEnd

      public void visitEnd()
      Description copied from class: ClassVisitor
      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.
      Overrides:
      visitEnd in class 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 IOException
      Computes and returns the value of SVUID.
      Returns:
      the serial version UID.
      Throws:
      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.