001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.annotations;
016    
017    import java.beans.Introspector;
018    import java.lang.annotation.Annotation;
019    import java.lang.reflect.Method;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.hivemind.Location;
023    import org.apache.hivemind.Resource;
024    import org.apache.tapestry.util.DescribedLocation;
025    
026    /**
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    
031    public final class AnnotationUtils
032    {
033        /* defeat instantiation */
034        private AnnotationUtils() { }
035        
036        /**
037         * Determines the property name for a method, by stripping off the is/get/set prefix and
038         * decapitalizing the first name.
039         * 
040         * @param method
041         *            accessor method (get/set/is)
042         * @return the property name for the method
043         * @throws ApplicationRuntimeException
044         *             if the method is not an accessor or mutator method
045         */
046        public static String getPropertyName(Method method)
047        {
048            String name = method.getName();
049    
050            if (name.startsWith("is"))
051            {
052                checkGetter(method);
053                return Introspector.decapitalize(name.substring(2));
054            }
055    
056            if (name.startsWith("get"))
057            {
058                checkGetter(method);
059                return Introspector.decapitalize(name.substring(3));
060            }
061    
062            if (name.startsWith("set"))
063            {
064                checkSetter(method);
065                return Introspector.decapitalize(name.substring(3));
066            }
067    
068            throw new ApplicationRuntimeException(AnnotationMessages.notAccessor(method));
069        }
070    
071        private static void checkGetter(Method method)
072        {
073            if (method.getParameterTypes().length > 0)
074                throw new ApplicationRuntimeException(AnnotationMessages.noParametersExpected(method));
075    
076            if (method.getReturnType().equals(void.class))
077                throw new ApplicationRuntimeException(AnnotationMessages.voidAccessor(method));
078    
079        }
080    
081        private static void checkSetter(Method method)
082        {
083            if (!method.getReturnType().equals(void.class))
084                throw new ApplicationRuntimeException(AnnotationMessages.nonVoidMutator(method));
085    
086            if (method.getParameterTypes().length != 1)
087                throw new ApplicationRuntimeException(AnnotationMessages.wrongParameterCount(method));
088        }
089    
090        public static Location buildLocationForAnnotation(Method method, Annotation annotation,
091                Resource classResource)
092        {
093            return new DescribedLocation(classResource, AnnotationMessages.methodAnnotation(
094                    annotation,
095                    method));
096        }
097    }