Clover coverage report - Code Coverage for hivemind-lib release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:42:02 EDT
file stats: LOC: 142   Methods: 5
NCLOC: 93   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
DefaultImplementationBuilderImpl.java 100% 97.2% 100% 98.2%
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.lib.impl;
 16   
 
 17   
 import java.lang.reflect.Method;
 18   
 import java.lang.reflect.Modifier;
 19   
 import java.util.Collections;
 20   
 import java.util.HashMap;
 21   
 import java.util.Map;
 22   
 
 23   
 import org.apache.hivemind.ApplicationRuntimeException;
 24   
 import org.apache.hivemind.impl.BaseLocatable;
 25   
 import org.apache.hivemind.internal.Module;
 26   
 import org.apache.hivemind.lib.DefaultImplementationBuilder;
 27   
 import org.apache.hivemind.service.ClassFab;
 28   
 import org.apache.hivemind.service.ClassFabUtils;
 29   
 import org.apache.hivemind.service.ClassFactory;
 30   
 import org.apache.hivemind.service.MethodSignature;
 31   
 
 32   
 /**
 33   
  * Implemenation of {@link org.apache.hivemind.lib.DefaultImplementationBuilder}.
 34   
  *
 35   
  * @author Howard Lewis Ship
 36   
  */
 37   
 public class DefaultImplementationBuilderImpl
 38   
     extends BaseLocatable
 39   
     implements DefaultImplementationBuilder
 40   
 {
 41   
     private Map _instances = Collections.synchronizedMap(new HashMap());
 42   
 
 43   
     private ClassFactory _classFactory;
 44   
 
 45  10
     public Object buildDefaultImplementation(Class interfaceType, Module module)
 46   
     {
 47  10
         Object result = _instances.get(interfaceType);
 48   
 
 49  10
         if (result == null)
 50   
         {
 51  9
             result = create(interfaceType, module);
 52  8
             _instances.put(interfaceType, result);
 53   
         }
 54   
 
 55  9
         return result;
 56   
     }
 57   
 
 58  9
     private Object create(Class interfaceType, Module module)
 59   
     {
 60  9
         Class defaultClass = createClass(interfaceType, module);
 61   
 
 62  8
         try
 63   
         {
 64  8
             return defaultClass.newInstance();
 65   
         }
 66   
         catch (Exception ex)
 67   
         {
 68  0
             throw new ApplicationRuntimeException(
 69   
                 ImplMessages.unableToCreateDefaultImplementation(interfaceType, ex),
 70   
                 ex);
 71   
         }
 72   
     }
 73   
 
 74  9
     private Class createClass(Class interfaceType, Module module)
 75   
     {
 76  9
         if (!interfaceType.isInterface())
 77  1
             throw new ApplicationRuntimeException(ImplMessages.notAnInterface(interfaceType));
 78   
 
 79  8
         String name = ClassFabUtils.generateClassName("DefaultImpl");
 80   
 
 81  8
         ClassFab cf = _classFactory.newClass(name, Object.class, module);
 82   
 
 83  8
         cf.addInterface(interfaceType);
 84   
 
 85  8
         boolean toString = false;
 86   
 
 87  8
         Method[] methods = interfaceType.getMethods();
 88   
 
 89  8
         for (int i = 0; i < methods.length; i++)
 90   
         {
 91  10
             Method m = methods[i];
 92   
 
 93  10
             toString |= ClassFabUtils.isToString(m);
 94   
 
 95  10
             addMethod(cf, m);
 96   
         }
 97   
 
 98  8
         if (!toString)
 99  7
             ClassFabUtils.addToStringMethod(
 100   
                 cf,
 101   
                 ImplMessages.defaultImplementationDescription(interfaceType));
 102   
 
 103  8
         return cf.createClass();
 104   
     }
 105   
 
 106  10
     private void addMethod(ClassFab cf, Method m)
 107   
     {
 108  10
         StringBuffer body = new StringBuffer("{ ");
 109   
 
 110  10
         Class returnType = m.getReturnType();
 111   
 
 112  10
         if (returnType != void.class)
 113   
         {
 114  5
             body.append("return ");
 115   
 
 116  5
             if (returnType.isPrimitive())
 117   
             {
 118  3
                 if (returnType == boolean.class)
 119  1
                     body.append("false");
 120   
                 else
 121  2
                     body.append("0");
 122   
             }
 123   
             else
 124   
             {
 125  2
                 body.append("null");
 126   
             }
 127   
 
 128  5
             body.append(";");
 129   
         }
 130   
 
 131  10
         body.append(" }");
 132   
 
 133  10
         cf.addMethod(Modifier.PUBLIC, new MethodSignature(m), body.toString());
 134   
     }
 135   
 
 136  14
     public void setClassFactory(ClassFactory factory)
 137   
     {
 138  14
         _classFactory = factory;
 139   
     }
 140   
 
 141   
 }
 142