1 15 package org.apache.hivemind.service.impl; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import javassist.CtClass; 23 import javassist.CtMethod; 24 25 import org.apache.hivemind.ApplicationRuntimeException; 26 import org.apache.hivemind.service.InterfaceFab; 27 import org.apache.hivemind.service.MethodSignature; 28 29 32 public class InterfaceFabImpl extends AbstractFab implements InterfaceFab 33 { 34 private List _methods = new ArrayList (); 35 36 public InterfaceFabImpl(CtClassSource source, CtClass ctClass) 37 { 38 super(source, ctClass); 39 } 40 41 public String toString() 42 { 43 StringBuffer buffer = new StringBuffer ("InterfaceFabImpl[\npublic interface "); 44 45 CtClass ctClass = getCtClass(); 46 47 buffer.append(ctClass.getName()); 48 49 try 50 { 51 CtClass[] interfaces = ctClass.getInterfaces(); 52 53 for (int i = 0; i < interfaces.length; i++) 54 { 55 buffer.append(i == 0 ? " extends " : ", "); 56 buffer.append(interfaces[i].getName()); 57 } 58 59 } 60 catch (Exception ex) 61 { 62 buffer.append("<Exception: " + ex + ">"); 63 } 64 65 Iterator i = _methods.iterator(); 66 67 while (i.hasNext()) 68 { 69 MethodSignature sig = (MethodSignature) i.next(); 70 71 buffer.append("\n\npublic "); 72 buffer.append(sig); 73 buffer.append(";"); 74 } 75 76 buffer.append("\n]"); 77 78 return buffer.toString(); 79 } 80 81 public void addMethod(MethodSignature ms) 82 { 83 CtClass ctReturnType = convertClass(ms.getReturnType()); 84 CtClass[] ctParameters = convertClasses(ms.getParameterTypes()); 85 CtClass[] ctExceptions = convertClasses(ms.getExceptionTypes()); 86 87 CtMethod method = new CtMethod(ctReturnType, ms.getName(), ctParameters, getCtClass()); 88 89 try 90 { 91 method.setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT); 92 method.setExceptionTypes(ctExceptions); 93 94 getCtClass().addMethod(method); 95 } 96 catch (Exception ex) 97 { 98 throw new ApplicationRuntimeException(ServiceMessages.unableToAddMethod( 99 ms, 100 getCtClass(), 101 ex), ex); 102 } 103 104 _methods.add(ms); 105 } 106 107 public Class createInterface() 108 { 109 return createClass(); 110 } 111 112 } | Popular Tags |