A JBoss Project
Red Hat

Tools Tip#1 - Effective editing of enhanced for loops in Eclipse

posted by Max Rydahl Andersen on Dec 10, 2014.

tip jbosscentral

Comments

When you have have typed collections in your code like:

public void process(List<Product> products) {
  List<Customer> customers = getCustomers();
  // cursor here
}

Most know that you can use the foreach template via normal content assist to quickly generate code like this:

public void process(List<Product> products) {
  List<Customer> customers = getCustomers();
  for(Customer c : customers) {
    // cursor here
  }
}

But the foreach template will always choose the nearest collection, making it impossible to use with for example the products parameter in the above example.

The solution is to use the 'enhanced for loop' Quick Fix.

It is available when you have type the name of the collection and use the Ctrl+1 (Cmd+1 if on OS X) shortcut.

To illustrate it, here is a small video of it in Eclipse.

Hope you enjoyed this little tip!

Thanks to Xavier Coulon for asking the question and giving the idea for this Tip blog, and thanks to Markus Keller (JDT UI lead) for reminding me about the Quick Fix solution.

Have fun,
Max Rydahl Andersen
@maxandersen

Latest posts

JBoss Tools for Eclipse 2023-03M3

by Stéphane Bouchet on Mar 10, 2023.

JBoss Tools 4.26.0.Final for Eclipse 2022-12

by Jeff Maury on Dec 20, 2022.

JBoss Tools for Eclipse 2022-12M2

by Stéphane Bouchet on Nov 23, 2022.

JBoss Tools 4.25.0.Final for Eclipse 2022-09

by Jeff Maury on Oct 17, 2022.

Looking for older articles ? See the Archived entries.
back to top