KickJava   Java API By Example, From Geeks To Geeks.

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


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

15 package org.apache.hivemind.service.impl;
16
17 import javassist.CtClass;
18 import javassist.NotFoundException;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.service.ClassFabUtils;
22
23 /**
24  * Wrapper around Javassist's {@link javassist.ClassPool} and our own
25  * {@link org.apache.hivemind.service.impl.ClassFactoryClassLoader} that manages the creation of new
26  * instance of {@link javassist.CtClass} and converts finished CtClass's into instantiable Classes.
27  *
28  * @author Howard Lewis Ship
29  */

30 public class CtClassSource
31 {
32     private HiveMindClassPool _pool;
33
34     private int _createdClassCount = 0;
35
36     /**
37      * Returns the number of classes (and interfaces) created by this source.
38      *
39      * @see #createClass(CtClass)
40      * @return the count
41      * @since 1.2
42      */

43     public int getCreatedClassCount()
44     {
45         return _createdClassCount;
46     }
47
48     public CtClassSource(HiveMindClassPool pool)
49     {
50         _pool = pool;
51     }
52
53     public boolean canConvert(Class JavaDoc searchClass)
54     {
55         ensureClassLoaderAvailable(searchClass);
56         String JavaDoc name = ClassFabUtils.getJavaClassName(searchClass);
57         try
58         {
59             _pool.get(name);
60             return true;
61         }
62         catch (NotFoundException ex)
63         {
64             return false;
65         }
66     }
67     
68     public CtClass getCtClass(Class JavaDoc searchClass)
69     {
70         ensureClassLoaderAvailable(searchClass);
71
72         String JavaDoc name = ClassFabUtils.getJavaClassName(searchClass);
73
74         try
75         {
76             return _pool.get(name);
77         }
78         catch (NotFoundException ex)
79         {
80             throw new ApplicationRuntimeException(ServiceMessages.unableToLookupClass(name, ex), ex);
81         }
82     }
83
84     /**
85      * @param searchClass
86      */

87     private void ensureClassLoaderAvailable(Class JavaDoc searchClass) {
88         ClassLoader JavaDoc loader = searchClass.getClassLoader();
89
90         // Add the class loader for the searchClass to the class pool and
91
// delegating class loader if needed.
92

93         _pool.appendClassLoader(loader);
94     }
95
96     public CtClass newClass(String JavaDoc name, Class JavaDoc superClass)
97     {
98         CtClass ctSuperClass = getCtClass(superClass);
99
100         return _pool.makeClass(name, ctSuperClass);
101     }
102
103     /**
104      * Creates a new, empty interace, with the given name.
105      *
106      * @since 1.1
107      */

108
109     public CtClass newInterface(String JavaDoc name)
110     {
111         return _pool.makeInterface(name);
112     }
113
114     public Class JavaDoc createClass(CtClass ctClass)
115     {
116         // String className = ctClass.getName();
117

118         try
119         {
120             Class JavaDoc result = _pool.toClass(ctClass);
121
122             _createdClassCount++;
123
124             return result;
125         }
126         catch (Throwable JavaDoc ex)
127         {
128             throw new ApplicationRuntimeException(ServiceMessages.unableToWriteClass(ctClass, ex),
129                     ex);
130         }
131     }
132 }
Popular Tags