1 package org.andromda.cartridges.meta.metafacades;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6
7
8 /***
9 * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
10 * @author Chad Brandon
11 * @since 10.12.2003
12 */
13 public class MethodData
14 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 ArrayList arguments = new ArrayList();
23 private final ArrayList exceptions = new ArrayList();
24
25 public MethodData(
26 String metafacadeName,
27 String visibility,
28 boolean isAbstract,
29 String returnTypeName,
30 String name,
31 String documentation)
32 {
33 this.metafacadeName = metafacadeName;
34 this.visibility = visibility;
35 this.isAbstract = isAbstract;
36 this.name = name;
37 this.returnTypeName = returnTypeName;
38 this.documentation = documentation;
39 }
40
41 public void addArgument(ArgumentData argument)
42 {
43 arguments.add(argument);
44 }
45
46 /***
47 * @return
48 */
49 public Collection getArguments()
50 {
51 return arguments;
52 }
53
54 public void addException(String typeName)
55 {
56 exceptions.add(typeName);
57 }
58
59 public Collection getExceptions()
60 {
61 return exceptions;
62 }
63
64 /***
65 * Gets the metafacade name.
66 *
67 * @return the name of the metafacade
68 */
69 public String getMetafacadeName()
70 {
71 return metafacadeName;
72 }
73
74 /***
75 * Gets the name of the method.
76 *
77 * @return the name.
78 */
79 public String getName()
80 {
81 return name;
82 }
83
84 /***
85 * Gets the name of the return type for this method.
86 *
87 * @return the return type name.
88 */
89 public String getReturnTypeName()
90 {
91 return returnTypeName;
92 }
93
94 /***
95 * Builds a string representing a declaration for this method.
96 *
97 * @param suppressAbstractDeclaration optionally suppress the "abstract" modifier
98 * @return String the declaration
99 */
100 public String buildMethodDeclaration(boolean suppressAbstractDeclaration)
101 {
102 String declaration =
103 visibility + " " + ((isAbstract && !suppressAbstractDeclaration) ? "abstract " : "") +
104 ((returnTypeName != null) ? (returnTypeName + " ") : "") + name + "(";
105
106 for (final Iterator iterator = arguments.iterator(); iterator.hasNext();)
107 {
108 final ArgumentData argument = (ArgumentData)iterator.next();
109 declaration += (argument.getFullyQualifiedTypeName() + " " + argument.getName());
110 if (iterator.hasNext())
111 {
112 declaration += ", ";
113 }
114 }
115 declaration += ")";
116
117 if (exceptions.size() > 0)
118 {
119 declaration += " throws ";
120 for (final Iterator iterator = exceptions.iterator(); iterator.hasNext();)
121 {
122 String exception = (String)iterator.next();
123 declaration += exception;
124 if (iterator.hasNext())
125 {
126 declaration += ", ";
127 }
128 }
129 }
130
131 return declaration;
132 }
133
134 /***
135 * Builds a string representing a call to the method.
136 *
137 * @return String how a call would look like
138 */
139 public String buildMethodCall()
140 {
141 String call = getName() + "(";
142
143 for (final Iterator iterator = arguments.iterator(); iterator.hasNext();)
144 {
145 final ArgumentData argument = (ArgumentData)iterator.next();
146 call += argument.getName();
147 if (iterator.hasNext())
148 {
149 call += ", ";
150 }
151 }
152 call += ")";
153 return call;
154 }
155
156 /***
157 * Builds a signature which can be used as a key into a map. Consists of the return type, the name and the f.q.
158 * types of the arguements.
159 *
160 * @return String the key that identifies this method
161 */
162 public String buildCharacteristicKey()
163 {
164 String key = ((returnTypeName != null) ? (returnTypeName + " ") : "") + name + "(";
165
166 for (final Iterator iterator = arguments.iterator(); iterator.hasNext();)
167 {
168 final ArgumentData argument = (ArgumentData)iterator.next();
169 key += argument.getFullyQualifiedTypeName();
170 if (iterator.hasNext())
171 {
172 key += ",";
173 }
174 }
175 key += ")";
176
177 return key;
178 }
179
180 /***
181 * Indicates whether or not this method is abstract.
182 *
183 * @return true/false
184 */
185 public boolean isAbstract()
186 {
187 return isAbstract;
188 }
189
190 /***
191 * Gets the visibility of this method.
192 *
193 * @return the visibility.
194 */
195 public String getVisibility()
196 {
197 return visibility;
198 }
199
200 /***
201 * Gets the documentation for this method.
202 *
203 * @return the documentation.
204 */
205 public String getDocumentation()
206 {
207 return documentation;
208 }
209
210 /***
211 * Tells if this method returns something.
212 *
213 * @return boolean
214 */
215 public boolean isReturnTypePresent()
216 {
217 return returnTypeName != null && !returnTypeName.equals("void");
218 }
219
220 /***
221 * @see java.lang.Comparable#compareTo(java.lang.Object)
222 */
223 public int compareTo(final Object object)
224 {
225 MethodData other = (MethodData)object;
226 int result = getMetafacadeName().compareTo(other.getMetafacadeName());
227 return (result != 0) ? result : getName().compareTo(other.getName());
228 }
229 }