Package org.mybatis.generator.merge.java
Class JavaFileMergerJavaParserImpl
java.lang.Object
org.mybatis.generator.merge.java.JavaFileMergerJavaParserImpl
- All Implemented Interfaces:
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:
- 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
@Generatedannotation 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
- A body element (field, method, nested class, etc.) not marked as generated - missing both a
- If there are no custom additions, the newly generated file is returned unmodified.
- 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:
- This implementation supports merging enums and records
- 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).
- This implementation does not support merging the super class from an existing file to the newly generated file.
- 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 Summary
ConstructorsConstructorDescriptionJavaFileMergerJavaParserImpl(JavaMergerFactory.PrinterConfiguration printerConfiguration) -
Method Summary
Modifier and TypeMethodDescriptiongetMergedSource(String newFileContent, String existingFileContent) Merge a newly generated Java file with existing Java file content.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.mybatis.generator.merge.java.JavaFileMerger
getMergedSource
-
Constructor Details
-
JavaFileMergerJavaParserImpl
-
-
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:
getMergedSourcein interfaceJavaFileMerger- Parameters:
newFileContent- the content of the newly generated Java fileexistingFileContent- the content of the existing Java file- Returns:
- the merged source, properly formatted
- Throws:
MergeException- if the file cannot be merged for some reason
-