1 package org.catacomb.act;
2
3 import java.util.ArrayList;
4
5 import org.catacomb.report.E;
6
7 public class MethodSignature implements BlockSignature {
8
9 int type;
10
11 public final static int USER_SOURCE = 1;
12 public final static int SYSTEM_SOURCE = 2;
13 public final static int REQUIRED_USER_SOURCE = 3;
14 public final static int SUPER_SOURCE = 4;
15
16
17 int sourceType;
18
19 String info;
20 String returnType;
21 String functionName;
22 ArrayList<String[]> args;
23 String body;
24 MethodBody methodBody;
25
26 boolean userAccessible;
27
28 boolean superdefd;
29
30
31 public MethodSignature(String fn) {
32 this(fn, UNKNOWN);
33 }
34
35
36 public MethodSignature(String fn, int typ) {
37 functionName = fn;
38 type = typ;
39 sourceType = USER_SOURCE;
40 userAccessible = true;
41 superdefd = false;
42 }
43
44 public void setSuperDefined() {
45 superdefd = true;
46 }
47
48 public boolean superDefined() {
49 return superdefd;
50 }
51
52 public void setUserSource() {
53 sourceType = USER_SOURCE;
54 }
55
56 public void setSuperSource() {
57 sourceType = SUPER_SOURCE;
58 }
59
60
61 public void setSystemSource() {
62 sourceType = SYSTEM_SOURCE;
63 }
64
65 public void setRequiredUserSource() {
66 sourceType = REQUIRED_USER_SOURCE;
67 }
68
69 public void setReturnType(String s) {
70 returnType = s;
71 }
72
73 public void addArgument(String typ, String nm) {
74 String[] sa = {typ, nm};
75 if (args == null) {
76 args = new ArrayList<String[]>();
77 }
78 args.add(sa);
79 }
80
81
82 public void setBody(String txt) {
83 body = txt;
84 setSystemSource();
85 }
86
87
88 public void setType(int ity) {
89 type = ity;
90 }
91
92 public int getTypeCode() {
93 return type;
94 }
95
96
97 public static String getTypeInfo(int itc) {
98 String ret = "";
99 if (itc == RECEIVE) {
100 ret = "Handlers: functions that are called when an event occurs in a connected component.";
101
102 } else if (itc == SEND) {
103 ret = "Senders: these send an event to any connected components. The handler \n" +
104 "on the receiving component will be called.";
105
106
107 } else if (itc == SETTER) {
108 ret = "Setters: these set a value for use later, but have no other effect: \n" +
109 "the value is available to connected components if they ask for it";
110
111 } else if (itc == GETTER) {
112 ret = "Getters: give access to quantities in connected components.";
113 }
114 return ret;
115 }
116
117
118 public void setInfo(String s) {
119 info = s;
120 }
121
122 public String getName() {
123 return functionName;
124 }
125
126 public String toJavaSource() {
127 return toJavaSource("");
128 }
129
130 public String toAbstractJavaSource() {
131 return toJavaSource(" abstract ");
132 }
133
134
135
136
137
138
139 public String toJavaSource(String qualifier) {
140 StringBuffer sb = new StringBuffer();
141
142 if (info != null) {
143 if (info.length() > 60 || info.indexOf("\n") > 0) {
144 sb.append("/*\n");
145 sb.append(" " + info.replaceAll("\n", "\n "));
146 sb.append("\n*/\n");
147
148 } else {
149 sb.append("//");
150 sb.append(info);
151 sb.append("\n");
152 }
153 }
154 sb.append("public ");
155 sb.append(qualifier);
156 if (returnType != null) {
157 sb.append(returnType + " ");
158 } else {
159 sb.append("void ");
160 }
161 sb.append(functionName + "(");
162 sb.append(writeArgs());
163
164 sb.append(")");
165 if (qualifier.indexOf("abstract") >= 0) {
166 sb.append(";\n");
167 } else {
168 sb.append(" {\n");
169 if (body != null) {
170 sb.append(body);
171 if (body.trim().endsWith(";")) {
172
173 } else {
174 sb.append(";");
175 }
176 sb.append("\n");
177
178 } else if (methodBody != null) {
179 sb.append(methodBody.write());
180 sb.append("\n");
181 }
182 sb.append("}\n");
183 }
184 return sb.toString();
185 }
186
187
188
189
190
191
192
193
194 public MethodStub getStub() {
195 MethodStub ret = new MethodStub();
196
197 if (info != null) {
198 ret.setInfo(info);
199 }
200 ret.setVisibility("public");
201 ret.setReturnType(returnType);
202 ret.setMethodName(functionName);
203 ret.setArgList(writeArgs());
204
205 if (body != null || methodBody != null) {
206 E.warning("stubs ignoring non-null body? " + body + " " + methodBody);
207 }
208 return ret;
209 }
210
211
212
213
214
215
216
217
218
219
220
221 private String writeArgs() {
222 StringBuffer sb = new StringBuffer();
223 if (args != null && args.size() > 0) {
224 boolean follower = false;
225 for (String[] sa : args) {
226 if (follower) {
227 sb.append(", ");
228 }
229 follower = true;
230 sb.append(sa[0] + " " + sa[1]);
231 }
232
233 }
234 return sb.toString();
235 }
236
237
238 public void setUserHidden() {
239 userAccessible = false;
240 }
241
242 public boolean emptyBody() {
243 boolean ret = true;
244 if (body != null && body.trim().length() > 0) {
245 ret = false;
246 } else if (methodBody != null) {
247 ret = false;
248 }
249 return ret;
250 }
251
252 public boolean isUserAccessible() {
253 return userAccessible;
254 }
255
256 public boolean isSuper() {
257 return (sourceType == SUPER_SOURCE);
258 }
259
260 public boolean isSystem() {
261 return (sourceType == SYSTEM_SOURCE);
262 }
263
264 public boolean isUser() {
265 return (sourceType == USER_SOURCE);
266 }
267
268 public boolean isRequiredUser() {
269 return (sourceType == REQUIRED_USER_SOURCE);
270 }
271
272
273 public Object writeCommentSignature() {
274 StringBuffer sb = new StringBuffer();
275 if (returnType == null || returnType.equals("void")) {
276 sb.append(" ");
277 } else {
278 sb.append(" " + returnType + " v = ");
279 }
280 sb.append(functionName + "(" + writeArgs() + ")");
281 return sb.toString();
282 }
283
284
285 public void addBodyCaseRelay(String svar, String sid, String call) {
286 if (methodBody == null) {
287 methodBody = new CaseRelayBody(svar);
288 }
289 ((CaseRelayBody)methodBody).addCase(sid, call);
290 }
291
292
293 public void appendToBody(String line) {
294 setSystemSource();
295 if (body == null) {
296 body = line;
297 } else {
298 body = body + ";\n " + line;
299 }
300 }
301
302
303 }