Coverage Report - org.apache.tapestry.annotations.AnnotationUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnotationUtils
0%
0/46
0%
0/24
3.857
 
 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.beans.Introspector;
 18  
 import java.lang.annotation.Annotation;
 19  
 import java.lang.reflect.Method;
 20  
 import java.util.Iterator;
 21  
 
 22  
 import org.apache.hivemind.ApplicationRuntimeException;
 23  
 import org.apache.hivemind.Location;
 24  
 import org.apache.hivemind.Resource;
 25  
 import org.apache.tapestry.spec.IBindingSpecification;
 26  
 import org.apache.tapestry.spec.IContainedComponent;
 27  
 import org.apache.tapestry.util.DescribedLocation;
 28  
 
 29  
 /**
 30  
  * @author Howard M. Lewis Ship
 31  
  * @since 4.0
 32  
  */
 33  
 
 34  
 public final class AnnotationUtils
 35  
 {
 36  
     /* defeat instantiation */
 37  0
     private AnnotationUtils() { }
 38  
     
 39  
     /**
 40  
      * Determines the property name for a method, by stripping off the is/get/set prefix and
 41  
      * decapitalizing the first name.
 42  
      * 
 43  
      * @param method
 44  
      *            accessor method (get/set/is)
 45  
      * @return the property name for the method
 46  
      * @throws ApplicationRuntimeException
 47  
      *             if the method is not an accessor or mutator method
 48  
      */
 49  
     public static String getPropertyName(Method method)
 50  
     {
 51  0
         String name = method.getName();
 52  
 
 53  0
         if (name.startsWith("is"))
 54  
         {
 55  0
             checkGetter(method);
 56  0
             return Introspector.decapitalize(name.substring(2));
 57  
         }
 58  
 
 59  0
         if (name.startsWith("get"))
 60  
         {
 61  0
             checkGetter(method);
 62  0
             return Introspector.decapitalize(name.substring(3));
 63  
         }
 64  
 
 65  0
         if (name.startsWith("set"))
 66  
         {
 67  0
             checkSetter(method);
 68  0
             return Introspector.decapitalize(name.substring(3));
 69  
         }
 70  
 
 71  0
         throw new ApplicationRuntimeException(AnnotationMessages.notAccessor(method));
 72  
     }
 73  
     
 74  
     /**
 75  
      * Converts a method name to a property key. The steps performed are:
 76  
      * <p>The prefix "get" is stripped off (if present)
 77  
      * <p>The letter following "get" is converted to lower case
 78  
      * <p>Other capitalized letters are converted to lower case and preceded with a dash ("-")
 79  
      * 
 80  
      * @param methodName the method to convert
 81  
      * @return the converted key
 82  
      * 
 83  
      * @since 4.1.1 
 84  
      */
 85  
     public static String convertMethodNameToKeyName(String methodName)
 86  
     {
 87  0
         StringBuffer buffer = new StringBuffer();
 88  
     
 89  0
         int cursorx = methodName.startsWith("get") ? 3 : 0;
 90  0
         int length = methodName.length();
 91  0
         boolean atStart = true;
 92  
     
 93  0
         while (cursorx < length)
 94  
         {
 95  0
             char ch = methodName.charAt(cursorx);
 96  
     
 97  0
             if (Character.isUpperCase(ch))
 98  
             {
 99  0
                 if (!atStart)
 100  0
                     buffer.append('-');
 101  0
                 buffer.append(Character.toLowerCase(ch));
 102  
             }
 103  
             else
 104  0
                 buffer.append(ch);
 105  
     
 106  0
             atStart = false;
 107  
     
 108  0
             cursorx++;
 109  0
         }
 110  
     
 111  0
         return buffer.toString();
 112  
     }    
 113  
     
 114  
     /**
 115  
      * Copies all bindings of a component to another one.
 116  
      * @param source
 117  
      * @param target
 118  
      * 
 119  
      * @since 4.1.1
 120  
      */
 121  
     public static void copyBindings(IContainedComponent source, IContainedComponent target)
 122  
     {
 123  0
         Iterator i = source.getBindingNames().iterator();
 124  0
         while (i.hasNext())
 125  
         {
 126  0
             String bindingName = (String) i.next();
 127  0
             IBindingSpecification binding = source.getBinding(bindingName);
 128  0
             target.setBinding(bindingName, binding);
 129  0
         }
 130  
     
 131  0
         target.setType(source.getType());
 132  0
     }    
 133  
 
 134  
     private static void checkGetter(Method method)
 135  
     {
 136  0
         if (method.getParameterTypes().length > 0)
 137  0
             throw new ApplicationRuntimeException(AnnotationMessages.noParametersExpected(method));
 138  
 
 139  0
         if (method.getReturnType().equals(void.class))
 140  0
             throw new ApplicationRuntimeException(AnnotationMessages.voidAccessor(method));
 141  
 
 142  0
     }
 143  
 
 144  
     private static void checkSetter(Method method)
 145  
     {
 146  0
         if (!method.getReturnType().equals(void.class))
 147  0
             throw new ApplicationRuntimeException(AnnotationMessages.nonVoidMutator(method));
 148  
 
 149  0
         if (method.getParameterTypes().length != 1)
 150  0
             throw new ApplicationRuntimeException(AnnotationMessages.wrongParameterCount(method));
 151  0
     }
 152  
 
 153  
     public static Location buildLocationForAnnotation(Method method, Annotation annotation,
 154  
             Resource classResource)
 155  
     {
 156  0
         return new DescribedLocation(classResource, AnnotationMessages.methodAnnotation(
 157  
                 annotation,
 158  
                 method));
 159  
     }
 160  
 }