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