Class JavaFileMergerJavaParserImpl

java.lang.Object
org.mybatis.generator.merge.java.JavaFileMergerJavaParserImpl
All Implemented Interfaces:
JavaFileMerger

public class JavaFileMergerJavaParserImpl extends Object implements JavaFileMerger
This class handles the task of merging changes into an existing Java file using JavaParser. It supports merging by removing methods and fields that have specific Javadoc tags or annotations.

Given an existing source file and a newly generated file of the same name, the merger will:

  1. Parse the existing file looking for custom additions. A custom addition is defined in these ways:
    • A body element (field, method, nested class, etc.) not marked as generated - missing both a @Generated annotation and an older style custom Javadoc tag.
    • A body element (field, method, nested class, etc.) marked as generated by an older style custom Javadoc tag and also containing the phrase "do_not_delete_during_merge".
    • Any import in the existing file that is not present in the newly generated file
    • Any super interface in the existing file that is not present in the newly generated file
    • Any enum constant missing a "generated" marker in the existing file that is not present in the new file
    It is important to know that the parser will only look for direct children of either the public type or the first non-public type in a source file.
  2. If there are no custom additions, the newly generated file is returned unmodified.
  3. If there are custom additions, then:
    • Add any imports present in the existing file but missing in the new file
    • Remove any members in the new file that match custom additions in the existing file
    • Add all custom additions from the existing file
    • The merged file is formatted and returned.

This implementation differs from the original Eclipse-based implementation in the following ways:

  1. This implementation supports merging enums and records
  2. This implementation supports merging when the existing file is a class or interface, and the newly generated file is a record (the result will be a record).
  3. This implementation does not support merging the super class from an existing file to the newly generated file.
  4. This implementation does not attempt to preserve custom annotations added to generated elements. With the generator now generating code with many annotations, it is challenging to distinguish between annotations created by MBG, and custom annotations added after code generation by a user. If you need to add annotations to generated elements, consider implementing a plugin that will create the annotations whenever the generator runs.
Author:
Freeman (original), Jeff Butler (refactoring and enhancements)
  • Constructor Details

  • Method Details

    • getMergedSource

      public String getMergedSource(String newFileContent, String existingFileContent) throws MergeException
      Merge a newly generated Java file with existing Java file content.
      Specified by:
      getMergedSource in interface JavaFileMerger
      Parameters:
      newFileContent - the content of the newly generated Java file
      existingFileContent - the content of the existing Java file
      Returns:
      the merged source, properly formatted
      Throws:
      MergeException - if the file cannot be merged for some reason