KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > template > Parameters


1 package spoon.support.template;
2
3 import java.lang.reflect.Field JavaDoc;
4 import java.lang.reflect.Modifier JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
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 /**
25  * This class defines an API to manipulate template parameters.
26  */

27 public abstract class Parameters {
28
29     private Parameters() {
30     }
31
32     /**
33      * The prefix "_FIELD_" for a parameter that represents a fields in order to
34      * avoid name clashes.
35      */

36     protected static final String JavaDoc fieldPrefix = "_FIELD_";
37
38     /**
39      * Gets the index of a one-dimension array (helper).
40      */

41     public static Integer JavaDoc getIndex(CtExpression e) {
42         if (e.getParent() instanceof CtArrayAccess) {
43             CtExpression<Integer JavaDoc> indexExpression = ((CtArrayAccess<?, CtExpression>) e
44                     .getParent()).getIndexExpression();
45             return ((CtLiteral<Integer JavaDoc>) indexExpression).getValue();
46         } else {
47             return null;
48         }
49     }
50
51     /**
52      * Gets a template field parameter value.
53      */

54     public static Object JavaDoc getValue(Template template, String JavaDoc parameterName,
55             Integer JavaDoc index) {
56         Object JavaDoc tparamValue = null;
57         try {
58             Field JavaDoc rtField = null;
59             for (Field JavaDoc 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 JavaDoc<String JavaDoc, Object JavaDoc> 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 JavaDoc[]) tparamValue)[index];
78             }
79         } catch (Exception JavaDoc e) {
80             throw new UndefinedParameterException();
81         }
82         return tparamValue;
83     }
84
85     static Map JavaDoc<Template, Map JavaDoc<String JavaDoc, Object JavaDoc>> finals = new HashMap JavaDoc<Template, Map JavaDoc<String JavaDoc, Object JavaDoc>>();
86
87     public static CtField getParameterField(CtClass<? extends Template> templateClass,String JavaDoc 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     /**
101      * Sets a template field parameter value.
102      */

103     public static void setValue(Template template, String JavaDoc parameterName,
104             Integer JavaDoc index, Object JavaDoc value) {
105         Object JavaDoc tparamValue = null;
106         try {
107             Field JavaDoc rtField = null;
108             for (Field JavaDoc 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 JavaDoc<String JavaDoc, Object JavaDoc> m = finals.get(template);
120                 if (m == null) {
121                     finals.put(template, m = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>());
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 JavaDoc[]) tparamValue)[index];
130             }
131         } catch (Exception JavaDoc e) {
132             throw new UndefinedParameterException();
133         }
134     }
135
136     private static String JavaDoc getParameterName(Field JavaDoc f) {
137         String JavaDoc 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 JavaDoc getParameterName(CtFieldReference f) {
146         String JavaDoc 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     /**
155      * Gets the names of all the template parameters of a given template type
156      * (including the ones defined by the super types).
157      */

158     public static Collection JavaDoc<String JavaDoc> getNames(
159             CtClass<? extends Template> templateType) {
160         Collection JavaDoc<String JavaDoc> params = new ArrayList JavaDoc<String JavaDoc>();
161         try {
162             for (CtFieldReference f : templateType.getReference().getAllFields()) {
163                 if (isParameterSource(f)) {
164                     params.add(getParameterName(f));
165                 }
166             }
167         } catch (Exception JavaDoc e) {
168             e.printStackTrace();
169         }
170         return params;
171     }
172
173     /**
174      * Tells if a given field is a template parameter.
175      */

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     /**
184      * Tells if a given field is a template parameter.
185      */

186     public static boolean isParameterSource(Field JavaDoc field) {
187         return field.getAnnotation(Parameter.class) != null
188                 || TemplateParameter.class.isAssignableFrom(field.getType());
189     }
190
191     /**
192      * Creates an empty template parameter of the <code>T</code> type where
193      * {@link TemplateParameter#S()} does not return <code>null</code> in case
194      * the template code needs to be executed such as in static initializers.
195      */

196     @SuppressWarnings JavaDoc("unchecked")
197     public static <T> TemplateParameter<T> NIL(Class JavaDoc<? extends T> type) {
198         if (Number JavaDoc.class.isAssignableFrom(type)) {
199             return (TemplateParameter<T>) (TemplateParameter) new TemplateParameter<Number JavaDoc>() {
200                 public CtCodeElement getSubstitution(CtSimpleType targetType) {
201                     return null;
202                 }
203
204                 public Number JavaDoc 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