Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CtClassSource |
|
| 1.8571428571428572;1.857 |
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 | package org.apache.tapestry.enhance; | |
15 | ||
16 | import javassist.CtClass; | |
17 | import javassist.NotFoundException; | |
18 | import org.apache.hivemind.ApplicationRuntimeException; | |
19 | import org.apache.hivemind.service.ClassFabUtils; | |
20 | ||
21 | /** | |
22 | * Wrapper around Javassist's {@link javassist.ClassPool} and our own | |
23 | * {@link org.apache.hivemind.service.impl.ClassFactoryClassLoader} that manages the creation of new | |
24 | * instance of {@link javassist.CtClass} and converts finished CtClass's into instantiable Classes. | |
25 | * | |
26 | * @author Howard Lewis Ship | |
27 | */ | |
28 | public class CtClassSource | |
29 | { | |
30 | private HiveMindClassPool _pool; | |
31 | ||
32 | public CtClassSource(HiveMindClassPool pool) | |
33 | 0 | { |
34 | 0 | _pool = pool; |
35 | 0 | } |
36 | ||
37 | public CtClass getCtClass(Class searchClass) | |
38 | { | |
39 | 0 | ClassLoader loader = searchClass.getClassLoader(); |
40 | ||
41 | // Add the class loader for the searchClass to the class pool and | |
42 | // delegating class loader if needed. | |
43 | ||
44 | 0 | _pool.appendClassLoader(loader); |
45 | ||
46 | 0 | String name = ClassFabUtils.getJavaClassName(searchClass); |
47 | ||
48 | try | |
49 | { | |
50 | 0 | return _pool.get(name); |
51 | } | |
52 | 0 | catch (NotFoundException ex) |
53 | { | |
54 | 0 | throw new ApplicationRuntimeException(EnhanceMessages.unableToLookupClass(name, ex), ex); |
55 | } | |
56 | } | |
57 | ||
58 | public CtClass newClass(String name, Class superClass) | |
59 | { | |
60 | 0 | CtClass ctSuperClass = getCtClass(superClass); |
61 | ||
62 | 0 | return _pool.makeClass(name, ctSuperClass); |
63 | } | |
64 | ||
65 | /** | |
66 | * Creates a new, empty interace, with the given name. | |
67 | * | |
68 | * @since 1.1 | |
69 | */ | |
70 | ||
71 | public CtClass newInterface(String name) | |
72 | { | |
73 | 0 | return _pool.makeInterface(name); |
74 | } | |
75 | ||
76 | public Class createClass(CtClass ctClass) | |
77 | { | |
78 | 0 | return createClass(ctClass, false); |
79 | } | |
80 | ||
81 | public Class createClass(CtClass ctClass, boolean detach) | |
82 | { | |
83 | try | |
84 | { | |
85 | 0 | return _pool.toClass(ctClass, detach); |
86 | } | |
87 | 0 | catch (Throwable ex) |
88 | { | |
89 | 0 | throw new ApplicationRuntimeException(EnhanceMessages.unableToWriteClass(ctClass, ex), ex); |
90 | } | |
91 | } | |
92 | ||
93 | public void setPool(HiveMindClassPool pool) | |
94 | { | |
95 | 0 | _pool = pool; |
96 | 0 | } |
97 | } |