[galileo] Preparing Migration oaw4 –> oaw5 (Beautifier)

openArchitectureWare moved to Eclipse Modeling as part of Galileo release. This blog series reports my experiences to migrate a huge oaw4 project. This is part 3  - you’ll find an overview of this blog series here.

oaw4 means openArchitectureWare 4.3.1oaw5 means MWE (EMFT) + Xpand (M2T) + Xtext (TMF) - more infos at oaw WorkingGroup.

You know: generated code also has to look as good as manual written code. This is the reason why openArchitectureWare supports Code – Beautifier. Since some time oaw can use the Eclipse JDT Formatter as Beautifier – the good thing of using the JDT Code Formatter: the same rules can be used in generated code as in manual written code.

Inside your workflow this might look like this:

oaw4:

<workflow>
...
        <outlet name="myOutlet" path="${my.src.dir}" overwrite="true">
<postprocessor
          		class="org.openarchitectureware.xpand2.output.JavaBeautifier"
          		configFile="format_profile.xml"/>
        </outlet>
...
</workflow>

If generating code using Xpand you write the generated code to an outlet. There can be more then one outlets and they can have different behavior, but this is another topic. After generating the code and before writing the result to disc you can use one ore more postprocessor to modify the code. The example above shows the use of a JavaBeautifier. The Xpand JavaBeautifier is in reality the Eclipse JDT Beautifier and the configFile contains your settings. If you don’t know how to create the configFile – here’s an old blog entry of Peter Friese explaining this in detail here.

Beautifying the code is not all you have to do after generating – you have to manage your Import statements. Normaly while generating you always use full qualified Class names and before writing this to disc you have to organize the Imports. This is no trivial step, but I know some have own solutions to resolve  this in their projects. AFAIK you cannot use the Eclipse “Organize Imports” programmatically.

But help is near ;-) Karsten Klein wrote some years ago an own Java code Beautifier: Hybridlabs. I used Hybrilabs as my Java Code Beautifier long time ago before it was possible to use the Eclipse JDT Formatter itself. Hybridlabs not only offers a Beautifier – Hybridlabs also offers to organize your Imports. If you’re not really sure what a beautifier does, please read the Hybridlabs documentation, there are examples. As explained above a Xpand outlet can have more then one postprocessor, so we can use both:

  • organizing imports using Hybridlabs
  • formatting code by Xpand JavaBeautifier (JDT Code Formatter)

Here’s a workflow using more then one postprocessor:

oaw4:

<workflow>
...
        <outlet name="myOutlet" path="${my.src.dir}" overwrite="true">
<postprocessor
          		class="org.hybridlabs.source.formatter.JavaImportBeautifier"
				organizeImports="true"
				format="false"/>
<postprocessor
          		class="org.openarchitectureware.xpand2.output.JavaBeautifier"
          		configFile="format_profile.xml"/>
        </outlet>
...
</workflow>
  • 1st postprocessor: Hybridlabs JavaImportBeautifier
  • 2nd postprocessor: Xpand JavaBeautifier

The order is important: At first you have to add the Beautifier to organize the Imports, because this will change your code and then you can format the code.
Please watch the use of properties:

  • organizeImports is true
  • format is false

for Hybridlabs, because we only want to organize our imports. Its up to you – if you’re not happy how Xpand JavaBeautifier formats your code then you can use Hybridlabs as the only one. The Java Formatter of Hybridlabs relys on Jalopy and you have to configure this. Please look at the doc of Hybridlabs and Jalopy. I’m happy using Eclipse JDT Formatter because then manual written code and generated code are using the same rules.

Hint: Hybridlabs also manages Imports where you already have some Imports inside a Protected Region.

Another problem arised with oaw5: Hybridlabs uses the oaw4 Interfaces, wasn’t Open Source and Karsten Klein (the owner and developer) didn’t have enough time. The good news: Hybridlabs 2.0.0 now is Open Source hosted from Karsten Klein and Karsten Thoms at Google Code here. Hybridlabs 2.0.0 supports oaw4 and oaw5 – the first release is just out and can be downloaded from here:

install oaw eclipse install hybridlabs

Because Hybridlabs 2.0.0 supports oaw4 and oaw5 its a good idea to use the new version before moving your projects to oaw5.

After downloading Hybridlabs 2.0.0 I created an oaw4 beautifier project in workspace:

oaw4:

oaw4 beautifier project libraries

there’s also in Package-Import of org.apache.commons.logging:

oaw4 beautifier project imported packages

In my case this package was exported from my slf4j / logback implementation – this may be different in your projects.

the oaw4 beautifier project exports:

oaw4 beautifier project exported packages

Because I’m only using the Import Formatter – my cartridges – projects only import org.hybridlabs.source.formatter.

Now formatting my Imports using Hybridlabs 2.0.0 works same as my old Hybridlabs 1.1.9 implementation :-) I have to make it run also using deployed bundles (plug-ins), but thats another story and at the moment the only goal is to move my projects to oaw5 and make them run in my workspace.

I created a 2nd beautifier project to be used from oaw5 in my workspace. While testing in oaw4 I close the oaw5 project and while testing in oaw5 I close the other one.

The only difference in my oaw5 beautifier project are the referenced libraries:

oaw5:

oaw5 hybridlabs referenced libs

Running the Hybridlabs Beautifier under oaw4 you need the m2t-xpand.jar inside the Beautifier project – under oaw5 you need the old oaw-xpand.jar. Of course there are other ways to do this – you can also have the libs of Hybridlabs directly inside your generator projects, but I prefer to separate things.

Now all is prepared to beautify code and organize imports in oaw4 and oaw5 :-). This blog series will continue to explain the migration – stay tuned…

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

Join 1,067 other followers

%d bloggers like this: