Clover coverage report - Code Coverage for hivemind release 1.0-rc-2
Coverage timestamp: Sat Sep 11 2004 09:09:48 EDT
file stats: LOC: 174   Methods: 9
NCLOC: 113   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ClassFabImpl.java 100% 97.7% 100% 98.3%
coverage coverage
 1   
 //  Copyright 2004 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   
 
 15   
 package org.apache.hivemind.service.impl;
 16   
 
 17   
 import java.lang.reflect.Modifier;
 18   
 import java.util.HashMap;
 19   
 import java.util.Map;
 20   
 
 21   
 import javassist.CannotCompileException;
 22   
 import javassist.CtClass;
 23   
 import javassist.CtConstructor;
 24   
 import javassist.CtField;
 25   
 import javassist.CtMethod;
 26   
 
 27   
 import org.apache.hivemind.ApplicationRuntimeException;
 28   
 import org.apache.hivemind.service.ClassFab;
 29   
 import org.apache.hivemind.service.MethodFab;
 30   
 import org.apache.hivemind.service.MethodSignature;
 31   
 
 32   
 /**
 33   
  * Implementation of {@link org.apache.hivemind.service.ClassFab}. Hides,
 34   
  * as much as possible, the underlying library (Javassist).
 35   
  *
 36   
  * @author Howard Lewis Ship
 37   
  */
 38   
 public class ClassFabImpl implements ClassFab
 39   
 {
 40   
     private CtClass _ctClass;
 41   
     private CtClassSource _source;
 42   
 
 43   
     /**
 44   
      * Map of {@link MethodFab} keyed on {@link MethodSignature}.
 45   
      */
 46   
     private Map _methods = new HashMap();
 47   
 
 48  1369
     public ClassFabImpl(CtClassSource source, CtClass ctClass)
 49   
     {
 50  1369
         _source = source;
 51  1369
         _ctClass = ctClass;
 52   
     }
 53   
 
 54   
     /**
 55   
      * Returns the name of the class fabricated by this instance.
 56   
      */
 57  1
     String getName()
 58   
     {
 59  1
         return _ctClass.getName();
 60   
     }
 61   
 
 62  2697
     public void addInterface(Class interfaceClass)
 63   
     {
 64  2697
         CtClass ctInterfaceClass = _source.getCtClass(interfaceClass);
 65   
 
 66  2697
         _ctClass.addInterface(ctInterfaceClass);
 67   
     }
 68   
 
 69  3386
     public void addField(String name, Class type)
 70   
     {
 71  3386
         CtClass ctType = _source.getCtClass(type);
 72   
 
 73  3386
         try
 74   
         {
 75  3386
             CtField field = new CtField(ctType, name, _ctClass);
 76  3386
             field.setModifiers(Modifier.PRIVATE);
 77   
 
 78  3386
             _ctClass.addField(field);
 79   
         }
 80   
         catch (CannotCompileException ex)
 81   
         {
 82  0
             throw new ApplicationRuntimeException(
 83   
                 ServiceMessages.unableToAddField(name, _ctClass, ex),
 84   
                 ex);
 85   
         }
 86   
     }
 87   
 
 88  6284
     public MethodFab addMethod(int modifiers, MethodSignature ms, String body)
 89   
     {
 90  6284
         if (_methods.get(ms) != null)
 91  1
             throw new ApplicationRuntimeException(ServiceMessages.duplicateMethodInClass(ms, this));
 92   
 
 93  6283
         CtClass ctReturnType = _source.getCtClass(ms.getReturnType());
 94   
 
 95  6283
         CtClass[] ctParameters = convertClasses(ms.getParameterTypes());
 96  6283
         CtClass[] ctExceptions = convertClasses(ms.getExceptionTypes());
 97   
 
 98  6283
         CtMethod method = new CtMethod(ctReturnType, ms.getName(), ctParameters, _ctClass);
 99   
 
 100  6283
         try
 101   
         {
 102  6283
             method.setBody(body);
 103  6282
             method.setModifiers(modifiers);
 104  6282
             method.setExceptionTypes(ctExceptions);
 105   
 
 106  6282
             _ctClass.addMethod(method);
 107   
         }
 108   
         catch (Exception ex)
 109   
         {
 110  1
             throw new ApplicationRuntimeException(
 111   
                 ServiceMessages.unableToAddMethod(ms, _ctClass, ex),
 112   
                 ex);
 113   
         }
 114   
 
 115   
         // Return a MethodFab so the caller can add catches.
 116   
 
 117  6282
         MethodFab result = new MethodFabImpl(_source, ms, method);
 118   
 
 119  6282
         _methods.put(ms, result);
 120   
 
 121  6282
         return result;
 122   
     }
 123   
 
 124  2
     public MethodFab getMethodFab(MethodSignature ms)
 125   
     {
 126  2
         return (MethodFab) _methods.get(ms);
 127   
     }
 128   
 
 129  690
     public void addConstructor(Class[] parameterTypes, Class[] exceptions, String body)
 130   
     {
 131  690
         CtClass[] ctParameters = convertClasses(parameterTypes);
 132  690
         CtClass[] ctExceptions = convertClasses(exceptions);
 133   
 
 134  690
         try
 135   
         {
 136  690
             CtConstructor constructor = new CtConstructor(ctParameters, _ctClass);
 137  690
             constructor.setExceptionTypes(ctExceptions);
 138  690
             constructor.setBody(body);
 139   
 
 140  689
             _ctClass.addConstructor(constructor);
 141   
         }
 142   
         catch (Exception ex)
 143   
         {
 144  1
             throw new ApplicationRuntimeException(
 145   
                 ServiceMessages.unableToAddConstructor(_ctClass, ex),
 146   
                 ex);
 147   
         }
 148   
     }
 149   
 
 150  13946
     private CtClass[] convertClasses(Class[] inputClasses)
 151   
     {
 152  13946
         if (inputClasses == null || inputClasses.length == 0)
 153  11364
             return null;
 154   
 
 155  2582
         int count = inputClasses.length;
 156  2582
         CtClass[] result = new CtClass[count];
 157   
 
 158  2582
         for (int i = 0; i < count; i++)
 159   
         {
 160  6187
             CtClass ctClass = _source.getCtClass(inputClasses[i]);
 161   
 
 162  6187
             result[i] = ctClass;
 163   
         }
 164   
 
 165  2582
         return result;
 166   
     }
 167   
 
 168  1363
     public Class createClass()
 169   
     {
 170  1363
         return _source.createClass(_ctClass);
 171   
     }
 172   
 
 173   
 }
 174