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