KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > examples > fieldaccess > template > FieldAccessTemplate


1 package spoon.examples.fieldaccess.template;
2
3 import spoon.examples.fieldaccess.annotation.Getter;
4 import spoon.examples.fieldaccess.annotation.Setter;
5 import spoon.reflect.code.CtExpression;
6 import spoon.reflect.code.CtFieldAccess;
7 import spoon.reflect.reference.CtTypeReference;
8 import spoon.template.Local;
9 import spoon.template.Parameter;
10 import spoon.template.Template;
11
12 /**
13  * This template defines the needed template code for matching and/or generating
14  * field accesses through getters and setters:
15  */

16 public class FieldAccessTemplate<_FieldType_> implements Template {
17
18     @Parameter
19     CtTypeReference _FieldType_;
20
21     @Parameter("_field_")
22     String JavaDoc __field_;
23
24     @Parameter
25     String JavaDoc _Field_;
26
27     @Parameter
28     CtExpression<_FieldType_> _setExpression_;
29
30     @Parameter
31     CtExpression<FieldAccessTemplate<_FieldType_>> _target_;
32
33     @SuppressWarnings JavaDoc("unchecked")
34     @Local
35     public FieldAccessTemplate(CtTypeReference type, String JavaDoc field,
36             CtFieldAccess<_FieldType_> fieldAccess,
37             CtExpression<_FieldType_> setExpression) {
38         _FieldType_ = type;
39         __field_ = field;
40         char[] chars = field.toCharArray();
41         chars[0] = Character.toUpperCase(chars[0]);
42         _Field_ = new String JavaDoc(chars);
43         if (fieldAccess != null) {
44             _target_ = (CtExpression<FieldAccessTemplate<_FieldType_>>) fieldAccess
45                     .getTarget();
46             if (!fieldAccess.getVariable().isStatic() && _target_ == null) {
47                 _target_ = (CtExpression<FieldAccessTemplate<_FieldType_>>) fieldAccess
48                         .getFactory().Code().createThisAccess(
49                                 fieldAccess.getVariable().getDeclaringType());
50             }
51         }
52         _setExpression_ = setExpression;
53     }
54
55     @Local
56     _FieldType_ _field_;
57
58     /**
59      * This template code defines the setter of a field {@link #_field_}. Note
60      * the template parameter {@link #_FieldType_} that contains the field's
61      * type (also defined as a type parameter) and the {@link #_Field_} that
62      * contains the name of the field with the first letter uppercased.
63      */

64     @Setter
65     public void set_Field_(_FieldType_ _field_) {
66         this._field_ = _field_;
67     }
68
69     /**
70      * This template code defines the getter of a field {@link #_field_}. Note
71      * the template parameter {@link #_FieldType_} that contains the field's
72      * type (also defined as a type parameter) and the {@link #_Field_} that
73      * contains the name of the field with the first letter uppercased.
74      */

75     @Getter
76     public _FieldType_ get_Field_() {
77         return _field_;
78     }
79
80     /**
81      * This template code defines the invocation of a setter for a field
82      * {@link #_field_}. The set expression is stored in the
83      * {@link #_setExpression_} template parameter. The {@link #_target_} can be
84      * null:
85      */

86     @Local
87     void setterInvocation() {
88         _target_.S().set_Field_(_setExpression_.S());
89     }
90
91     /**
92      * This template code defines the invocation of a getter for a field
93      * {@link #_field_}. The {@link #_target_} can be null:
94      */

95     @Local
96     void getterInvocation() {
97         _target_.S().get_Field_();
98     }
99
100 }
101
Popular Tags