KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > template > ExpressionTemplateParameter


1 package spoon.template;
2
3 import spoon.reflect.code.CtBlock;
4 import spoon.reflect.code.CtExpression;
5 import spoon.reflect.code.CtReturn;
6 import spoon.reflect.declaration.CtClass;
7 import spoon.reflect.declaration.CtSimpleType;
8
9 /**
10  * This class represents an expression template parameter expressed in Java.
11  *
12  * <p>
13  * To define a new expression template parameter, you must subclass this class
14  * and implement the {@link #expression()} method, which actually defines the
15  * Java expression. It corresponds to a {@link spoon.reflect.code.CtExpression}.
16  */

17 public abstract class ExpressionTemplateParameter<T> implements
18         TemplateParameter<T> {
19
20     /**
21      * Creates a new expression template parameter.
22      */

23     public ExpressionTemplateParameter() {
24     }
25
26     /**
27      * This method must be implemented to define the template expression. The
28      * convention is that the defined expression corresponds to the expression
29      * returned by the return statement of the method.
30      */

31     public abstract T expression() throws Throwable JavaDoc;
32
33     public CtExpression getSubstitution(CtSimpleType<?> targetType) {
34         CtClass<?> c;
35         CtBlock<?> b;
36         c= targetType.getFactory().Template().get(this.getClass());
37         if(c==null) {
38             c=targetType.getFactory().Class().get(this.getClass());
39         }
40         if (this instanceof Template) {
41             b = Substitution.substitute(targetType, (Template) this, c
42                     .getMethod("expression").getBody());
43         } else {
44             b = targetType.getFactory().Core().clone(c.getMethod("expression")
45                     .getBody());
46         }
47         return ((CtReturn<?>) b.getStatements().get(0)).getReturnedExpression();
48     }
49
50     public T S() {
51         return null;
52     }
53 }
54
Popular Tags