Clover coverage report - Code Coverage for tapestry release 4.0-beta-9
Coverage timestamp: Sat Oct 1 2005 08:36:20 EDT
file stats: LOC: 187   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  10005 public int compare(Object o1, Object o2)
 45    {
 46  10005 Method m1 = (Method) o1;
 47  10005 Method m2 = (Method) o2;
 48   
 49  10005 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  35 public ListenerMap getListenerMapForObject(Object object)
 62    {
 63  35 Defense.notNull(object, "object");
 64   
 65  35 Class objectClass = object.getClass();
 66   
 67  35 Map invokerMap = findInvokerMap(objectClass);
 68   
 69  35 return new ListenerMapImpl(object, invokerMap);
 70    }
 71   
 72  0 public synchronized void resetEventDidOccur()
 73    {
 74  0 _classToInvokerMap.clear();
 75    }
 76   
 77  35 private synchronized Map findInvokerMap(Class targetClass)
 78    {
 79  35 Map result = (Map) _classToInvokerMap.get(targetClass);
 80   
 81  35 if (result == null)
 82    {
 83  33 result = buildInvokerMapForClass(targetClass);
 84  33 _classToInvokerMap.put(targetClass, result);
 85    }
 86   
 87  35 return result;
 88    }
 89   
 90  33 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  33 Map map = new HashMap();
 97   
 98  33 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  33 Arrays.sort(methods, new ParameterCountComparator());
 105   
 106  33 for (int i = 0; i < methods.length; i++)
 107    {
 108  2298 Method m = methods[i];
 109   
 110  2298 if (!isAcceptibleListenerMethodReturnType(m))
 111  781 continue;
 112   
 113  1517 if (Modifier.isStatic(m.getModifiers()))
 114  8 continue;
 115   
 116  1509 String name = m.getName();
 117   
 118  1509 addMethodToMappedList(map, m, name);
 119    }
 120   
 121  33 return convertMethodListMapToInvokerMap(map);
 122    }
 123   
 124  2304 boolean isAcceptibleListenerMethodReturnType(Method m)
 125    {
 126  2304 Class returnType = m.getReturnType();
 127   
 128  2304 if (returnType == void.class || returnType == String.class)
 129  1464 return true;
 130   
 131  840 return IPage.class.isAssignableFrom(returnType) || ILink.class.isAssignableFrom(returnType);
 132    }
 133   
 134  33 private Map convertMethodListMapToInvokerMap(Map map)
 135    {
 136  33 Map result = new HashMap();
 137   
 138  33 Iterator i = map.entrySet().iterator();
 139  33 while (i.hasNext())
 140    {
 141  1365 Map.Entry e = (Map.Entry) i.next();
 142   
 143  1365 String name = (String) e.getKey();
 144  1365 List methodList = (List) e.getValue();
 145   
 146  1365 Method[] methods = convertMethodListToArray(methodList);
 147   
 148  1365 ListenerMethodInvoker invoker = createListenerMethodInvoker(name, methods);
 149   
 150  1365 result.put(name, invoker);
 151    }
 152   
 153  33 return result;
 154    }
 155   
 156    /**
 157    * This implementation returns a new
 158    * {@link org.apache.tapestry.listener.ListenerMethodInvokerImpl}. Subclasses can override to
 159    * provide their own implementation.
 160    */
 161   
 162  1365 protected ListenerMethodInvokerImpl createListenerMethodInvoker(String name, Method[] methods)
 163    {
 164  1365 return new ListenerMethodInvokerImpl(name, methods);
 165    }
 166   
 167  1365 private Method[] convertMethodListToArray(List methodList)
 168    {
 169  1365 int size = methodList.size();
 170  1365 Method[] result = new Method[size];
 171   
 172  1365 return (Method[]) methodList.toArray(result);
 173    }
 174   
 175  1509 private void addMethodToMappedList(Map map, Method m, String name)
 176    {
 177  1509 List l = (List) map.get(name);
 178   
 179  1509 if (l == null)
 180    {
 181  1365 l = new ArrayList();
 182  1365 map.put(name, l);
 183    }
 184   
 185  1509 l.add(m);
 186    }
 187    }