KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > description > TpMethod


1 package org.objectweb.kilim.description;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.objectweb.kilim.KilimException;
8
9 /**
10  * @author horn
11  */

12
13 public class TpMethod extends InlinedElement {
14     private BasicElement support;
15     private String JavaDoc methodName;
16     private boolean isStatic;
17
18     private List JavaDoc parameters;
19     private Object JavaDoc resultValue;
20
21     /**
22      * public constructor of a descriptor for a method used in a kilim template.
23      * @param aSupport : the element on which the method will be invoked.
24      * @param aMethodName : the name of the method.
25      * @param jStatic : true if the method is static.
26      * @param isP : true if the method can be used as a provider.
27      * @param isT : true if the method can be used as a transformer.
28      * @param aTemplate : the template in which the method is declared.
29      * @throws KilimException : generated if aSupport, aMethodName or aTemplate is null.
30      */

31     public TpMethod(BasicElement aSupport, String JavaDoc aMethodName, boolean jStatic, boolean isP, boolean isT, TemplateDescription aTemplate) throws KilimException {
32         super(isP, isT, aTemplate);
33         if (aMethodName == null) {
34             throw new KilimException("illegal null method name in MethodActionImpl");
35         }
36
37         support = aSupport;
38         methodName = aMethodName;
39         isStatic = jStatic;
40     }
41     
42     /**
43      * @see org.objectweb.kilim.description.BasicElement#getKind()
44      */

45     public int getKind() {
46         return KILIM.METHOD;
47     }
48
49     /**,
50      * @see org.objectweb.kilim.description.libs.MethodAction#isStatic()
51      */

52     public boolean isStatic() {
53         return isStatic;
54     }
55     
56     /**
57      * @see org.objectweb.kilim.description.libs.FieldAction#isStatic(boolean)
58      */

59     public void isStatic(boolean jStatic) throws KilimException {
60         isStatic = jStatic;
61     }
62     
63     /**
64      * @see org.objectweb.kilim.description.libs.MethodAction#getSupport()
65      */

66     public BasicElement getSupport() {
67         return support;
68     }
69     
70     /**
71      * sets the support of the method i.e. the object on which the method will be invoked.
72      * @param aSupport : the element to be used as a support.
73      * @throws KilimException ; generated if aSupport is null
74      */

75     public void setSupport(BasicElement aSupport) throws KilimException {
76         if (aSupport == null) {
77             throw new KilimException("no null support is allowed in method source ");
78         }
79         support = aSupport;
80     }
81     
82     /**
83      * returns the method name.
84      * @return String
85      */

86     public String JavaDoc getMethodName() {
87         return methodName;
88     }
89     
90     /**
91      * sets the method name.
92      * @return String
93      */

94     public void setMethodName(String JavaDoc aMethodName) throws KilimException {
95         if (aMethodName == null) {
96             throw new KilimException("no null method name is allowed in method source ");
97         }
98         methodName = aMethodName;
99     }
100
101     /**
102      * returns the parameter number.
103      * @return int
104      */

105     public int getParameterNumber() {
106         if (parameters == null) {
107             return 0;
108         }
109         return parameters.size();
110     }
111             
112
113     /**
114      * adds a new parameter.
115      * @param aParam : the parameter to be added
116      * @throws KilimException : generated if aParam is null or if if its name clashes with an existing one.
117      */

118     public void addParameter(Parameter aParam) throws KilimException {
119         if (aParam == null) {
120             throw new KilimException("illegal null value when adding parameter");
121         }
122         if (aParam.isEventSource()) {
123             if (parameters == null) {
124                 parameters = new ArrayList JavaDoc();
125             }
126             parameters.add(aParam);
127             return;
128         }
129         if (checkParameterUnicity(aParam)) {
130             if (parameters == null) {
131                 parameters = new ArrayList JavaDoc();
132             }
133             parameters.add(aParam);
134         } else {
135             throw new KilimException("name clash in parameter addition : " + aParam.getName() + " of method " + methodName + " in template " + getContainingTemplate().getName());
136         }
137     }
138     
139
140     /**
141      * inserts a parameter at a given position.
142      * @param aParam : the parameter to be inserted.
143      * @param position : the position
144      * @throws KilimException : generated if aParam is null, if its name clashes with existing parameters
145      */

146     public void insertParameter(Parameter aParam, int position) throws KilimException {
147         if (aParam == null) {
148             throw new KilimException("illegal null value when inserting parameter to method " + methodName + " in template " + getContainingTemplate().getName());
149         }
150         if (aParam.isEventSource()) {
151             if (parameters == null) {
152                 parameters = new ArrayList JavaDoc();
153             }
154             parameters.add(position, aParam);
155             return;
156         }
157         if (checkParameterUnicity(aParam)) {
158             if (parameters == null) {
159                 parameters = new ArrayList JavaDoc();
160             }
161             parameters.add(position, aParam);
162         } else {
163             throw new KilimException("name clash in parameter insertion : " + aParam.getName() + "in method " + methodName + " in template " + getContainingTemplate().getName());
164         }
165     }
166
167     /**
168      * removes a parameter.
169      * @param aParam : the parameter to be removed.
170      * @throws KilimException : generated if aParam is null or is unknown
171      */

172     public void removeParameter(Parameter aParam) throws KilimException {
173         if (aParam == null) {
174             throw new KilimException("illegal null parameter in parameter removal in method " + methodName + " in template " + getContainingTemplate().getName());
175         }
176         if (parameters == null) {
177             throw new KilimException("illegal parameter removal on no arg method " + methodName + " in template " + getContainingTemplate().getName());
178         }
179         boolean result = parameters.remove(aParam);
180         if (!result) {
181             throw new KilimException("attempt to removel an unknown parameter in method " + methodName + " in template " + getContainingTemplate().getName());
182         }
183     }
184
185     /**
186      * returns the parameter of a given position.
187      * @param aPosition : the position
188      * @return Parameter
189      * @throws KilimException : generated if the position has an illegal value.
190      */

191     public Parameter getParameter(int aPosition) throws KilimException {
192         if (parameters == null) {
193             throw new KilimException("illegal getParameter on no arg method " + methodName + " in template " + getContainingTemplate().getName());
194         }
195         if (aPosition < 0 || aPosition >= parameters.size()) {
196             throw new KilimException("illegal position value in getParameter of method " + methodName + " in template " + getContainingTemplate().getName());
197         }
198         return (Parameter) parameters.get(aPosition);
199     }
200     
201
202     /**
203      * returns as an iterator the parameters of the method.
204      * @return Iterator
205      */

206     public Iterator JavaDoc getParameters() {
207         if (parameters == null) {
208             return KILIM.EMPTY_ITERATOR;
209         }
210         
211         return parameters.listIterator();
212     }
213     
214     private boolean checkParameterUnicity(Parameter aParam) throws KilimException {
215         if (parameters == null) {
216             return true;
217         }
218         
219         if (aParam.isEventSource()) {
220             return true;
221         }
222         String JavaDoc name = aParam.getName();
223         if (name == null) {
224             return true;
225         }
226         int pLength = parameters.size();
227         for (int i = 0; i < pLength ; i++) {
228             if (name != null && name.equals(((Parameter) parameters.get(i)).getName())) {
229                 return false;
230             }
231         }
232         return true;
233     }
234 }
Popular Tags