1 package spoon.support.template; 2 3 import java.lang.reflect.Field ; 4 import java.lang.reflect.Modifier ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 10 import spoon.reflect.code.CtArrayAccess; 11 import spoon.reflect.code.CtCodeElement; 12 import spoon.reflect.code.CtExpression; 13 import spoon.reflect.code.CtLiteral; 14 import spoon.reflect.declaration.CtClass; 15 import spoon.reflect.declaration.CtField; 16 import spoon.reflect.declaration.CtSimpleType; 17 import spoon.reflect.reference.CtFieldReference; 18 import spoon.reflect.reference.CtTypeParameterReference; 19 import spoon.support.util.RtHelper; 20 import spoon.template.Parameter; 21 import spoon.template.Template; 22 import spoon.template.TemplateParameter; 23 24 27 public abstract class Parameters { 28 29 private Parameters() { 30 } 31 32 36 protected static final String fieldPrefix = "_FIELD_"; 37 38 41 public static Integer getIndex(CtExpression e) { 42 if (e.getParent() instanceof CtArrayAccess) { 43 CtExpression<Integer > indexExpression = ((CtArrayAccess<?, CtExpression>) e 44 .getParent()).getIndexExpression(); 45 return ((CtLiteral<Integer >) indexExpression).getValue(); 46 } else { 47 return null; 48 } 49 } 50 51 54 public static Object getValue(Template template, String parameterName, 55 Integer index) { 56 Object tparamValue = null; 57 try { 58 Field rtField = null; 59 for (Field f : RtHelper.getAllFields(template.getClass())) { 60 if (isParameterSource(f)) { 61 if (parameterName.equals(getParameterName(f))) { 62 rtField = f; 63 break; 64 } 65 } 66 } 67 if (Modifier.isFinal(rtField.getModifiers())) { 68 Map <String , Object > m = finals.get(template); 69 if (m == null) { 70 return null; 71 } 72 return m.get(parameterName); 73 } 74 rtField.setAccessible(true); 75 tparamValue = rtField.get(template); 76 if (rtField.getType().isArray() && index!=null) { 77 tparamValue = ((Object []) tparamValue)[index]; 78 } 79 } catch (Exception e) { 80 throw new UndefinedParameterException(); 81 } 82 return tparamValue; 83 } 84 85 static Map <Template, Map <String , Object >> finals = new HashMap <Template, Map <String , Object >>(); 86 87 public static CtField getParameterField(CtClass<? extends Template> templateClass,String parameterName) { 88 for(CtField f:templateClass.getFields()) { 89 Parameter p=f.getAnnotation(Parameter.class); 90 if(p==null) continue; 91 if(f.getSimpleName().equals(parameterName)) { 92 return f; 93 } else { 94 if(parameterName.equals(p.value())) return f; 95 } 96 } 97 return null; 98 } 99 100 103 public static void setValue(Template template, String parameterName, 104 Integer index, Object value) { 105 Object tparamValue = null; 106 try { 107 Field rtField = null; 108 for (Field f : RtHelper.getAllFields(template.getClass())) { 109 if (isParameterSource(f)) { 110 if (parameterName.equals(getParameterName(f))) { 111 rtField = f; 112 break; 113 } 114 } 115 } 116 if (rtField == null) 117 return; 118 if (Modifier.isFinal(rtField.getModifiers())) { 119 Map <String , Object > m = finals.get(template); 120 if (m == null) { 121 finals.put(template, m = new HashMap <String , Object >()); 122 } 123 m.put(parameterName, value); 124 return; 125 } 126 rtField.setAccessible(true); 127 rtField.set(template, value); 128 if (rtField.getType().isArray()) { 129 tparamValue = ((Object []) tparamValue)[index]; 130 } 131 } catch (Exception e) { 132 throw new UndefinedParameterException(); 133 } 134 } 135 136 private static String getParameterName(Field f) { 137 String name = f.getName(); 138 Parameter p = f.getAnnotation(Parameter.class); 139 if (p != null && !p.value().equals("")) { 140 name = p.value(); 141 } 142 return name; 143 } 144 145 private static String getParameterName(CtFieldReference f) { 146 String name = f.getSimpleName(); 147 Parameter p = f.getAnnotation(Parameter.class); 148 if (p != null && !p.value().equals("")) { 149 name = p.value(); 150 } 151 return name; 152 } 153 154 158 public static Collection <String > getNames( 159 CtClass<? extends Template> templateType) { 160 Collection <String > params = new ArrayList <String >(); 161 try { 162 for (CtFieldReference f : templateType.getReference().getAllFields()) { 163 if (isParameterSource(f)) { 164 params.add(getParameterName(f)); 165 } 166 } 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 return params; 171 } 172 173 176 public static boolean isParameterSource(CtFieldReference<?> ref) { 177 return ref.getAnnotation(Parameter.class) != null 178 || (!((ref.getType() instanceof CtTypeParameterReference) || ref 179 .getSimpleName().equals("this")) && TemplateParameter.class 180 .isAssignableFrom(ref.getType().getActualClass())); 181 } 182 183 186 public static boolean isParameterSource(Field field) { 187 return field.getAnnotation(Parameter.class) != null 188 || TemplateParameter.class.isAssignableFrom(field.getType()); 189 } 190 191 196 @SuppressWarnings ("unchecked") 197 public static <T> TemplateParameter<T> NIL(Class <? extends T> type) { 198 if (Number .class.isAssignableFrom(type)) { 199 return (TemplateParameter<T>) (TemplateParameter) new TemplateParameter<Number >() { 200 public CtCodeElement getSubstitution(CtSimpleType targetType) { 201 return null; 202 } 203 204 public Number S() { 205 return 0; 206 } 207 }; 208 } 209 return new TemplateParameter<T>() { 210 public CtCodeElement getSubstitution(CtSimpleType targetType) { 211 return null; 212 } 213 214 public T S() { 215 return null; 216 } 217 }; 218 } 219 220 } 221 | Popular Tags |