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