KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > MethodFabImpl


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 JavaDoc;
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, MethodSignature, 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 JavaDoc _descriptionBody = new StringBuffer JavaDoc();
42
43     public MethodFabImpl(CtClassSource source, MethodSignature signature, CtMethod method,
44             String JavaDoc body)
45     {
46         _source = source;
47         _signature = signature;
48         _method = method;
49
50         _descriptionBody.append(body);
51     }
52
53     /**
54      * Returns a a text representation of the method, parameters and method body.
55      */

56     public String JavaDoc toString()
57     {
58         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
59
60         try
61         {
62             buffer.append(Modifier.toString(_method.getModifiers()));
63
64             buffer.append(" ");
65             buffer.append(_method.getReturnType().getName());
66
67             buffer.append(" ");
68             buffer.append(_method.getName());
69             buffer.append("(");
70
71             CtClass[] params = _method.getParameterTypes();
72
73             for (int i = 0; i < params.length; i++)
74             {
75                 if (i > 0)
76                     buffer.append(", ");
77
78                 buffer.append(params[i].getName());
79
80                 buffer.append(" $");
81                 buffer.append(i + 1);
82             }
83             buffer.append(")");
84
85             CtClass[] exceptions = _method.getExceptionTypes();
86
87             for (int i = 0; i < exceptions.length; i++)
88             {
89                 if (i == 0)
90                     buffer.append("\n throws ");
91                 else
92                     buffer.append(", ");
93
94                 buffer.append(exceptions[i].getName());
95             }
96
97             buffer.append("\n");
98             buffer.append(_descriptionBody);
99         }
100         catch (Exception JavaDoc ex)
101         {
102             buffer.append(" *** ");
103             buffer.append(ex);
104         }
105
106         return buffer.toString();
107     }
108
109     public void addCatch(Class JavaDoc exceptionClass, String JavaDoc catchBody)
110     {
111         CtClass ctException = _source.getCtClass(exceptionClass);
112
113         try
114         {
115             _method.addCatch(catchBody, ctException);
116         }
117         catch (Exception JavaDoc ex)
118         {
119             throw new ApplicationRuntimeException(ServiceMessages.unableToAddCatch(
120                     exceptionClass,
121                     _method,
122                     ex), ex);
123         }
124
125         _descriptionBody.append("\ncatch(");
126         _descriptionBody.append(exceptionClass.getName());
127         _descriptionBody.append(" $e)\n");
128         _descriptionBody.append(catchBody);
129     }
130
131     public void extend(String JavaDoc body, boolean asFinally)
132     {
133         try
134         {
135             _method.insertAfter(body, asFinally);
136         }
137         catch (Exception JavaDoc ex)
138         {
139             throw new ApplicationRuntimeException(ServiceMessages.unableToExtendMethod(
140                     _signature,
141                     _method.getDeclaringClass().getName(),
142                     ex), ex);
143         }
144
145         _descriptionBody.append("\n");
146
147         if (asFinally)
148             _descriptionBody.append("finally\n");
149
150         _descriptionBody.append(body);
151     }
152
153 }
Popular Tags