Eclipse Tips and tricks

In a few days, Eclipse Juno will be released. This changes the version number from 3.7 (Indigo) to 4.2 (Juno) and introduces several interesting changes. I scanned through the Trips and tricks section of the Java development user guide and found several interesting points to increase productivity. Not every change is only available in Eclipse Juno, however, these are the ones that I did not know before and will use in future:

  • Entering NPE is automatically replaced with NullPointerException when using CTRL + SPACE.  This works for any class in Code Completion! Next, I will only write IAE, ISE, LL, and many more.
  • Pressing CTRL during Code Completion does not introduce an additional pair of brackes () that has to be removed when changing a method. Normally, when you have list.add("ASDF"); and change the add method via Code Completion to remove, you get list.remove(o)("ASDF");. This is quite annoying. If you accept the remove method instead of ENTER with CTRL + ENTER, you will get list.remove("ASDF"); directly.
  • Compare With works on Files and on Directories. So you can compare complete directories including subdirectories and their files with this method.
  • Package View: You can provide package abreviations for long package names
  • You can configure forbidden and discouraged files for a specific library. This allows for better code quality in huge teams as this prevents the usage of, e.g., internal classes circumventing the appropriate interfaces
  • JUnit: You can run a single test by right clicking on the method Run As -> run as JUnit Test
  • Cursor is at a bracket: Quick Fix for deleting the sourrounding statement
  • ALT + SHIFT + Z: Sourround with can be used to create Runnables, while, if, for, try-catch, etc. blocks around (a) selected line(s).
  • ALT + SHIFT + UP/DOWN: Structured Selection following the bracket structures
  • You can activate the checking of redundant null checks, potential null pointer access and for null pointer access at Java > Compiler > Errors/Warnings / Null Analysis
  • You can activate the validation of the JavaDoc and its corresponding method as warnings in Java > Compiler > Javadoc settings.

Do you have any Eclipse tips or tricks that you use extensively?

A subtle difference …

I am currently developing with Intellij IDEA Community Edition (which is open source and free as in beer). Intellij supports Java7 very well and according my subjective opinion a little richer on features regarding refactoring or intelligent code suggestions.

Aim

My aim was to load some XML Schema files for validation purposes. Such files use the file ending xsd standing for XML Schema Definition. Now to the fun part:

You can load such files directly using the File class. This is simple, but, when packaging your application as a jar, this does not work anymore if the xsd file is within your jar. You have to reference the xsd file pointing in a jar file. This can be done by leveraging the class path and its package structure using the class loader.

If you have an xsd file name test.xsd in a package called logic along with a java class Solver you can do the following things:

//inside instance methods of a Solver instance
InputStream stream = this.getClass().getResourceAsStream("test.xsd")
//inside static methods of Solver
InputStream stream = Solver.class.getResourceAsStream("test.xsd")

//inside instance methods of any instance
InputStream stream = this.getClass().getResourceAsStream("/logic/test.xsd")
//inside static methods of any class
InputStream stream = AnyClass.class.getResourceAsStream("/logic/test.xsd")

More information on this can be found here.

Problem

And here is the very important information: Intellij IDEA does NOT copy your xsd files automatically to your binary folder. Consequently, using getResource[asStream] will NOT work. In Eclipse, everything worked fine.

Why?

Intellij IDEA has semi-colon separated list of regular expressions for files to be copied which is sold as a feature. This approach uses a white list compared to a black list in Eclipse.

How to solve?

[Settings] -> [Compiler] -> [Resource Patterns] and append ;?*.xsd to the resource pattern input field.

Automating EMF Tasks – Codegeneration

In order to execute the task of code generation of an EMF model, an ant task is quite handy to do this. Here is an example for this:

<exec executable="${eclipse.exe}">
	<arg value="-noSplash" />
	<arg value="-data ${workspace_location}" />
	<arg value="-model" />
	<arg value="-edit" />
	<arg value="-editor" />
	<arg value="-tests" />
	<arg value="-application" />
	<arg value="org.eclipse.emf.codegen.ecore.Generator" />
	<arg value="${genmodel}" />
</exec>

You can omit parameters like -edit if you do not need these parts.

Currently, I try to find an automation of the following tasks:

  • Clean the code generation (this can also be done using a simple delete command on the filesystem)
  • Create a genmodel from an ecore model
  • Reload a genmodel by an ecore model
  • Start an eclipse instance with the created plugins in its runtime

The most useful eclipse plugins

I will not explain in detail the eclipse plugins you obviously try to get like subclipse, or other eclipse plugins that helps you with your current language like PHPclipse for php, etc.

I focus on the eclipse plugins that helps you with small problems and make your like easier on a day to day basis.

  1. FindBugs for static code analysis in Java. Reviews the code and shows problems with solutions how to fix them.
    Update Site: http://findbugs.cs.umd.edu/eclipse/
  2. QuickREx for evaluating regular expressions as you type. This helps to create these expressions in quite some way.
    Update Site: http://www.bastian-bergerhoff.com/eclipse/features
  3. OpenExtern for opening a folder or file using the shell or the explorer
    Update Site: http://openextern.googlecode.com/svn/trunk/openextern_update/

Maybe this also helps you during your development of your solutions.

XText and Eclipse – FeatureMap

I wanted to convert my EMF model to an XText Grammar to generate an editor for my model. However, the feature maps are not supported in XText. Therefore, it is best to remove them. As an XSD is the source of my EMF, i just annotated the xsd:choice constructs with ecore:featureMap=””. Then, XText can create the language.

Other problems arise due to datatypes. For the XML Datatypes, specific IValueConverters have to be registered.

Acceleo 3.0 A short practical review

Acceleo features that I found after searching a little bit:

1. The variable i in a for loop is implicitly defined to represent the iteration counter

It can be used like this: [i/] and prints out 1 for the first iteration, 2 for the second, and so on and on.

2. You can define querys just like functions that can be invoked from anywhere.
[query public foo(muh : String) : OclAnyType = self /]
Call: [foo(“est”)/]

However, you can also define the query in that way, that it can be executed on a specific object.
[query public foo(e: E) : OclAnyType = self /]
Call: [e.foo()/]

3. For my current project, i have to increase the memory size of eclipse to 2048MB. Otherwise, it is unusable slow. I have more than 100 template files.

4. Properties can be used, however, there is no error message if a property is not found. Therefore, they should be used with caution as wrong text can be generated without your knowledge.

5. If you import a module which in turn also imports a module, you can only use the methods defined in the module you import yourself. This does not work transitively.

Example: Module A imports Module B; Module B imports Module C;
Module A can only use public queries and templates defined in Module B, not the ones in Module C

This has the negative side effect in my project, that i have to use about 10 or more imports on top of each file.

6. String concatenation with runtime-executed function results does not work

Example: [functionX(‘muh sadf’ + getNumberAsString(5)) /]

Solution: Use variables of the templates

[template private generateIt(path : String)
{
fullPath : String = path + ‘Filename.extension’;
}]

This is also more selfdocumenting.

7. If you need some conditional logic in your queries, just use static java methods and import them into your project as modules. This is way more easier. You can also you the types your ecore model defines in java.

Nevertheless, Eclipse Acceleo 3.0 MTL is quite a promising template framework and I enjoy using it. I hope, some of the issues above will be solved in the future.

If you have questions reguarding these issues, just contact me via email.