|
|||||||||||||||||||
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 | |||||||||||||||
ClassFabImpl.java | 100% | 97.7% | 100% | 98.3% |
|
1 |
// Copyright 2004 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 |
import java.util.HashMap;
|
|
19 |
import java.util.Map;
|
|
20 |
|
|
21 |
import javassist.CannotCompileException;
|
|
22 |
import javassist.CtClass;
|
|
23 |
import javassist.CtConstructor;
|
|
24 |
import javassist.CtField;
|
|
25 |
import javassist.CtMethod;
|
|
26 |
|
|
27 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
28 |
import org.apache.hivemind.service.ClassFab;
|
|
29 |
import org.apache.hivemind.service.MethodFab;
|
|
30 |
import org.apache.hivemind.service.MethodSignature;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Implementation of {@link org.apache.hivemind.service.ClassFab}. Hides,
|
|
34 |
* as much as possible, the underlying library (Javassist).
|
|
35 |
*
|
|
36 |
* @author Howard Lewis Ship
|
|
37 |
*/
|
|
38 |
public class ClassFabImpl implements ClassFab |
|
39 |
{ |
|
40 |
private CtClass _ctClass;
|
|
41 |
private CtClassSource _source;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Map of {@link MethodFab} keyed on {@link MethodSignature}.
|
|
45 |
*/
|
|
46 |
private Map _methods = new HashMap(); |
|
47 |
|
|
48 | 1370 |
public ClassFabImpl(CtClassSource source, CtClass ctClass)
|
49 |
{ |
|
50 | 1370 |
_source = source; |
51 | 1370 |
_ctClass = ctClass; |
52 |
} |
|
53 |
|
|
54 |
/**
|
|
55 |
* Returns the name of the class fabricated by this instance.
|
|
56 |
*/
|
|
57 | 1 |
String getName() |
58 |
{ |
|
59 | 1 |
return _ctClass.getName();
|
60 |
} |
|
61 |
|
|
62 | 2698 |
public void addInterface(Class interfaceClass) |
63 |
{ |
|
64 | 2698 |
CtClass ctInterfaceClass = _source.getCtClass(interfaceClass); |
65 |
|
|
66 | 2698 |
_ctClass.addInterface(ctInterfaceClass); |
67 |
} |
|
68 |
|
|
69 | 3388 |
public void addField(String name, Class type) |
70 |
{ |
|
71 | 3388 |
CtClass ctType = _source.getCtClass(type); |
72 |
|
|
73 | 3388 |
try
|
74 |
{ |
|
75 | 3388 |
CtField field = new CtField(ctType, name, _ctClass);
|
76 | 3388 |
field.setModifiers(Modifier.PRIVATE); |
77 |
|
|
78 | 3388 |
_ctClass.addField(field); |
79 |
} |
|
80 |
catch (CannotCompileException ex)
|
|
81 |
{ |
|
82 | 0 |
throw new ApplicationRuntimeException( |
83 |
ServiceMessages.unableToAddField(name, _ctClass, ex), |
|
84 |
ex); |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 | 6286 |
public MethodFab addMethod(int modifiers, MethodSignature ms, String body) |
89 |
{ |
|
90 | 6286 |
if (_methods.get(ms) != null) |
91 | 1 |
throw new ApplicationRuntimeException(ServiceMessages.duplicateMethodInClass(ms, this)); |
92 |
|
|
93 | 6285 |
CtClass ctReturnType = _source.getCtClass(ms.getReturnType()); |
94 |
|
|
95 | 6285 |
CtClass[] ctParameters = convertClasses(ms.getParameterTypes()); |
96 | 6285 |
CtClass[] ctExceptions = convertClasses(ms.getExceptionTypes()); |
97 |
|
|
98 | 6285 |
CtMethod method = new CtMethod(ctReturnType, ms.getName(), ctParameters, _ctClass);
|
99 |
|
|
100 | 6285 |
try
|
101 |
{ |
|
102 | 6285 |
method.setBody(body); |
103 | 6284 |
method.setModifiers(modifiers); |
104 | 6284 |
method.setExceptionTypes(ctExceptions); |
105 |
|
|
106 | 6284 |
_ctClass.addMethod(method); |
107 |
} |
|
108 |
catch (Exception ex)
|
|
109 |
{ |
|
110 | 1 |
throw new ApplicationRuntimeException( |
111 |
ServiceMessages.unableToAddMethod(ms, _ctClass, ex), |
|
112 |
ex); |
|
113 |
} |
|
114 |
|
|
115 |
// Return a MethodFab so the caller can add catches.
|
|
116 |
|
|
117 | 6284 |
MethodFab result = new MethodFabImpl(_source, ms, method);
|
118 |
|
|
119 | 6284 |
_methods.put(ms, result); |
120 |
|
|
121 | 6284 |
return result;
|
122 |
} |
|
123 |
|
|
124 | 2 |
public MethodFab getMethodFab(MethodSignature ms)
|
125 |
{ |
|
126 | 2 |
return (MethodFab) _methods.get(ms);
|
127 |
} |
|
128 |
|
|
129 | 691 |
public void addConstructor(Class[] parameterTypes, Class[] exceptions, String body) |
130 |
{ |
|
131 | 691 |
CtClass[] ctParameters = convertClasses(parameterTypes); |
132 | 691 |
CtClass[] ctExceptions = convertClasses(exceptions); |
133 |
|
|
134 | 691 |
try
|
135 |
{ |
|
136 | 691 |
CtConstructor constructor = new CtConstructor(ctParameters, _ctClass);
|
137 | 691 |
constructor.setExceptionTypes(ctExceptions); |
138 | 691 |
constructor.setBody(body); |
139 |
|
|
140 | 690 |
_ctClass.addConstructor(constructor); |
141 |
} |
|
142 |
catch (Exception ex)
|
|
143 |
{ |
|
144 | 1 |
throw new ApplicationRuntimeException( |
145 |
ServiceMessages.unableToAddConstructor(_ctClass, ex), |
|
146 |
ex); |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 | 13952 |
private CtClass[] convertClasses(Class[] inputClasses)
|
151 |
{ |
|
152 | 13952 |
if (inputClasses == null || inputClasses.length == 0) |
153 | 11369 |
return null; |
154 |
|
|
155 | 2583 |
int count = inputClasses.length;
|
156 | 2583 |
CtClass[] result = new CtClass[count];
|
157 |
|
|
158 | 2583 |
for (int i = 0; i < count; i++) |
159 |
{ |
|
160 | 6189 |
CtClass ctClass = _source.getCtClass(inputClasses[i]); |
161 |
|
|
162 | 6189 |
result[i] = ctClass; |
163 |
} |
|
164 |
|
|
165 | 2583 |
return result;
|
166 |
} |
|
167 |
|
|
168 | 1364 |
public Class createClass()
|
169 |
{ |
|
170 | 1364 |
return _source.createClass(_ctClass);
|
171 |
} |
|
172 |
|
|
173 |
} |
|
174 |
|
|