类 SerialVersionUIDAdder
java.lang.Object
cn.taketoday.bytecode.ClassVisitor
cn.taketoday.bytecode.commons.SerialVersionUIDAdder
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:
- 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;
- 作者:
- Rajendra Inamdar, Vishal Vishnoi
-
嵌套类概要
嵌套类 -
字段概要
字段修饰符和类型字段说明private intThe class access flags.private static final StringThe JVM name of static initializer methods.private booleanA flag that indicates if we need to compute SVUID.private booleanWhether the class has a static initializer.private booleanWhether the class already has a SVUID.private String[]The interfaces implemented by the class.private StringThe internal name of the class.private Collection<SerialVersionUIDAdder.Item>The constructors of the class that are needed to compute the SVUID.private Collection<SerialVersionUIDAdder.Item>The fields of the class that are needed to compute the SVUID.private Collection<SerialVersionUIDAdder.Item>The methods of the class that are needed to compute the SVUID.从类继承的字段 cn.taketoday.bytecode.ClassVisitor
cv -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected voidaddSVUID(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 longComputes and returns the value of SVUID.booleanhasSVUID()Returns true if the class already has a SVUID field.voidvisit(int version, int access, String name, String signature, String superName, String[] interfaces) Visits the header of the class.voidvisitEnd()Visits the end of the class.visitField(int access, String name, String desc, String signature, Object value) Visits a field of the class.voidvisitInnerClass(String innerClassName, String outerName, String innerName, int innerClassAccess) Visits information about an inner class.visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) Visits a method of the class.private static voidwriteItems(Collection<SerialVersionUIDAdder.Item> itemCollection, DataOutput dataOutputStream, boolean dotted) Sorts the items in the collection and writes it to the given output stream.从类继承的方法 cn.taketoday.bytecode.ClassVisitor
visitAnnotation, visitAttribute, visitModule, visitNestHost, visitNestMember, visitOuterClass, visitPermittedSubclass, visitRecordComponent, visitSource, visitTypeAnnotation
-
字段详细资料
-
CLINIT
The JVM name of static initializer methods.- 另请参阅:
-
computeSvuid
private boolean computeSvuidA flag that indicates if we need to compute SVUID. -
hasSvuid
private boolean hasSvuidWhether the class already has a SVUID. -
access
private int accessThe class access flags. -
name
The internal name of the class. -
interfaces
The interfaces implemented by the class. -
svuidFields
The fields of the class that are needed to compute the SVUID. -
hasStaticInitializer
private boolean hasStaticInitializerWhether the class has a static initializer. -
svuidConstructors
The constructors of the class that are needed to compute the SVUID. -
svuidMethods
The methods of the class that are needed to compute the SVUID.
-
-
构造器详细资料
-
SerialVersionUIDAdder
Constructs a newSerialVersionUIDAdder.- 参数:
classVisitor- aClassVisitorto which this visitor will delegate calls.
-
-
方法详细资料
-
visit
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) 从类复制的说明:ClassVisitorVisits the header of the class.- 覆盖:
visit在类中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 (seeOpcodes). This parameter also indicates if the class is deprecatedOpcodes.ACC_DEPRECATEDor a recordOpcodes.ACC_RECORD.name- the internal name of the class (seeType.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 (seeType.getInternalName()). For interfaces, the super class isObject. May be null, but only for theObjectclass.interfaces- the internal names of the class's interfaces (seeType.getInternalName()). May be null.
-
visitMethod
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) 从类复制的说明:ClassVisitorVisits a method of the class. This method must return a newMethodVisitorinstance (or null) each time it is called, i.e., it should not return a previously returned visitor.- 覆盖:
visitMethod在类中ClassVisitor- 参数:
access- the method's access flags (seeOpcodes). This parameter also indicates if the method is synthetic and/or deprecated.name- the method's name.descriptor- the method's descriptor (seeType).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 (seeType.getInternalName()). May be null.- 返回:
- 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) 从类复制的说明:ClassVisitorVisits a field of the class.- 覆盖:
visitField在类中ClassVisitor- 参数:
access- the field's access flags (seeOpcodes). This parameter also indicates if the field is synthetic and/or deprecated.name- the field's name.desc- the field's descriptor (seeType).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 anInteger, aFloat, aLong, aDoubleor aString(forint,float,longorStringfields 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.- 返回:
- 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) 从类复制的说明:ClassVisitorVisits information about an inner class. This inner class is not necessarily a member of the class being visited.- 覆盖:
visitInnerClass在类中ClassVisitor- 参数:
innerClassName- the internal name of an inner class (seeType.getInternalName()).outerName- the internal name of the class to which the inner class belongs (seeType.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.
-
visitEnd
public void visitEnd()从类复制的说明:ClassVisitorVisits 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.- 覆盖:
visitEnd在类中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.- 返回:
- 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.- 参数:
svuid- the serialVersionUID field value.
-
computeSVUID
Computes and returns the value of SVUID.- 返回:
- the serial version UID.
- 抛出:
IOException- if an I/O error occurs.
-
computeSHADigest
protected byte[] computeSHADigest(byte[] value) Returns the SHA-1 message digest of the given value.- 参数:
value- the value whose SHA message digest must be computed.- 返回:
- the SHA-1 message digest of the given value.
-
writeItems
private static void writeItems(Collection<SerialVersionUIDAdder.Item> itemCollection, DataOutput dataOutputStream, boolean dotted) throws IOException Sorts the items in the collection and writes it to the given output stream.- 参数:
itemCollection- a collection of items.dataOutputStream- where the items must be written.dotted- whether package names must use dots, instead of slashes.- 抛出:
IOException- if an error occurs.
-