Clover coverage report - Code Coverage for tapestry-annotations release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:56:46 EDT
file stats: LOC: 83   Methods: 3
NCLOC: 49   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MetaAnnotationWorker.java 83.3% 100% 100% 96.9%
coverage coverage
 1    package org.apache.tapestry.annotations;
 2   
 3    import java.util.ArrayList;
 4    import java.util.Collections;
 5    import java.util.List;
 6   
 7    import org.apache.hivemind.ApplicationRuntimeException;
 8    import org.apache.hivemind.Location;
 9    import org.apache.tapestry.enhance.EnhancementOperation;
 10    import org.apache.tapestry.spec.IComponentSpecification;
 11   
 12    /**
 13    * Recognizes the {@link org.apache.tapestry.annotations.Meta} annotation, and converts it into
 14    * properties on the specification. It is desirable to have meta data in base classes be "merged"
 15    * with meta data from sub classes, with the sub classes overriding any conflicting elements from
 16    * the base class. What we do is work our way up the inheritance tree to java.lang.Object and work
 17    * with each Meta annotation we find.
 18    *
 19    * @author Howard M. Lewis Ship
 20    * @since 4.0
 21    */
 22    public class MetaAnnotationWorker implements ClassAnnotationEnhancementWorker
 23    {
 24   
 25  3 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
 26    Class baseClass, Location location)
 27    {
 28  3 List<Meta> metas = assembleMetas(baseClass);
 29   
 30  3 for (Meta meta : metas)
 31  4 applyPropertiesFromMeta(meta, spec, location);
 32    }
 33   
 34  3 private List<Meta> assembleMetas(Class baseClass)
 35    {
 36  3 Class searchClass = baseClass;
 37  3 List<Meta> result = new ArrayList<Meta>();
 38  3 Meta lastMeta = null;
 39   
 40  3 while (true)
 41    {
 42  7 Meta meta = (Meta) searchClass.getAnnotation(Meta.class);
 43   
 44    // When reach a class that doesn't define or inherit a @Meta, we're done
 45  7 if (meta == null)
 46  3 break;
 47   
 48    // Cheap approach, based on annotation inheritance, for determining
 49    // whether a meta is already in the result list
 50   
 51  4 if (meta != lastMeta)
 52  4 result.add(meta);
 53   
 54  4 searchClass = searchClass.getSuperclass();
 55    }
 56   
 57  3 Collections.reverse(result);
 58   
 59  3 return result;
 60    }
 61   
 62  4 private void applyPropertiesFromMeta(Meta meta, IComponentSpecification spec, Location location)
 63    {
 64  4 String[] pairs = meta.value();
 65   
 66  4 for (String pair : pairs)
 67    {
 68  6 int equalx = pair.indexOf('=');
 69   
 70    // The only big problem is that the location will be the location of the @Meta in
 71    // the subclass, even if the @Meta with a problem is from a base class.
 72   
 73  6 if (equalx < 0)
 74  1 throw new ApplicationRuntimeException(AnnotationMessages.missingEqualsInMeta(pair),
 75    location, null);
 76   
 77  5 String key = pair.substring(0, equalx);
 78  5 String value = pair.substring(equalx + 1);
 79   
 80  5 spec.setProperty(key, value);
 81    }
 82    }
 83    }