Community
Participate
Working Groups
When inserted directly in the body of a class, the following template public void m() { ${f:field(java.lang.Object)} = null; } should have "f" expanded to whatever field (of type Object) happens to be in scope; instead, it expands to just public void m() { f = null; } However, I would expect it expand to "a field in the current scope that is a subtype of any of the given types" as per the documentation [1]. If this is indeed the desired behavior, I can start working on a patch but would like to get confirmation beforehand that this is indeed how ${...:field(...)} should work. [1] <http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Fconcepts%2Fconcept-template-variables.htm>
Looks like your question is valid. In fact, I don't find this working even for the examples mentioned in the documentation. I am not sure if this ever worked and whether it belongs in UI or core. Copying Markus who might have more info.
Currently, the ${field} template variable only works at insertion positions where the field name is valid content assist completion (e.g. inside a method body). Code pointer: org.eclipse.jdt.internal.corext.template.java.FieldResolver.getVisibleVariables(..). I agree it would be nice to have it work in the "Java type members" context as well. I'm not sure about the best way to implement this. We'd probably need a second invocation of code assist (JavaContext.getCompletion()) in a CU that already contains the method header "public void m() {", but this may be hard to get right, given that we don't know much about the structure of the actual template.