KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 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 java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javassist.CtClass;
21
22 /**
23  * Common code for {@link org.apache.hivemind.service.impl.ClassFabImpl} and
24  * {@link org.apache.hivemind.service.impl.InterfaceFabImpl}.
25  *
26  * @author Howard M. Lewis Ship
27  * @since 1.1
28  */

29 public class AbstractFab
30 {
31
32     private final CtClass _ctClass;
33
34     private final CtClassSource _source;
35
36     public AbstractFab(CtClassSource source, CtClass ctClass)
37     {
38         _ctClass = ctClass;
39         _source = source;
40     }
41
42     /**
43      * Map from Class to CtClass.
44      *
45      * @since 1.1
46      */

47     private Map JavaDoc _ctClassCache = new HashMap JavaDoc();
48
49     public void addInterface(Class JavaDoc interfaceClass)
50     {
51         CtClass ctInterfaceClass = _source.getCtClass(interfaceClass);
52
53         _ctClass.addInterface(ctInterfaceClass);
54     }
55
56     protected CtClass[] convertClasses(Class JavaDoc[] inputClasses)
57     {
58         if (inputClasses == null || inputClasses.length == 0)
59             return null;
60
61         int count = inputClasses.length;
62         CtClass[] result = new CtClass[count];
63
64         for (int i = 0; i < count; i++)
65         {
66             CtClass ctClass = convertClass(inputClasses[i]);
67
68             result[i] = ctClass;
69         }
70
71         return result;
72     }
73
74     public boolean canConvert(Class JavaDoc inputClass)
75     {
76         return _source.canConvert(inputClass);
77     }
78     
79     /**
80      * @since 1.1
81      */

82     protected CtClass convertClass(Class JavaDoc inputClass)
83     {
84         CtClass result = (CtClass) _ctClassCache.get(inputClass);
85
86         if (result == null)
87         {
88             result = _source.getCtClass(inputClass);
89             _ctClassCache.put(inputClass, result);
90         }
91
92         return result;
93     }
94
95     public Class JavaDoc createClass()
96     {
97         return _source.createClass(_ctClass);
98     }
99
100     protected CtClass getCtClass()
101     {
102         return _ctClass;
103     }
104
105     protected CtClassSource getSource()
106     {
107         return _source;
108     }
109
110 }
Popular Tags