Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 186   Methods: 10
NCLOC: 106   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ListenerMapSourceImpl.java 100% 97.9% 90% 97.2%
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.IPage;
 29    import org.apache.tapestry.engine.ILink;
 30    import org.apache.tapestry.event.ResetEventListener;
 31   
 32    /**
 33    * @author Howard M. Lewis Ship
 34    * @since 4.0
 35    */
 36    public class ListenerMapSourceImpl implements ListenerMapSource, ResetEventListener
 37    {
 38    /**
 39    * Sorts {@link Method}s into descending order by parameter count.
 40    */
 41   
 42    private static class ParameterCountComparator implements Comparator
 43    {
 44  20790 public int compare(Object o1, Object o2)
 45    {
 46  20790 Method m1 = (Method) o1;
 47  20790 Method m2 = (Method) o2;
 48   
 49  20790 return m2.getParameterTypes().length - m1.getParameterTypes().length;
 50    }
 51   
 52    }
 53   
 54    /**
 55    * Keyed on Class, value is a Map. The inner Map is an invoker map ... keyed on listener method
 56    * name, value is {@link org.apache.tapestry.listener.ListenerMethodInvoker}.
 57    */
 58   
 59    private final Map _classToInvokerMap = new HashMap();
 60   
 61  70 public ListenerMap getListenerMapForObject(Object object)
 62    {
 63  70 Defense.notNull(object, "object");
 64   
 65  70 Class objectClass = object.getClass();
 66   
 67  70 Map invokerMap = findInvokerMap(objectClass);
 68   
 69  70 return new ListenerMapImpl(object, invokerMap);
 70    }
 71   
 72  0 public synchronized void resetEventDidOccur()
 73    {
 74  0 _classToInvokerMap.clear();
 75    }
 76   
 77  70 private synchronized Map findInvokerMap(Class targetClass)
 78    {
 79  70 Map result = (Map) _classToInvokerMap.get(targetClass);
 80   
 81  70 if (result == null)
 82    {
 83  66 result = buildInvokerMapForClass(targetClass);
 84  66 _classToInvokerMap.put(targetClass, result);
 85    }
 86   
 87  70 return result;
 88    }
 89   
 90  66 private Map buildInvokerMapForClass(Class targetClass)
 91    {
 92    // map, keyed on method name, value is List of Method
 93    // only methods that return void, return String, or return
 94    // something assignable to IPage are kept.
 95   
 96  66 Map map = new HashMap();
 97   
 98  66 Method[] methods = targetClass.getMethods();
 99   
 100    // Sort all the arrays, just once, and the methods will be
 101    // added to the individual lists in the correct order
 102    // (descending by parameter count).
 103   
 104  66 Arrays.sort(methods, new ParameterCountComparator());
 105   
 106  66 for (int i = 0; i < methods.length; i++)
 107    {
 108  4638 Method m = methods[i];
 109   
 110  4638 if (!isAcceptibleListenerMethodReturnType(m))
 111  1562 continue;
 112   
 113  3076 if (Modifier.isStatic(m.getModifiers()))
 114  16 continue;
 115   
 116  3060 String name = m.getName();
 117   
 118  3060 addMethodToMappedList(map, m, name);
 119    }
 120   
 121  66 return convertMethodListMapToInvokerMap(map);
 122    }
 123   
 124  4650 boolean isAcceptibleListenerMethodReturnType(Method m)
 125    {
 126  4650 Class returnType = m.getReturnType();
 127   
 128  4650 if (returnType == void.class || returnType == String.class)
 129  2970 return true;
 130   
 131  1680 return IPage.class.isAssignableFrom(returnType) || ILink.class.isAssignableFrom(returnType);
 132    }
 133   
 134  66 private Map convertMethodListMapToInvokerMap(Map map)
 135    {
 136  66 Map result = new HashMap();
 137   
 138  66 Iterator i = map.entrySet().iterator();
 139  66 while (i.hasNext())
 140    {
 141  2772 Map.Entry e = (Map.Entry) i.next();
 142   
 143  2772 String name = (String) e.getKey();
 144  2772 List methodList = (List) e.getValue();
 145   
 146  2772 Method[] methods = convertMethodListToArray(methodList);
 147   
 148  2772 ListenerMethodInvoker invoker = createListenerMethodInvoker(name, methods);
 149   
 150  2772 result.put(name, invoker);
 151    }
 152   
 153  66 return result;
 154    }
 155   
 156    /**
 157    * This implementation returns a new {@link ListenerMethodInvoker}. Subclasses can override to
 158    * provide their own implementation.
 159    */
 160   
 161  2772 protected ListenerMethodInvoker createListenerMethodInvoker(String name, Method[] methods)
 162    {
 163  2772 return new ListenerMethodInvokerImpl(name, methods);
 164    }
 165   
 166  2772 private Method[] convertMethodListToArray(List methodList)
 167    {
 168  2772 int size = methodList.size();
 169  2772 Method[] result = new Method[size];
 170   
 171  2772 return (Method[]) methodList.toArray(result);
 172    }
 173   
 174  3060 private void addMethodToMappedList(Map map, Method m, String name)
 175    {
 176  3060 List l = (List) map.get(name);
 177   
 178  3060 if (l == null)
 179    {
 180  2772 l = new ArrayList();
 181  2772 map.put(name, l);
 182    }
 183   
 184  3060 l.add(m);
 185    }
 186    }