Search code examples
javaeclipseeclipse-templates

Foreach assist using class-variables?


If i use the fore-each-template for the following values using

  • foreCtrl+SpaceEnterEnter in line 5
  • foreCtrl+SpaceEnterTabTabDownEnter in line 8
  • foreCtrl+SpaceEnterTabTabDownDownEnter in line 11

the following code will be produced (by Eclipse 4.2)

01  public static String[] c = new String[]{"hi"};
02  public static void test() {
03   String[] a = new String[]{"hi"};
04   int[] b = new int[]{2};
05   for (String string : a) {
06     // 1. fine
07   }
08   for (int i : b) {
09     // 2. fine too
10   }
11   for (iterable_type iterable_element : c) {
12     // 3. not resolved?
13   }
14 }

Now the question:

  1. Why cant the Array c resolve it's Array-Type and name?
  2. May this be a bug in Eclipse?

Solution

  • In the template for while,

    while (${condition:var(boolean)}) {
        ${line_selection}${cursor}
    }
    

    The ${condition:var(boolean)} does match members and static members. Note that the content-assist for var says:

    ${id:var(type[,type]*)} Evaluates to a field, local variable or parameter visible in the current scope that is a subtype of any of the given types. If no type is specified, any non- primitive variable matches.

    In the template for foreach, the template variable is different:

    for (${iterable_type} ${iterable_element} : ${iterable}) {
        ${cursor}
    }
    

    The doc for the ${iterable} variable states:

    A proposal for an iterable (array or java.lang.Iterable)

    It is not specific on whether (static) members should be proposed or not.

    EDIT: this documentation page states that

    ${iterable} Evaluates to a proposal for an iterable or array visible in the current scope.

    So according to the doc, this could in fact be a bug. It has actually already been reported here.