Clover coverage report - Code Coverage for hivemind release 1.1-alpha-3
Coverage timestamp: Tue Mar 22 2005 09:10:26 EST
file stats: LOC: 180   Methods: 11
NCLOC: 118   Classes: 2
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
InterfaceSynthesizerImpl.java 100% 100% 100% 100%
coverage
 1   
 // Copyright 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   
 
 15   
 package org.apache.hivemind.service.impl;
 16   
 
 17   
 import java.lang.reflect.Method;
 18   
 import java.lang.reflect.Modifier;
 19   
 import java.util.ArrayList;
 20   
 import java.util.HashSet;
 21   
 import java.util.Iterator;
 22   
 import java.util.List;
 23   
 import java.util.Set;
 24   
 
 25   
 import org.apache.hivemind.service.ClassFabUtils;
 26   
 import org.apache.hivemind.service.ClassFactory;
 27   
 import org.apache.hivemind.service.InterfaceFab;
 28   
 import org.apache.hivemind.service.InterfaceSynthesizer;
 29   
 import org.apache.hivemind.service.MethodSignature;
 30   
 
 31   
 /**
 32   
  * @author Howard M. Lewis Ship
 33   
  */
 34   
 public class InterfaceSynthesizerImpl implements InterfaceSynthesizer
 35   
 {
 36   
     private ClassFactory _classFactory;
 37   
 
 38   
     private static class Operation
 39   
     {
 40   
         private Set _interfaces = new HashSet();
 41   
 
 42   
         private Set _interfaceMethods = new HashSet();
 43   
 
 44   
         private Set _allMethods = new HashSet();
 45   
 
 46   
         private List _interfaceQueue = new ArrayList();
 47   
 
 48  8
         public Set getInterfaces()
 49   
         {
 50  8
             return _interfaces;
 51   
         }
 52   
 
 53  8
         public Set getNonInterfaceMethodSignatures()
 54   
         {
 55  8
             Set result = new HashSet(_allMethods);
 56   
 
 57  8
             result.removeAll(_interfaceMethods);
 58   
 
 59  8
             return result;
 60   
         }
 61   
 
 62  8
         public void processInterfaceQueue()
 63   
         {
 64  8
             while (!_interfaceQueue.isEmpty())
 65   
             {
 66  4
                 Class interfaceClass = (Class) _interfaceQueue.remove(0);
 67   
 
 68  4
                 processInterface(interfaceClass);
 69   
             }
 70   
         }
 71   
 
 72  4
         private void processInterface(Class interfaceClass)
 73   
         {
 74  4
             Class[] interfaces = interfaceClass.getInterfaces();
 75   
 
 76  4
             for (int i = 0; i < interfaces.length; i++)
 77  1
                 addInterfaceToQueue(interfaces[i]);
 78   
 
 79  4
             Method[] methods = interfaceClass.getDeclaredMethods();
 80   
 
 81  4
             for (int i = 0; i < methods.length; i++)
 82   
             {
 83  4
                 MethodSignature sig = new MethodSignature(methods[i]);
 84   
 
 85  4
                 _interfaceMethods.add(sig);
 86   
             }
 87   
         }
 88   
 
 89  5
         private void addInterfaceToQueue(Class interfaceClass)
 90   
         {
 91  5
             if (_interfaces.contains(interfaceClass))
 92  1
                 return;
 93   
 
 94  4
             _interfaces.add(interfaceClass);
 95  4
             _interfaceQueue.add(interfaceClass);
 96   
         }
 97   
 
 98  9
         public void processClass(Class beanClass)
 99   
         {
 100  9
             Class[] interfaces = beanClass.getInterfaces();
 101   
 
 102  9
             for (int i = 0; i < interfaces.length; i++)
 103  4
                 addInterfaceToQueue(interfaces[i]);
 104   
 
 105  9
             Method[] methods = beanClass.getDeclaredMethods();
 106   
 
 107  9
             for (int i = 0; i < methods.length; i++)
 108   
             {
 109  15
                 Method m = methods[i];
 110  15
                 int modifiers = m.getModifiers();
 111   
 
 112  15
                 if (Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers))
 113  3
                     continue;
 114   
 
 115  12
                 MethodSignature sig = new MethodSignature(m);
 116   
 
 117  12
                 _allMethods.add(sig);
 118   
             }
 119   
         }
 120   
 
 121   
     }
 122   
 
 123  8
     public Class synthesizeInterface(Class beanClass)
 124   
     {
 125  8
         Operation op = new Operation();
 126   
 
 127  8
         explodeClass(beanClass, op);
 128   
 
 129  8
         return createInterface(beanClass, op);
 130   
     }
 131   
 
 132  8
     void explodeClass(Class beanClass, Operation op)
 133   
     {
 134  8
         Class current = beanClass;
 135   
 
 136  8
         while (current != Object.class)
 137   
         {
 138  9
             op.processClass(current);
 139   
 
 140  9
             current = current.getSuperclass();
 141   
         }
 142   
 
 143  8
         op.processInterfaceQueue();
 144   
     }
 145   
 
 146  8
     Class createInterface(Class beanClass, Operation op)
 147   
     {
 148  8
         String name = ClassFabUtils.generateClassName(beanClass);
 149   
 
 150  8
         return createInterface(name, op);
 151   
     }
 152   
 
 153  8
     private Class createInterface(String name, Operation op)
 154   
     {
 155  8
         InterfaceFab fab = _classFactory.newInterface(name);
 156   
 
 157  8
         Iterator i = op.getInterfaces().iterator();
 158  8
         while (i.hasNext())
 159   
         {
 160  4
             Class interfaceClass = (Class) i.next();
 161   
 
 162  4
             fab.addInterface(interfaceClass);
 163   
         }
 164   
 
 165  8
         i = op.getNonInterfaceMethodSignatures().iterator();
 166  8
         while (i.hasNext())
 167   
         {
 168  8
             MethodSignature sig = (MethodSignature) i.next();
 169   
 
 170  8
             fab.addMethod(sig);
 171   
         }
 172   
 
 173  8
         return fab.createInterface();
 174   
     }
 175   
 
 176  8
     public void setClassFactory(ClassFactory classFactory)
 177   
     {
 178  8
         _classFactory = classFactory;
 179   
     }
 180   
 }