Reputation: 7940
I want to create a Java code template (Preferences -> Java -> Editor -> Templates) for a JUnit4 test method that should also include a static import to use some hamcrest matchers.
${:importStatic('org.hamcrest.MatcherAssert.*')}
${:import(org.junit.Test)}
@Test
public void ${testName}() throws Exception {
${cursor}
}
I am not able to save the code template - an error message is shown:
Template variable '' has incompatible types.
It works fine with either import statement, so the syntax seems to be correct.
How can I combine the two import statements to work together?
Upvotes: 9
Views: 3268
Reputation: 410
Simons answer worked for me.
The "Help - Eclipse Platform" explains staticimports, it also offers an example. For version Photon you can look over here:
Upvotes: 1
Reputation: 1323
This should work:
${imp:import(org.junit.Test)}
${impst:importStatic('org.hamcrest.MatcherAssert.*')}
@Test
public void ${testName}() throws Exception {
${cursor}
}
edit: If I recall correctly the first parts acts as the name variable and the second part is the type (import, var, ...). For example the following also won't work
${:newType(org.eclipse.swt.widgets.ExpandItem)}
if (${:var(boolean)}) {
${cursor}
} else {
}
You could remove imp or impst from my answer and it would also still work. In a template you can perform multiple actions on a variable, a good example is the List template.
${type:newType(org.eclipse.swt.widgets.List)} ${list:newName(org.eclipse.swt.widgets.List)}= new ${type}(${parent:var(org.eclipse.swt.widgets.Composite)}, ${style:link('SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL', 'SWT.MULTI | SWT.V_SCROLL', SWT.MULTI, 'SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL', 'SWT.SINGLE | SWT.V_SCROLL', SWT.SINGLE)});
${list}.setLayoutData(new ${gridDataType:newType(org.eclipse.swt.layout.GridData)}(SWT.${horizontal:link(FILL, BEGINNING, CENTER, END)}, SWT.${vertical:link(FILL, TOP, CENTER, BOTTOM)}, ${hex:link(true, false)}, ${vex:link(true, false)}));
${list}.setItems(${word_selection}${});
${imp:import(org.eclipse.swt.SWT)}${cursor}
Here they are performing multiple operations on the list variable
Upvotes: 12