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