Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 174   Methods: 9
NCLOC: 97   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
ListenerMapSourceImpl.java 100% 97.7% 88.9% 96.9%
coverage 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.tapestry.listener;
 16   
 
 17   
 import java.lang.reflect.Method;
 18   
 import java.lang.reflect.Modifier;
 19   
 import java.util.ArrayList;
 20   
 import java.util.Arrays;
 21   
 import java.util.Comparator;
 22   
 import java.util.HashMap;
 23   
 import java.util.Iterator;
 24   
 import java.util.List;
 25   
 import java.util.Map;
 26   
 
 27   
 import org.apache.hivemind.util.Defense;
 28   
 import org.apache.tapestry.event.ResetEventListener;
 29   
 
 30   
 /**
 31   
  * @author Howard M. Lewis Ship
 32   
  * @since 4.0
 33   
  */
 34   
 public class ListenerMapSourceImpl implements ListenerMapSource, ResetEventListener
 35   
 {
 36   
     /**
 37   
      * Sorts {@link Method}s into descending order by parameter count.
 38   
      */
 39   
 
 40   
     private static class ParameterCountComparator implements Comparator
 41   
     {
 42  10105
         public int compare(Object o1, Object o2)
 43   
         {
 44  10105
             Method m1 = (Method) o1;
 45  10105
             Method m2 = (Method) o2;
 46   
 
 47  10105
             return m2.getParameterTypes().length - m1.getParameterTypes().length;
 48   
         }
 49   
 
 50   
     }
 51   
 
 52   
     /**
 53   
      * Keyed on Class, value is a Map. The inner Map is an invoker map ... keyed on listener method
 54   
      * name, value is {@link org.apache.tapestry.listener.ListenerMethodInvoker}.
 55   
      */
 56   
 
 57   
     private final Map _classToInvokerMap = new HashMap();
 58   
 
 59  36
     public ListenerMap getListenerMapForObject(Object object)
 60   
     {
 61  36
         Defense.notNull(object, "object");
 62   
 
 63  36
         Class objectClass = object.getClass();
 64   
 
 65  36
         Map invokerMap = findInvokerMap(objectClass);
 66   
 
 67  36
         return new ListenerMapImpl(object, invokerMap);
 68   
     }
 69   
 
 70  0
     public synchronized void resetEventDidOccur()
 71   
     {
 72  0
         _classToInvokerMap.clear();
 73   
     }
 74   
 
 75  36
     private synchronized Map findInvokerMap(Class targetClass)
 76   
     {
 77  36
         Map result = (Map) _classToInvokerMap.get(targetClass);
 78   
 
 79  36
         if (result == null)
 80   
         {
 81  34
             result = buildInvokerMapForClass(targetClass);
 82  34
             _classToInvokerMap.put(targetClass, result);
 83   
         }
 84   
 
 85  36
         return result;
 86   
     }
 87   
 
 88  34
     private Map buildInvokerMapForClass(Class targetClass)
 89   
     {
 90   
         // map, keyed on method name, value is List of Method
 91   
         // only public void methods go into this map.
 92   
 
 93  34
         Map map = new HashMap();
 94   
 
 95  34
         Method[] methods = targetClass.getMethods();
 96   
 
 97   
         // Sort all the arrays, just once, and the methods will be
 98   
         // added to the individual lists in the correct order
 99   
         // (descending by parameter count).
 100   
 
 101  34
         Arrays.sort(methods, new ParameterCountComparator());
 102   
 
 103  34
         for (int i = 0; i < methods.length; i++)
 104   
         {
 105  2357
             Method m = methods[i];
 106   
 
 107  2357
             if (m.getReturnType() != void.class)
 108  1037
                 continue;
 109   
 
 110  1320
             if (Modifier.isStatic(m.getModifiers()))
 111  8
                 continue;
 112   
 
 113  1312
             String name = m.getName();
 114   
 
 115  1312
             addMethodToMappedList(map, m, name);
 116   
         }
 117   
 
 118  34
         return convertMethodListMapToInvokerMap(map);
 119   
     }
 120   
 
 121  34
     private Map convertMethodListMapToInvokerMap(Map map)
 122   
     {
 123  34
         Map result = new HashMap();
 124   
 
 125  34
         Iterator i = map.entrySet().iterator();
 126  34
         while (i.hasNext())
 127   
         {
 128  1236
             Map.Entry e = (Map.Entry) i.next();
 129   
 
 130  1236
             String name = (String) e.getKey();
 131  1236
             List methodList = (List) e.getValue();
 132   
 
 133  1236
             Method[] methods = convertMethodListToArray(methodList);
 134   
 
 135  1236
             ListenerMethodInvoker invoker = createListenerMethodInvoker(name, methods);
 136   
 
 137  1236
             result.put(name, invoker);
 138   
         }
 139   
 
 140  34
         return result;
 141   
     }
 142   
 
 143   
     /**
 144   
      * This implementation returns a new
 145   
      * {@link org.apache.tapestry.listener.ListenerMethodInvokerImpl}. Subclasses can override to
 146   
      * provide their own implementation.
 147   
      */
 148   
 
 149  1236
     protected ListenerMethodInvokerImpl createListenerMethodInvoker(String name, Method[] methods)
 150   
     {
 151  1236
         return new ListenerMethodInvokerImpl(name, methods);
 152   
     }
 153   
 
 154  1236
     private Method[] convertMethodListToArray(List methodList)
 155   
     {
 156  1236
         int size = methodList.size();
 157  1236
         Method[] result = new Method[size];
 158   
 
 159  1236
         return (Method[]) methodList.toArray(result);
 160   
     }
 161   
 
 162  1312
     private void addMethodToMappedList(Map map, Method m, String name)
 163   
     {
 164  1312
         List l = (List) map.get(name);
 165   
 
 166  1312
         if (l == null)
 167   
         {
 168  1236
             l = new ArrayList();
 169  1236
             map.put(name, l);
 170   
         }
 171   
 
 172  1312
         l.add(m);
 173   
     }
 174   
 }