Coverage Report - org.apache.tapestry.annotations.ComponentAnnotationWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentAnnotationWorker
100% 
100% 
0
 
 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.annotations;
 16  
 
 17  
 import java.lang.reflect.Method;
 18  
 
 19  
 import org.apache.hivemind.ApplicationRuntimeException;
 20  
 import org.apache.hivemind.HiveMind;
 21  
 import org.apache.hivemind.Location;
 22  
 import org.apache.tapestry.enhance.EnhancementOperation;
 23  
 import org.apache.tapestry.spec.BindingSpecification;
 24  
 import org.apache.tapestry.spec.BindingType;
 25  
 import org.apache.tapestry.spec.ContainedComponent;
 26  
 import org.apache.tapestry.spec.IBindingSpecification;
 27  
 import org.apache.tapestry.spec.IComponentSpecification;
 28  
 import org.apache.tapestry.spec.IContainedComponent;
 29  
 
 30  
 /**
 31  
  * Adds a {@link org.apache.tapestry.spec.IContainedComponent} to the
 32  
  * {@link org.apache.tapestry.spec.IComponentSpecification}.
 33  
  * 
 34  
  * @author Howard Lewis Ship
 35  
  * @since 4.0
 36  
  * @see Component
 37  
  */
 38  12
 public class ComponentAnnotationWorker implements MethodAnnotationEnhancementWorker
 39  
 {
 40  
 
 41  
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
 42  
             Method method, Location location)
 43  
     {
 44  9
         Component component = method.getAnnotation(Component.class);
 45  
 
 46  9
         String propertyName = AnnotationUtils.getPropertyName(method);
 47  9
         String type = component.type();
 48  9
         String copyOf = component.copyOf();
 49  9
         boolean hasCopyOf = HiveMind.isNonBlank(copyOf);
 50  
         
 51  9
         if (hasCopyOf)
 52  
         {
 53  2
             if (HiveMind.isNonBlank(type))
 54  1
                 throw new ApplicationRuntimeException(AnnotationMessages.bothTypeAndCopyOf(propertyName));
 55  1
             type = null;
 56  
         }
 57  
         else
 58  
         {
 59  7
             if (type.equals(""))
 60  
             {
 61  1
                 Class retTypeClazz = method.getReturnType();
 62  1
                 type = resolveComponentType(retTypeClazz);
 63  
             }
 64  7
             copyOf = null;
 65  
         }
 66  
         
 67  8
         IContainedComponent cc = new ContainedComponent();
 68  
 
 69  8
         cc.setInheritInformalParameters(component.inheritInformalParameters());
 70  8
         cc.setType(type);
 71  8
         cc.setCopyOf(copyOf);
 72  8
         cc.setPropertyName(propertyName);
 73  8
         cc.setLocation(location);
 74  
 
 75  14
         for (String binding : component.bindings())
 76  
         {
 77  6
             addBinding(cc, binding, location);
 78  
         }
 79  
 
 80  8
         String id = component.id();
 81  
 
 82  8
         if (id.equals(""))
 83  6
             id = propertyName;
 84  
         
 85  8
         spec.addComponent(id, cc);
 86  
         
 87  8
         if (hasCopyOf)
 88  
         {
 89  1
             IContainedComponent source = spec.getComponent(copyOf);
 90  1
             if (source != null)                
 91  1
                 AnnotationUtils.copyBindings(source, cc);
 92  
         }
 93  8
     }
 94  
     
 95  
     protected String resolveComponentType(Class retTypeClass)
 96  
     {
 97  1
         return retTypeClass.getSimpleName();
 98  
     }
 99  
 
 100  
     void addBinding(IContainedComponent component, String binding, Location location)
 101  
     {
 102  9
         int equalsx = binding.indexOf('=');
 103  
 
 104  9
         if (equalsx < 1)
 105  2
             invalidBinding(binding);
 106  
 
 107  7
         if (equalsx + 1 >= binding.length())
 108  1
             invalidBinding(binding);
 109  
 
 110  6
         String name = binding.substring(0, equalsx).trim();
 111  6
         String value = binding.substring(equalsx + 1).trim();
 112  
 
 113  6
         IBindingSpecification bs = new BindingSpecification();
 114  6
         bs.setType(BindingType.PREFIXED);
 115  6
         bs.setValue(value);
 116  6
         bs.setLocation(location);
 117  
 
 118  6
         component.setBinding(name, bs);
 119  6
     }    
 120  
 
 121  
     protected void invalidBinding(String binding)
 122  
     {
 123  3
         throw new ApplicationRuntimeException(AnnotationMessages.bindingWrongFormat(binding));
 124  
     }
 125  
 }