I want to judge the necessity of testing by comparing the difference of class files when refactoring Java

I want to judge the necessity of testing by comparing the difference of class files when refactoring Java

Refactoring release without testing over 200,000 lines of codebase After reading this article, I wondered if Java could do the same with class files.

As for the background of my research, I myself handle Java at work. This is because I wanted to refactor, but I wanted to reduce the man-hours as much as possible.

Survey procedure

We will compare the hash value (SHA1) of the class file compiled with various changes based on the standard source code. I used the certutil -hashfile command to get the hash value. image.png

Reference source code

Main.java


import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * test
     */
    public static void test() {
        System.out.println("test");
    }

    /**
     * main
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(list);
        System.out.println("main");
        test();
    }

}

Hash value (SHA1): b38492ff5641fa0f074a82ca5f397c7ba7f3575d

The hash value of the Main.class file compiled from Main.java is as above. Compare this with the differences between the files after various changes.

Summary of survey results

Since the survey content is long, I will write from the results and conclusions.

There is a difference in the hash value (SHA1) of the class file

--Add code --Add a comment (new line) --Add a blank line

There was no difference in the hash value (SHA1) of the class file

--Change import order --Add a comment (existing line) --Change indent --Change grouping brackets

Conclusion

** There was no difference in the hash value (SHA1) of the class file ** Regarding the change, it seems that you can say "OK without testing" by comparing the difference of the class file. In particular, changing the indent tends to cause a large amount of difference in the source code, so I thought it would be nice if I could say "Compare class files and there is no difference, so it's OK".

investigation

Add code

Main.java


import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * test
     */
    public static void test() {
        System.out.println("test"); System.out.println("test2");
    }

    /**
     * main
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(list);
        System.out.println("main");
        test();
    }

}

Hash value (SHA1): 2356a12ac1ca0147356c1eb9824bdacf94e5eaac

Change import order

Main.java


import java.util.List;
import java.util.Arrays;

public class Main {
    /**
     * test
     */
    public static void test() {
        System.out.println("test");
    }

    /**
     * main
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(list);
        System.out.println("main");
        test();
    }

}

Hash value (SHA1): b38492ff5641fa0f074a82ca5f397c7ba7f3575d

Add comment (existing line)

Main.java


import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * test
     */
    public static void test() {
        System.out.println("test");
    }

    /**
     * main addcomment
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(list);
        System.out.println("main");
        test();
    }

}

Hash value (SHA1): b38492ff5641fa0f074a82ca5f397c7ba7f3575d

Add comment (new line)

Main.java


import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * test
     */
    public static void test() {
        System.out.println("test");
    }

    /**
     * main
     * add comment
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(list);
        System.out.println("main");
        test();
    }

}

Hash value (SHA1): ʻa05020054dd7c85ef01c20bc9d1f0cc45c46d1e1`

Add a blank line

Main.java


import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * test
     */
    public static void test() {
        System.out.println("test");
    }

    /**
     * main
     * @param args
     */
    public static void main(String[] args) {

        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(list);
        System.out.println("main");
        test();
    }

}

Hash value (SHA1): ʻa05020054dd7c85ef01c20bc9d1f0cc45c46d1e1`

Change indent

Main.java


import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * test
     */
    public static void test() {
        System.out.println("test");
    }

    /**
     * main
     * @param args
     */
    public static void main(String[] args) {
            List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(list);
        System.out.println("main");
        test();
    }

}

Hash value (SHA1): b38492ff5641fa0f074a82ca5f397c7ba7f3575d

Change the grouping brackets

Main.java


import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * test
     */
    public static void test() {
        System.out.println("test");
    }

    /**
     * main
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println((list));
        System.out.println("main");
        test();
    }

}

Hash value (SHA1): b38492ff5641fa0f074a82ca5f397c7ba7f3575d

Recommended Posts

I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
[Java] I want to calculate the difference from the date
I want to limit the input by narrowing the range of numbers
I want to know the JSP of the open portlet when developing Liferay
I want to get the IP address when connecting to Wi-Fi in Java
I want to get the field name of the [Java] field. (Old tale tone)
I want to output the day of the week
I want to var_dump the contents of the intent
First touch of the Files class (or Java 8)
I want to delete files managed by Git
[RxSwift] I want to deepen my understanding by following the definition of Observable
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
I want to recursively get the superclass and interface of a certain class
I want to call a method of another class
[JavaScript] I want to limit the processing by checking the input items when the specified time elapses at the time of focus out.
I want to judge the range using the monthly degree
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to find out which version of java the jar file I have is available
Use JLine when you want to handle keystrokes on the console character by character in Java
I tried to summarize the basics of kotlin and java
When using the constructor of Java's Date class, the date advances by 1900.
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
I want to use the Java 8 DateTime API slowly (now)
I want to give a class name to the select attribute
I want to recursively search the class list under the package
When you want to change the MySQL password of docker-compose
I want to narrow down the display of docker ps
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to simplify the conditional if-else statement in Java
Comparison of version strings (Java implementation) when you want to branch the process between two versions
I want to make the frame of the text box red when there is an input error
[Scala] [Java] I tried to fix the directory traversal vulnerability when unzipping Zip4j by bytecode operation.
Switch the version of java installed by SDKMAN when moving directories
I want to understand the flow of Spring processing request parameters
I want to return to the previous screen with kotlin and java!
The story of Collectors.groupingBy that I want to keep for posterity
I want to avoid OutOfMemory when outputting large files with POI
I tried to translate the error message when executing Eclipse (Java)
I tried to summarize the methods of Java String and StringBuilder
[Java] I tried to make a maze by the digging method ♪
[Java] I want to perform distinct with the key in the object
I want to control the default error message of Spring Boot
I made a tool to output the difference of CSV file
I want to change the value of Attribute in Selenium of Ruby
By checking the operation of Java on linux, I was able to understand compilation and hierarchical understanding.
How to disassemble Java class files
How to decompile java class files
A memo when you want to clear the time part of the calendar
What I thought when passing the user input value to the Service class
I want to display the number of orders for today using datetime.
A story I was addicted to when testing the API using MockMVC
I want to display images with REST Controller of Java and Spring!
Recorded because I was addicted to the standard input of the Scanner class
[Ruby] I want to extract only the value of the hash and only the key