1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.service.impl; |
16 |
| |
17 |
| import java.lang.reflect.Modifier; |
18 |
| |
19 |
| import javassist.CtClass; |
20 |
| import javassist.CtMethod; |
21 |
| |
22 |
| import org.apache.hivemind.ApplicationRuntimeException; |
23 |
| import org.apache.hivemind.service.MethodFab; |
24 |
| import org.apache.hivemind.service.MethodSignature; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| class MethodFabImpl implements MethodFab |
34 |
| { |
35 |
| private CtClassSource _source; |
36 |
| |
37 |
| private MethodSignature _signature; |
38 |
| |
39 |
| private CtMethod _method; |
40 |
| |
41 |
| private StringBuffer _descriptionBody = new StringBuffer(); |
42 |
| |
43 |
10647
| public MethodFabImpl(CtClassSource source, MethodSignature signature, CtMethod method,
|
44 |
| String body) |
45 |
| { |
46 |
10647
| _source = source;
|
47 |
10647
| _signature = signature;
|
48 |
10647
| _method = method;
|
49 |
| |
50 |
10647
| _descriptionBody.append(body);
|
51 |
| } |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
1
| public String toString()
|
57 |
| { |
58 |
1
| StringBuffer buffer = new StringBuffer();
|
59 |
| |
60 |
1
| try
|
61 |
| { |
62 |
1
| buffer.append(Modifier.toString(_method.getModifiers()));
|
63 |
| |
64 |
1
| buffer.append(" ");
|
65 |
1
| buffer.append(_method.getReturnType().getName());
|
66 |
| |
67 |
1
| buffer.append(" ");
|
68 |
1
| buffer.append(_method.getName());
|
69 |
1
| buffer.append("(");
|
70 |
| |
71 |
1
| CtClass[] params = _method.getParameterTypes();
|
72 |
| |
73 |
1
| for (int i = 0; i < params.length; i++)
|
74 |
| { |
75 |
2
| if (i > 0)
|
76 |
1
| buffer.append(", ");
|
77 |
| |
78 |
2
| buffer.append(params[i].getName());
|
79 |
| |
80 |
2
| buffer.append(" $");
|
81 |
2
| buffer.append(i + 1);
|
82 |
| } |
83 |
1
| buffer.append(")");
|
84 |
| |
85 |
1
| CtClass[] exceptions = _method.getExceptionTypes();
|
86 |
| |
87 |
1
| for (int i = 0; i < exceptions.length; i++)
|
88 |
| { |
89 |
2
| if (i == 0)
|
90 |
1
| buffer.append("\n throws ");
|
91 |
| else |
92 |
1
| buffer.append(", ");
|
93 |
| |
94 |
2
| buffer.append(exceptions[i].getName());
|
95 |
| } |
96 |
| |
97 |
1
| buffer.append("\n");
|
98 |
1
| buffer.append(_descriptionBody);
|
99 |
| } |
100 |
| catch (Exception ex) |
101 |
| { |
102 |
0
| buffer.append(" *** ");
|
103 |
0
| buffer.append(ex);
|
104 |
| } |
105 |
| |
106 |
1
| return buffer.toString();
|
107 |
| } |
108 |
| |
109 |
21
| public void addCatch(Class exceptionClass, String catchBody)
|
110 |
| { |
111 |
21
| CtClass ctException = _source.getCtClass(exceptionClass);
|
112 |
| |
113 |
21
| try
|
114 |
| { |
115 |
21
| _method.addCatch(catchBody, ctException);
|
116 |
| } |
117 |
| catch (Exception ex) |
118 |
| { |
119 |
1
| throw new ApplicationRuntimeException(ServiceMessages.unableToAddCatch(
|
120 |
| exceptionClass, |
121 |
| _method, |
122 |
| ex), ex); |
123 |
| } |
124 |
| |
125 |
20
| _descriptionBody.append("\ncatch(");
|
126 |
20
| _descriptionBody.append(exceptionClass.getName());
|
127 |
20
| _descriptionBody.append(" $e)\n");
|
128 |
20
| _descriptionBody.append(catchBody);
|
129 |
| } |
130 |
| |
131 |
4
| public void extend(String body, boolean asFinally)
|
132 |
| { |
133 |
4
| try
|
134 |
| { |
135 |
4
| _method.insertAfter(body, asFinally);
|
136 |
| } |
137 |
| catch (Exception ex) |
138 |
| { |
139 |
1
| throw new ApplicationRuntimeException(ServiceMessages.unableToExtendMethod(
|
140 |
| _signature, |
141 |
| _method.getDeclaringClass().getName(), |
142 |
| ex), ex); |
143 |
| } |
144 |
| |
145 |
3
| _descriptionBody.append("\n");
|
146 |
| |
147 |
3
| if (asFinally)
|
148 |
1
| _descriptionBody.append("finally\n");
|
149 |
| |
150 |
3
| _descriptionBody.append(body);
|
151 |
| } |
152 |
| |
153 |
| } |