|
|||||||||||||||||||
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 | |||||||||||||||
CtClassSource.java | - | 93.8% | 100% | 95% |
|
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 javassist.ClassPath;
|
|
18 |
import javassist.ClassPool;
|
|
19 |
import javassist.CtClass;
|
|
20 |
import javassist.LoaderClassPath;
|
|
21 |
import javassist.NotFoundException;
|
|
22 |
|
|
23 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
24 |
import org.apache.hivemind.service.ClassFabUtils;
|
|
25 |
|
|
26 |
/**
|
|
27 |
* Wrapper around Javassist's {@link javassist.ClassPool} and
|
|
28 |
* our own {@link org.apache.hivemind.service.impl.ClassFactoryClassLoader}
|
|
29 |
* that manages the creation of new instance of {@link javassist.CtClass}
|
|
30 |
* and converts finished CtClass's into instantiable Classes.
|
|
31 |
*
|
|
32 |
* @author Howard Lewis Ship
|
|
33 |
*/
|
|
34 |
public class CtClassSource |
|
35 |
{ |
|
36 |
private ClassPool _pool;
|
|
37 |
private ClassFactoryClassLoader _loader;
|
|
38 |
|
|
39 | 156 |
public CtClassSource(ClassLoader parentLoader)
|
40 |
{ |
|
41 | 156 |
_pool = new ClassPool(null); |
42 |
|
|
43 | 156 |
ClassPath path = new LoaderClassPath(parentLoader);
|
44 |
|
|
45 | 156 |
_pool.appendClassPath(path); |
46 |
|
|
47 | 156 |
_loader = new ClassFactoryClassLoader(parentLoader);
|
48 |
} |
|
49 |
|
|
50 | 19941 |
public CtClass getCtClass(Class searchClass)
|
51 |
{ |
|
52 | 19941 |
String name = ClassFabUtils.getJavaClassName(searchClass); |
53 |
|
|
54 | 19941 |
try
|
55 |
{ |
|
56 | 19941 |
return _pool.get(name);
|
57 |
} |
|
58 |
catch (NotFoundException ex)
|
|
59 |
{ |
|
60 | 0 |
throw new ApplicationRuntimeException( |
61 |
ServiceMessages.unableToLookupClass(name, ex), |
|
62 |
ex); |
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 | 1369 |
public CtClass newClass(String name, Class superClass)
|
67 |
{ |
|
68 | 1369 |
CtClass ctSuperClass = getCtClass(superClass); |
69 |
|
|
70 | 1369 |
return _pool.makeClass(name, ctSuperClass);
|
71 |
} |
|
72 |
|
|
73 | 1363 |
public Class createClass(CtClass ctClass)
|
74 |
{ |
|
75 | 1363 |
String className = ctClass.getName(); |
76 |
|
|
77 | 1363 |
try
|
78 |
{ |
|
79 | 1363 |
_pool.write(className); |
80 |
|
|
81 | 1362 |
byte[] bytecode = _pool.write(className);
|
82 |
|
|
83 | 1362 |
return _loader.loadClass(className, bytecode);
|
84 |
} |
|
85 |
catch (Throwable ex)
|
|
86 |
{ |
|
87 | 3 |
throw new ApplicationRuntimeException( |
88 |
ServiceMessages.unableToWriteClass(ctClass, ex), |
|
89 |
ex); |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
|
|