1 package org.andromda.cartridges.meta.metafacades;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.List;
7
8
9
10
11
12
13
14 public class MethodData implements Comparable
15 {
16 private String metafacadeName;
17 private String visibility;
18 private boolean isAbstract;
19 private String name;
20 private String returnTypeName;
21 private String documentation;
22 private final List<ArgumentData> arguments = new ArrayList<ArgumentData>();
23 private final List<String> exceptions = new ArrayList<String>();
24
25
26
27
28
29
30
31
32
33 public MethodData(
34 String metafacadeNameIn,
35 String visibilityIn,
36 boolean isAbstractIn,
37 String returnTypeNameIn,
38 String nameIn,
39 String documentationIn)
40 {
41 this.metafacadeName = metafacadeNameIn;
42 this.visibility = visibilityIn;
43 this.isAbstract = isAbstractIn;
44 this.name = nameIn;
45 this.returnTypeName = returnTypeNameIn;
46 this.documentation = documentationIn;
47 }
48
49
50
51
52 public void addArgument(ArgumentData argument)
53 {
54 this.arguments.add(argument);
55 }
56
57
58
59
60 public Collection<ArgumentData> getArguments()
61 {
62 return this.arguments;
63 }
64
65
66
67
68 public void addException(String typeName)
69 {
70 this.exceptions.add(typeName);
71 }
72
73
74
75
76 public Collection<String> getExceptions()
77 {
78 return this.exceptions;
79 }
80
81
82
83
84
85
86 public String getMetafacadeName()
87 {
88 return this.metafacadeName;
89 }
90
91
92
93
94
95
96 public String getName()
97 {
98 return this.name;
99 }
100
101
102
103
104
105
106 public String getReturnTypeName()
107 {
108 return this.returnTypeName;
109 }
110
111
112
113
114
115
116
117
118 public String buildMethodDeclaration(boolean suppressAbstractDeclaration)
119 {
120 String declaration = this.visibility + ' '
121 + ((this.isAbstract && !suppressAbstractDeclaration) ? "abstract " : "")
122 + ((this.returnTypeName != null) ? (this.returnTypeName + ' ') : "") + this.name + '(';
123
124 declaration += getTypedArgumentList();
125
126
127
128
129
130
131
132
133
134 declaration += ')';
135
136 if (!this.exceptions.isEmpty())
137 {
138 declaration += " throws ";
139 for (final Iterator<String> iterator = this.exceptions.iterator(); iterator.hasNext();)
140 {
141 String exception = iterator.next();
142 declaration += exception;
143 if (iterator.hasNext())
144 {
145 declaration += ", ";
146 }
147 }
148 }
149
150 return declaration;
151 }
152
153
154
155
156
157
158 public String getTypedArgumentList()
159 {
160 return getTypedArgumentList(null);
161 }
162
163
164
165
166
167
168
169 public String getTypedArgumentList(String modifier)
170 {
171 String declaration = "";
172
173 for (final Iterator<ArgumentData> iterator = this.arguments.iterator(); iterator.hasNext();)
174 {
175 final ArgumentData argument = iterator.next();
176 if (modifier!=null)
177 {
178 declaration += modifier + ' ';
179 }
180 declaration += (argument.getFullyQualifiedTypeName() + ' ' + argument.getName());
181 if (iterator.hasNext())
182 {
183 declaration += ", ";
184 }
185 }
186 return declaration;
187 }
188
189
190
191
192
193
194 public String buildMethodCall()
195 {
196 String call = getName() + '(';
197
198 for (final Iterator<ArgumentData> iterator = this.arguments.iterator(); iterator.hasNext();)
199 {
200 final ArgumentData argument = iterator.next();
201 call += argument.getName();
202 if (iterator.hasNext())
203 {
204 call += ", ";
205 }
206 }
207 call += ')';
208 return call;
209 }
210
211
212
213
214
215
216
217 public String buildCharacteristicKey()
218 {
219 String key = ((this.returnTypeName != null) ? (this.returnTypeName + ' ') : "") + this.name + '(';
220
221 for (final Iterator<ArgumentData> iterator = this.arguments.iterator(); iterator.hasNext();)
222 {
223 final ArgumentData argument = iterator.next();
224 key += argument.getFullyQualifiedTypeName();
225 if (iterator.hasNext())
226 {
227 key += ',';
228 }
229 }
230 key += ')';
231
232 return key;
233 }
234
235
236
237
238
239
240 public boolean isAbstract()
241 {
242 return this.isAbstract;
243 }
244
245
246
247
248
249
250 public String getVisibility()
251 {
252 return this.visibility;
253 }
254
255
256
257
258
259
260 public String getDocumentation()
261 {
262 return this.documentation;
263 }
264
265
266
267
268
269
270 public boolean isReturnTypePresent()
271 {
272 return this.returnTypeName != null && !"void".equals(this.returnTypeName);
273 }
274
275
276
277
278 public int compareTo(final Object object)
279 {
280 MethodData other = (MethodData)object;
281 final int result = getMetafacadeName().compareTo(other.getMetafacadeName());
282
283
284 return (result != 0) ? result : (name + arguments.size() + ", " + buildCharacteristicKey())
285 .compareTo(other.getName() + other.getArguments().size() + ", " + other.buildCharacteristicKey());
286 }
287
288
289
290
291 @Override
292 public String toString()
293 {
294 return this.buildMethodDeclaration(false);
295 }
296 }