1 12 package org.eclipse.jdt.internal.corext.template.java; 13 14 import java.util.List ; 15 16 import org.eclipse.jface.text.templates.TemplateContext; 17 import org.eclipse.jface.text.templates.TemplateVariable; 18 import org.eclipse.jface.text.templates.TemplateVariableResolver; 19 20 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitCompletion.Variable; 21 22 28 public class VarResolver extends TemplateVariableResolver { 29 30 private final String fDefaultType; 31 private String fType; 32 33 36 public VarResolver() { 37 this("java.lang.Object"); } 39 40 VarResolver(String defaultType) { 41 fDefaultType= defaultType; 42 } 43 44 47 public void resolve(TemplateVariable variable, TemplateContext context) { 48 List params= variable.getVariableType().getParams(); 49 if (params.size() == 0) 50 fType= fDefaultType; 51 else 52 fType= (String ) params.get(0); 53 54 if (variable instanceof JavaVariable) { 55 JavaContext jc= (JavaContext) context; 56 JavaVariable jv= (JavaVariable) variable; 57 jv.setParamType(fType); 58 Variable[] localLariables= jc.getLocalVariables(fType); 59 Variable[] fields= jc.getFields(fType); 60 Variable[] variables= new Variable[localLariables.length + fields.length]; 61 System.arraycopy(fields, 0, variables, 0, fields.length); 62 System.arraycopy(localLariables, 0, variables, fields.length, localLariables.length); 63 64 if (variables.length > 0) { 65 jv.setChoices(variables); 66 jc.markAsUsed(jv.getDefaultValue()); 67 } else { 68 super.resolve(variable, context); 69 return; 70 } 71 if (variables.length > 1) 72 variable.setUnambiguous(false); 73 else 74 variable.setUnambiguous(isUnambiguous(context)); 75 } else 76 super.resolve(variable, context); 77 } 78 79 82 protected String [] resolveAll(TemplateContext context) { 83 JavaContext jc= (JavaContext) context; 84 Variable[] localVariables= jc.getLocalVariables(fType); 85 Variable[] fields= jc.getFields(fType); 86 Variable[] variables= new Variable[localVariables.length + fields.length]; 87 System.arraycopy(fields, 0, variables, 0, fields.length); 88 System.arraycopy(localVariables, 0, variables, fields.length, localVariables.length); 89 90 String [] names= new String [variables.length]; 91 for (int i= 0; i < variables.length; i++) 92 names[i]= variables[i].getName(); 93 if (names.length > 0) 94 jc.markAsUsed(names[0]); 95 return names; 96 } 97 } 98 | Popular Tags |