1 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 30 public class CtClassSource 31 { 32 private HiveMindClassPool _pool; 33 34 private int _createdClassCount = 0; 35 36 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 searchClass) 54 { 55 ensureClassLoaderAvailable(searchClass); 56 String 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 searchClass) 69 { 70 ensureClassLoaderAvailable(searchClass); 71 72 String 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 87 private void ensureClassLoaderAvailable(Class searchClass) { 88 ClassLoader loader = searchClass.getClassLoader(); 89 90 93 _pool.appendClassLoader(loader); 94 } 95 96 public CtClass newClass(String name, Class superClass) 97 { 98 CtClass ctSuperClass = getCtClass(superClass); 99 100 return _pool.makeClass(name, ctSuperClass); 101 } 102 103 108 109 public CtClass newInterface(String name) 110 { 111 return _pool.makeInterface(name); 112 } 113 114 public Class createClass(CtClass ctClass) 115 { 116 118 try 119 { 120 Class result = _pool.toClass(ctClass); 121 122 _createdClassCount++; 123 124 return result; 125 } 126 catch (Throwable ex) 127 { 128 throw new ApplicationRuntimeException(ServiceMessages.unableToWriteClass(ctClass, ex), 129 ex); 130 } 131 } 132 } | Popular Tags |