KAFKA-10787: Introduce an import order in Java sources#8404
KAFKA-10787: Introduce an import order in Java sources#8404dongjinleekr wants to merge 9 commits intoapache:trunkfrom
Conversation
|
Thanks, I think this makes sense. I also get a bit annoyed with the constant import shuffling. I'd like to hear some feedback from others though before we consider merging this. @ijuma any thoughts? |
|
I think this would be good. A couple of things:
|
I also inspected this issue a little bit. In the case of IntelliJ, checkstyle-idea plugin works like a charm - I used this plugin. In the case of Gradle, It seems like diffplug/spotless plugin is the best option as of present. It has lots of features we need and is also actively developed and maintained.
Great. I will follow the discussion result. |
354f90c to
0daddf0
Compare
0daddf0 to
4eba935
Compare
4eba935 to
c11f9d4
Compare
|
Here is the update.
|
There was a problem hiding this comment.
According to description, the import order is
- org.apach.kafka.*
- javax.*
- java.*
- *
Is org.slf4j.Logger in wrong order?
There was a problem hiding this comment.
@chia7712 Oh, the description I had at first on 2nd April is outdated; After the discussion, we concluded that the following three-group ordering would be better:
kafka,org.apache.kafkacom,net,orgjava,javax
c11f9d4 to
315fb41
Compare
|
@dongjinleekr The PR shows merge conflict. Can you rebase it? |
|
About #8404 (comment) As "spotless" can reformat code, could we use it to re-order imports? This might be the best solution. |
315fb41 to
eebfa46
Compare
|
@mjsax Rebased onto the latest trunk. Since this PR touches so many files, this PR will be broken every time the latest trunk updates. I will follow up on the latest trunk periodically as far as I can do until it is merged. |
|
@mjsax And here are some explanation of why I did not adopt any auto-import reorganization or auto-formatting. At first, I tried to add some configuration to diff/spotless plugin to reformat the code automatically. However, I found that to enable this diff/spotless requires to adopt any code formatter, like Google Java Format or Eclipse JDT Formatter, etc. Since the current Kafka code uses its own checkstyle format with 4 spaces, any one of the formatters above will reformat the whole codebase. So I concluded that it would be better to leave it unchanged for now. If most of the community prefers to adopt a new formatting rule taking advantage of this chance, I will also adapt the auto-reformat plugin. It is totally up to the community's decision. How do you think? Would it much better to open a discussion thread on dev mailing list? |
|
Personally, the import rule is good to avoid import conflicts even though there is no auto-optimization tool. |
eebfa46 to
eb26157
Compare
1. Add CustomImportOrder rule to checkstyle/checkstyle.xml 2. Set exceptions on generated sources. 3. Document per-IDE checkstyle plugins.
eb26157 to
59c7d3d
Compare
|
Rebased onto the latest trunk. |
|
Closes this PR in preference of this one. |
Until now, Kafka has not enforced any import order policy. However, the lack of import order policy has been introduced many meaningless import order changes in the commits.
This PR adds an import order policy in
checkstyle.xmlthat has been most generally accepted - that is, it organizes the imports to the following 4 groups in alphabetical order:org.apach.kafka.*javax.*java.**As an example, I reorganized 5 files in
org.apache.kafka.streams.state.internalspackage here. Please pay attention to that all java package imports are grouped together and the imports onorg.apache.kafkaalways go to the top of the file.(This PR is WIP yet; As soon as this change is accpeted, I will update the PR with reorganizing all imports.)