I'm trying to write an Eclipse template to generate two variables with non-conflicting names.
When I use the template twice in the same method, the second matcherVar
has the same name as the first matcherVar
giving a naming conflict that I was trying to avoid.
Incorrect output:
Pattern patternVar = Pattern.compile(patternString);
Matcher matcherVar = patternVar.matcher(matcherString); // correct matcherVar
Pattern patternVar0 = Pattern.compile(patternString);
Matcher matcherVar = pattern.matcher(matcherString); // wrong, instead: matcherVar0
As you can see I have incorrectly generated a conflicting name running the template twice.
Current Template: (newline characters and comments have been added for readability)
${:include(java.util.regex.Pattern, java.util.regex.Matcher)}
// compile Pattern object with patternString
${pattern_type:newType(java.util.regex.Pattern)}
${patternVar:newName(pattern_type)}
= ${pattern_type.compile(${patternString:var(String)});
// create Matcher object from the Pattern and matchingString
${matcher_type:newType(java.util.regex.Matcher)}
${matcherVar:newName(matcher_type)} // <------------------------- **wrong**
= ${patternVar}.matcher(${matchingString:var(String)});
Note: What is strange is that after running the template 3 or more times in the same method, the generated name does not conflict so this might be a bug with Eclipse.