001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.hivemind.service.impl; 016 017 import java.util.HashMap; 018 import java.util.Map; 019 020 import javassist.CtClass; 021 022 /** 023 * Common code for {@link org.apache.hivemind.service.impl.ClassFabImpl} and 024 * {@link org.apache.hivemind.service.impl.InterfaceFabImpl}. 025 * 026 * @author Howard M. Lewis Ship 027 * @since 1.1 028 */ 029 public class AbstractFab 030 { 031 032 private final CtClass _ctClass; 033 034 private final CtClassSource _source; 035 036 public AbstractFab(CtClassSource source, CtClass ctClass) 037 { 038 _ctClass = ctClass; 039 _source = source; 040 } 041 042 /** 043 * Map from Class to CtClass. 044 * 045 * @since 1.1 046 */ 047 private Map _ctClassCache = new HashMap(); 048 049 public void addInterface(Class interfaceClass) 050 { 051 CtClass ctInterfaceClass = _source.getCtClass(interfaceClass); 052 053 _ctClass.addInterface(ctInterfaceClass); 054 } 055 056 protected CtClass[] convertClasses(Class[] inputClasses) 057 { 058 if (inputClasses == null || inputClasses.length == 0) 059 return null; 060 061 int count = inputClasses.length; 062 CtClass[] result = new CtClass[count]; 063 064 for (int i = 0; i < count; i++) 065 { 066 CtClass ctClass = convertClass(inputClasses[i]); 067 068 result[i] = ctClass; 069 } 070 071 return result; 072 } 073 074 public boolean canConvert(Class inputClass) 075 { 076 return _source.canConvert(inputClass); 077 } 078 079 /** 080 * @since 1.1 081 */ 082 protected CtClass convertClass(Class inputClass) 083 { 084 CtClass result = (CtClass) _ctClassCache.get(inputClass); 085 086 if (result == null) 087 { 088 result = _source.getCtClass(inputClass); 089 _ctClassCache.put(inputClass, result); 090 } 091 092 return result; 093 } 094 095 public Class createClass() 096 { 097 return _source.createClass(_ctClass); 098 } 099 100 protected CtClass getCtClass() 101 { 102 return _ctClass; 103 } 104 105 protected CtClassSource getSource() 106 { 107 return _source; 108 } 109 110 }