EQUALSVERIFIER: The technical principle analysis of the technical principles of the Release Normal Jar framework in the Java library

EQUALSVERIFIER is a tool to test the Java class. It helps developers to verify whether the classes are correctly implemented by Equals and HashCode methods.EQUALSVERIFIER uses an assertion to check the equal nature of the object, and provides a set of easy -to -use methods to check the equivalent of the class. The basic idea of the principle of EQUALSVERIFIER work is to create instances to return different objects in the Equals method, while maintaining the consistency of the HashCode method.Below we will analyze the technical principles of EqualSverifier in detail. 1. Equals and Hashcode methods In Java, the Equals method is used to compare whether the two objects are equal. The ideal Equals method should meet the following conditions: -Forneity: X.equals (x) should always return True. -Mymmetric: If x.equals (y) returns true, then y.equals (x) should also return True. -The transmission: If x.equals (y) returns true and y.equals (z) return true, then x.equals (z) should also return true. -Sexitability: If the object is not modified, then call the Equals method multiple times should return the same result. -The on the NULL value: x.equals (null) should return false. And the hashcode method is used to return the hash code of the object, the ideal hashcode method should follow the following rules: -If the two objects are equal, their hashcode value must be the same. -As that the HashCode value should be as different as possible to reduce the decline in performance caused by hash conflicts. 2. EQUALSVERIFIER working principle EQUALSVERIFIER compares them by using reflected objects and by calling Equals and HashCode methods.It first checks whether the class covers the Equals and HashCode methods. If not, it will throw an exception. Equalsverifier then generated a set of different object instances and conducted various equivalent assertions for them.It will check the following aspects: -Fihydity: First, EqualSverifier will randomly select an object instance and use it to compare it with other instances to ensure the symmetry of the Equals method. -Forneity: Next, Equalsverifier will ensure that the Equals method returns true when the object is compared with himself. -The transmission: Then, EqualSVerifier will check the transmission between objects, that is, if object A is equal to object B, object B and object C are equal, then object A and object C should also be equal. -Cheximity: EqualSverifier will call the Equals method multiple times to ensure that the same objects are called multiple times and always return the same results. -The on the NULL value: EQUALSVERIFIER will check the return value of the EQUALS method when comparing the object with the NULL value. If the class passes all the equivalent assertions and inspections, Equalsverifier will believe that the class has correctly implemented Equals and HashCode methods. 3. Use Equalsverifier Below is a simple example of using Equalsverifier: import nl.jqno.equalsverifier.EqualsVerifier; import nl.jqno.equalsverifier.Warning; public class User { private String name; private int age; // Construct function, Getter and Setter omit @Override public boolean equals(Object obj) { // Custom Equals method implementation } @Override public int hashCode() { // Customize the HashCode method implementation } public static void main(String[] args) { EqualsVerifier.forClass(User.class) .suppress (warning.nonfinal_fields) // ignore the non -final field warning .verify(); } } In the above example, we created a User class and used Equalsverifier for equivalent testing.Call the `Forclass` method to pass the class that needs to be tested, and then you can use the` SUPPRESS` method to ignore some warning information of Equalsverifier.Finally, call the `Verify` method to perform the test. Summarize: EQUALSVERIFIER is a convenient test tool that helps us verify whether the Java class is correctly implemented by Equals and HashCode methods.Its working principle is based on creating objects and verifying the equal nature of the class through equivalent assertions.By using EQUALSVERIFIER, we can easily test the equivalent of our class and improve the quality and stability of the code.