organize imports

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:

organize imports PR

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:





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:

organize imports PR w String

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.)

2 responses

  1. Just stumpled upon this post while looking for protected regions in Xpand. This post leads me to another question. – Is generating import-statements best-practise? – I never user imports while generating code, everything is referenced with FQN. Any idea? – And btw: how do you use the Formatter from JDT within your workflow?

    • Tom,
      generating code where all is referenced with FQN doesn’t make the code look well😉
      from my POV Imports should always be generated – there are different technologies possible: using HybridLabs Beautifier you can beautify the code and also generate Import statements –
      or as I’m doing it – I’m using JDT Formatter together with HybridLabs Import Generator.
      another way to solve this is with newer versions of Xpand where you can use a 2-step workflow: organizing all your imports by yourself and generating a placeholder where you later insert your organized import statements
      …will send you code later how to use JDT formatter in workflow … just working on customer project

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s