How to compare java code and apply or reject changes

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. Lots of companies create products using Java. That means that lots of teams write Java code and then the code is joined together. Sometimes young developers make mistakes. In this way experienced developers should check changes in the code. Some changes are correct and must be accepted, while others are wrong and must be rejected. GroupDocs.Comparison is the best solution for these cases.

How to compare Java code in application

The only thing you need to do is to configure GroupDocs.Comparison library in your project. After that, writing just a few lines of code, you can solve the issue.

Configuring Comparison library in a project

Firstly, add GroupDocs repository to the project to make it possible to download the library automatically

Add GroupDocs repository

Secondly, specify required library dependency

The latest version of Comparison library can be found here

Add GroupDocs.Comparison dependency

Running comparison process for Java code and getting all differences

GroupDocs.Comparison is very modern and powerful library. There are lots of facilities for comparing files, getting details of changes, analyzing changes, accepting or rejecting them, retrieving coordinates for each change and so on. In this article are shown just a small part of capabilities. More details about other facilities can find in the documentation

To compare source and target Java code files do next

As a result, you will find next two files:

  • .java result file with special marks, which mark each change in the document
  • .html human-readable result comparison file

These files can be used to analyze changes that were made in target document in comparison with source one. But it is not finish, the next step is much more useful and interesting.

Check changes in Java code to accept or reject them

With GroupDocs.Comparison you can compare lots of file formats. Moreover, you can get list of changes, reject or accept them applying to the result Java file. It can be very useful when some changes are invalid and must be reverted while other are good and must be applied to the document.

Example of applying or rejecting changes in Java code file

In the example above it gets list of changes to analyze each of them. The code checks the value of getSourceText() method. When it contains size;, or for (int i = 0; i < HASH_TABLE_SIZE (parts of changed lines in Java code file), the change will be accepted to be kept, otherwise change will be rejected to keep line like it was in source file. In order to understand described above, check images below. After rejecting or accepting, changes are applied and saved as a new file.

As a result you will find two files:

  • .java - valid Java code file where rejected changes were reverted while accepted changes were kept
  • .html - human-readable file, very similar to result that was generated on the previous step, but without changes which were rejected

Here are screenshots of all generated files:

Source Java code fileTarget Java code fileResult Html fileResult Html file with changes applied

On the images you can see that changes 0 -> 3 and 1 -> 5 were rejected, while changes long -> int and -- -> ++ were saved. The valid Java code file was also saved. You can see it below

Resultant Json file

Get a Free API License

In Comparison library are lots of facilities, for example, sensitivity of comparison, generating summary page and much more. Some of them are available only with valid license. You can get a free temporary license to use the API without evaluation limitations.

Free Online App

Along with full-featured Java/Kotlin library we provide simple, but powerful free applications.
You are welcome to compare your DOC or DOCX, XLS or XLSX, PPT or PPTX, PDF, EML, EMLX, MSG, XML and other documents with free to use online GroupDocs Comparison Application.

More details

There are much more examples of using GroupDocs.Comparison with Java/Kotlin available on our GitHub page. You are free to change and use them in order to compare photos, Excel, Word, code files, PowerPoint Presentations and much more supported file formats.

For getting details, options, and examples, you can go through the documentation and GitHub repository. Reach us on the forum for your queries.

See also