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.lang.reflect.Method;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.HiveMind;
021    import org.apache.hivemind.Location;
022    import org.apache.tapestry.IPage;
023    import org.apache.tapestry.enhance.EnhancementOperation;
024    import org.apache.tapestry.spec.IComponentSpecification;
025    import org.apache.tapestry.spec.IParameterSpecification;
026    import org.apache.tapestry.spec.ParameterSpecification;
027    
028    /**
029     * Generates a {@link org.apache.tapestry.spec.IParameterSpecification} from a
030     * {@link org.apache.tapestry.annotations.Parameter} annotation and adds it to the
031     * {@link org.apache.tapestry.spec.IComponentSpecification}.
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    public class ParameterAnnotationWorker implements MethodAnnotationEnhancementWorker
037    {
038    
039        public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
040                Method method, Location location)
041        {
042            if (IPage.class.isAssignableFrom(method.getDeclaringClass()))
043            {
044                throw new ApplicationRuntimeException(
045                        AnnotationMessages.invalidAnnotationInClass(Parameter.class, method.getDeclaringClass()));
046            }       
047            
048            Parameter parameter = method.getAnnotation(Parameter.class);
049    
050            String propertyName = AnnotationUtils.getPropertyName(method);
051    
052            boolean deprecated = method.isAnnotationPresent(Deprecated.class);
053    
054            IParameterSpecification ps = new ParameterSpecification();
055    
056            String parameterName = parameter.name();
057    
058            if (HiveMind.isBlank(parameterName))
059                parameterName = propertyName;
060    
061            Class propertyType = op.getPropertyType(propertyName);
062    
063            ps.setAliases(parameter.aliases());
064            ps.setCache(parameter.cache());
065    
066            if (HiveMind.isNonBlank(parameter.defaultValue()))
067                ps.setDefaultValue(parameter.defaultValue());
068    
069            ps.setDeprecated(deprecated);
070            ps.setParameterName(parameterName);
071            ps.setPropertyName(propertyName);
072            ps.setRequired(parameter.required());
073            ps.setType(propertyType.getName());
074            ps.setLocation(location);
075    
076            spec.addParameter(ps);
077        }
078    }