KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 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 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javassist.CtClass;
23 import javassist.CtMethod;
24
25 import org.apache.hivemind.ApplicationRuntimeException;
26 import org.apache.hivemind.service.InterfaceFab;
27 import org.apache.hivemind.service.MethodSignature;
28
29 /**
30  * @author Howard M. Lewis Ship
31  */

32 public class InterfaceFabImpl extends AbstractFab implements InterfaceFab
33 {
34     private List JavaDoc _methods = new ArrayList JavaDoc();
35
36     public InterfaceFabImpl(CtClassSource source, CtClass ctClass)
37     {
38         super(source, ctClass);
39     }
40
41     public String JavaDoc toString()
42     {
43         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("InterfaceFabImpl[\npublic interface ");
44
45         CtClass ctClass = getCtClass();
46
47         buffer.append(ctClass.getName());
48
49         try
50         {
51             CtClass[] interfaces = ctClass.getInterfaces();
52
53             for (int i = 0; i < interfaces.length; i++)
54             {
55                 buffer.append(i == 0 ? " extends " : ", ");
56                 buffer.append(interfaces[i].getName());
57             }
58
59         }
60         catch (Exception JavaDoc ex)
61         {
62             buffer.append("<Exception: " + ex + ">");
63         }
64
65         Iterator JavaDoc i = _methods.iterator();
66
67         while (i.hasNext())
68         {
69             MethodSignature sig = (MethodSignature) i.next();
70
71             buffer.append("\n\npublic ");
72             buffer.append(sig);
73             buffer.append(";");
74         }
75
76         buffer.append("\n]");
77
78         return buffer.toString();
79     }
80
81     public void addMethod(MethodSignature ms)
82     {
83         CtClass ctReturnType = convertClass(ms.getReturnType());
84         CtClass[] ctParameters = convertClasses(ms.getParameterTypes());
85         CtClass[] ctExceptions = convertClasses(ms.getExceptionTypes());
86
87         CtMethod method = new CtMethod(ctReturnType, ms.getName(), ctParameters, getCtClass());
88
89         try
90         {
91             method.setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
92             method.setExceptionTypes(ctExceptions);
93
94             getCtClass().addMethod(method);
95         }
96         catch (Exception JavaDoc ex)
97         {
98             throw new ApplicationRuntimeException(ServiceMessages.unableToAddMethod(
99                     ms,
100                     getCtClass(),
101                     ex), ex);
102         }
103
104         _methods.add(ms);
105     }
106
107     public Class JavaDoc createInterface()
108     {
109         return createClass();
110     }
111
112 }
Popular Tags