1 15 package org.apache.hivemind.service.impl; 16 17 import java.lang.reflect.Modifier ; 18 19 import javassist.CtClass; 20 import javassist.CtMethod; 21 22 import org.apache.hivemind.ApplicationRuntimeException; 23 import org.apache.hivemind.service.MethodFab; 24 import org.apache.hivemind.service.MethodSignature; 25 26 33 class MethodFabImpl implements MethodFab 34 { 35 private CtClassSource _source; 36 37 private MethodSignature _signature; 38 39 private CtMethod _method; 40 41 private StringBuffer _descriptionBody = new StringBuffer (); 42 43 public MethodFabImpl(CtClassSource source, MethodSignature signature, CtMethod method, 44 String body) 45 { 46 _source = source; 47 _signature = signature; 48 _method = method; 49 50 _descriptionBody.append(body); 51 } 52 53 56 public String toString() 57 { 58 StringBuffer buffer = new StringBuffer (); 59 60 try 61 { 62 buffer.append(Modifier.toString(_method.getModifiers())); 63 64 buffer.append(" "); 65 buffer.append(_method.getReturnType().getName()); 66 67 buffer.append(" "); 68 buffer.append(_method.getName()); 69 buffer.append("("); 70 71 CtClass[] params = _method.getParameterTypes(); 72 73 for (int i = 0; i < params.length; i++) 74 { 75 if (i > 0) 76 buffer.append(", "); 77 78 buffer.append(params[i].getName()); 79 80 buffer.append(" $"); 81 buffer.append(i + 1); 82 } 83 buffer.append(")"); 84 85 CtClass[] exceptions = _method.getExceptionTypes(); 86 87 for (int i = 0; i < exceptions.length; i++) 88 { 89 if (i == 0) 90 buffer.append("\n throws "); 91 else 92 buffer.append(", "); 93 94 buffer.append(exceptions[i].getName()); 95 } 96 97 buffer.append("\n"); 98 buffer.append(_descriptionBody); 99 } 100 catch (Exception ex) 101 { 102 buffer.append(" *** "); 103 buffer.append(ex); 104 } 105 106 return buffer.toString(); 107 } 108 109 public void addCatch(Class exceptionClass, String catchBody) 110 { 111 CtClass ctException = _source.getCtClass(exceptionClass); 112 113 try 114 { 115 _method.addCatch(catchBody, ctException); 116 } 117 catch (Exception ex) 118 { 119 throw new ApplicationRuntimeException(ServiceMessages.unableToAddCatch( 120 exceptionClass, 121 _method, 122 ex), ex); 123 } 124 125 _descriptionBody.append("\ncatch("); 126 _descriptionBody.append(exceptionClass.getName()); 127 _descriptionBody.append(" $e)\n"); 128 _descriptionBody.append(catchBody); 129 } 130 131 public void extend(String body, boolean asFinally) 132 { 133 try 134 { 135 _method.insertAfter(body, asFinally); 136 } 137 catch (Exception ex) 138 { 139 throw new ApplicationRuntimeException(ServiceMessages.unableToExtendMethod( 140 _signature, 141 _method.getDeclaringClass().getName(), 142 ex), ex); 143 } 144 145 _descriptionBody.append("\n"); 146 147 if (asFinally) 148 _descriptionBody.append("finally\n"); 149 150 _descriptionBody.append(body); 151 } 152 153 } | Popular Tags |