Skip to content

support Java source file merging #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

support Java source file merging #604

wants to merge 1 commit into from

Conversation

WongJiYong
Copy link

#595 java file merge
support Java source file merging
use https://javaparser.org/. so the maven scope from test to compile.
java source files include client,model and example.
1.Delete automatically generated code in existing files.
2.Add newly generated code to existing files.
3.And Sort by Field,Constructor,Method,InnerClass...

i fix a bug that forget to deal with the auto generate constructor code, and create pull request again.

Waitting for you create a subproject for this. Thank you for your work.

@jeffgbutler
Copy link
Member

@WongJiYong

The new sub-project is ready now. There is a new branch in this repo called "java-merger". Update your fork, then make a new pull request adding code to the new sub-project. You can add your code starting with this class: https://github.com/mybatis/generator/blob/java-merger/core/mybatis-generator-java-merger/src/main/java/org/mybatis/generator/javamerger/JavaParserJavaFileMerger.java

Once we get your code added and the basic tests run, then we'll work on doing some better end-to-end testing.

Let me know if you have any questions!

@WongJiYong
Copy link
Author

i have some trouble.

  1. in javaparse jar. i can pretty print like this.
public String prettyPrint(com.github.javaparser.ast.CompilationUnit unit) {
    PrettyPrinterConfiguration conf = new PrettyPrinterConfiguration();
    PrettyPrinter prettyPrinter = new PrettyPrinter(conf);
    return prettyPrinter.print(unit);
}

in mybatis/generator

public String prettyPrinter(org.mybatis.generator.api.dom.java.CompilationUnit  unit) {
    DefaultJavaFormatter formatter = new DefaultJavaFormatter();
    return formatter.getFormattedContent(unit);
}

but if the automatically generated java file has not changed. There will be a little difference in using javaparse to format and print after the merge,This is what we don't want to see,and is not friendly to version control.
so i need time to convert com.github.javaparser.ast.CompilationUnit to org.mybatis.generator.api.dom.java.CompilationUnit then pretty print it.
Or you give other suggestions?

@jeffgbutler
Copy link
Member

jeffgbutler commented Aug 4, 2020

I don't think it makes sense to try and convert from one DOM to another - that would be a very difficult task. But I think you can configure the PrettyPrinter in JavaParser so that it will closely match the code that comes out of the generator normally.

@jeffgbutler
Copy link
Member

Here are some settings to start with:

PrettyPrinterConfiguration conf = new PrettyPrinterConfiguration();
conf.setIndentSize(4);
conf.setIndentType(IndentType.SPACES);
conf.setIndentCaseInSwitch(false);

This should get you close, then you can tweak it as needed.

@WongJiYong
Copy link
Author

Yes, this config is default.
The merged format is very close to the auto generated format.
It's just a few line breaks that are different. and the toString statements of model class.

@jeffgbutler
Copy link
Member

Closed in favor of #606

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants