If you’re generating code you have to beautify the code and to organize your imports.
I blogged about this already here.
In some MDSD projects perhaps you have the need to use “Protected Regions” – then it becomes difficult if you have some Imports inside your Protected Region:
If you generate code there will be more imports. Using the Eclipse JDT formatter works well to beautify the code, but Using Eclipse “Organize Imports” will destroy your Protected Region, because Eclipse knows nothing about this. (also at the moment its not possible to use Eclipse Organize Imports programmatically)
You can use Hybridlabs to organize your imports:
<postprocessor class="org.hybridlabs.source.formatter.JavaImportBeautifier" organizeImports="true" format="false"/> <postprocessor class="org.openarchitectureware.xpand2.output.JavaBeautifier" configFile="format_profile.xml"/>
<postprocessor class="org.hybridlabs.source.formatter.JavaImportBeautifier" organizeImports="true" format="false"/> <postprocessor class="org.eclipse.xpand2.output.JavaBeautifier" configFile="format_profile.xml"/>
Hybridlabs has a special logic where to insert imports:
- existing imports will remain as-is
- new imports will be added directly behind the last found import
But this would insert the additional imports inside our ProtectedRegion – thats not the goal 😉
A little trick
With a little trick it works:
I always generate the import java.lang.String after the end of the Protected Regions.
Now Hybridlabs finds this import and can safely place all additional imports below.
(In the example above all from Serializable to PersistenceContext is generated by Hybridlabs JavaImportBeautifier.)