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