Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 156   Methods: 4
NCLOC: 83   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
EnhancedClassValidatorImpl.java 100% 100% 100% 100%
coverage
 1    // Copyright 2004, 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.enhance;
 16   
 17    import java.lang.reflect.Method;
 18    import java.lang.reflect.Modifier;
 19    import java.util.HashMap;
 20    import java.util.HashSet;
 21    import java.util.Iterator;
 22    import java.util.Map;
 23    import java.util.Set;
 24   
 25    import org.apache.hivemind.ErrorLog;
 26    import org.apache.hivemind.Location;
 27    import org.apache.hivemind.service.MethodSignature;
 28    import org.apache.tapestry.spec.IComponentSpecification;
 29   
 30    /**
 31    * Validates that an enhanced class is correct; checks that all inherited abstract methods are, in
 32    * fact, implemented in the class.
 33    *
 34    * @author Howard M. Lewis Ship
 35    * @since 4.0
 36    */
 37    public class EnhancedClassValidatorImpl implements EnhancedClassValidator
 38    {
 39    private ErrorLog _errorLog;
 40   
 41  393 public void validate(Class baseClass, Class enhancedClass, IComponentSpecification specification)
 42    {
 43    // Set of MethodSignatures for methods that have a non-abstract implementation
 44    // The Set is built working from the deepest subclass up to (and including) java.lang.Object
 45   
 46  393 Set implementedMethods = new HashSet();
 47    // Key is MethodSignature, value is Method
 48    // Tracks which methods come from interfaces
 49  393 Map interfaceMethods = new HashMap();
 50   
 51  393 Location location = specification.getLocation();
 52   
 53  393 Class current = enhancedClass;
 54   
 55  393 while (true)
 56    {
 57  1919 addInterfaceMethods(current, interfaceMethods);
 58   
 59    // Inside Eclipse, for abstract classes, getDeclaredMethods() does NOT report methods
 60    // inherited from interfaces. For Sun JDK and abstract classes, getDeclaredMethods()
 61    // DOES report interface methods
 62    // (as if they were declared by the class itself). This code is needlessly complex so
 63    // that the checks work in both
 64    // situations. Basically, I think Eclipse is right and Sun JDK is wrong and we're using
 65    // the interfaceMethods map as a filter to ignore methods that Sun JDK is attributing
 66    // to the class.
 67   
 68  1919 Method[] methods = current.getDeclaredMethods();
 69   
 70  1919 for (int i = 0; i < methods.length; i++)
 71    {
 72  34102 Method m = methods[i];
 73   
 74  34102 MethodSignature s = new MethodSignature(m);
 75   
 76  34102 boolean isAbstract = Modifier.isAbstract(m.getModifiers());
 77   
 78  34102 if (isAbstract)
 79    {
 80  2259 if (interfaceMethods.containsKey(s))
 81  483 continue;
 82   
 83    // If a superclass defines an abstract method that a subclass implements, then
 84    // all's OK.
 85   
 86  1776 if (implementedMethods.contains(s))
 87  1775 continue;
 88   
 89  1 _errorLog.error(EnhanceMessages.noImplForAbstractMethod(
 90    m,
 91    current,
 92    baseClass,
 93    enhancedClass), location, null);
 94    }
 95   
 96  31844 implementedMethods.add(s);
 97    }
 98   
 99  1919 current = current.getSuperclass();
 100   
 101    // No need to check Object.class; it is concrete and doesn't implement any interfaces,
 102    // or provide any methods
 103    // that might be declared in an interface.
 104   
 105  1919 if (current == null || current == Object.class)
 106  393 break;
 107    }
 108   
 109  393 Iterator i = interfaceMethods.entrySet().iterator();
 110  393 while (i.hasNext())
 111    {
 112  18506 Map.Entry entry = (Map.Entry) i.next();
 113   
 114  18506 MethodSignature sig = (MethodSignature) entry.getKey();
 115   
 116  18506 if (implementedMethods.contains(sig))
 117  18505 continue;
 118   
 119  1 Method method = (Method) entry.getValue();
 120   
 121  1 _errorLog.error(EnhanceMessages.unimplementedInterfaceMethod(
 122    method,
 123    baseClass,
 124    enhancedClass), location, null);
 125    }
 126   
 127    }
 128   
 129  1919 private void addInterfaceMethods(Class current, Map interfaceMethods)
 130    {
 131  1919 Class[] interfaces = current.getInterfaces();
 132   
 133  1919 for (int i = 0; i < interfaces.length; i++)
 134  1233 addMethodsFromInterface(interfaces[i], interfaceMethods);
 135    }
 136   
 137  1233 private void addMethodsFromInterface(Class interfaceClass, Map interfaceMethods)
 138    {
 139  1233 Method[] methods = interfaceClass.getMethods();
 140   
 141  1233 for (int i = 0; i < methods.length; i++)
 142    {
 143  32158 MethodSignature sig = new MethodSignature(methods[i]);
 144   
 145  32158 if (interfaceMethods.containsKey(sig))
 146  13652 continue;
 147   
 148  18506 interfaceMethods.put(sig, methods[i]);
 149    }
 150    }
 151   
 152  41 public void setErrorLog(ErrorLog errorLog)
 153    {
 154  41 _errorLog = errorLog;
 155    }
 156    }