|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MethodFabImpl.java | 100% | 95.7% | 100% | 96.7% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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 |
* Implementation of {@link org.apache.hivemind.service.MethodFab}, which is returned from
|
|
28 |
* {@link org.apache.hivemind.service.ClassFab#addMethod(int, String, Class, Class[], Class[], String)},
|
|
29 |
* so that additional exception handlers may be attached to the added method.
|
|
30 |
*
|
|
31 |
* @author Howard Lewis Ship
|
|
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 | 9946 |
public MethodFabImpl(CtClassSource source, MethodSignature signature, CtMethod method,
|
44 |
String body) |
|
45 |
{ |
|
46 | 9946 |
_source = source; |
47 | 9946 |
_signature = signature; |
48 | 9946 |
_method = method; |
49 |
|
|
50 | 9946 |
_descriptionBody.append(body); |
51 |
} |
|
52 |
|
|
53 |
/**
|
|
54 |
* Returns a a text representation of the method, parameters and method body.
|
|
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 |
} |
|