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.bean.LightweightBeanInitializer;
022 import org.apache.tapestry.enhance.EnhancementOperation;
023 import org.apache.tapestry.spec.BeanSpecification;
024 import org.apache.tapestry.spec.IBeanSpecification;
025 import org.apache.tapestry.spec.IComponentSpecification;
026
027 /**
028 * Creates a {@link org.apache.tapestry.spec.IBeanSpecification} from the
029 * {@link org.apache.tapestry.annotations.Bean} annotation.
030 *
031 * @author Howard M. Lewis Ship
032 * @since 4.0
033 */
034 public class BeanAnnotationWorker implements MethodAnnotationEnhancementWorker
035 {
036 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
037 Method method, Location location)
038 {
039 Bean bean = method.getAnnotation(Bean.class);
040 String propertyName = AnnotationUtils.getPropertyName(method);
041
042 Class beanClass = bean.value();
043 if (beanClass.equals(Object.class))
044 beanClass = op.getPropertyType(propertyName);
045
046 IBeanSpecification bs = new BeanSpecification();
047
048 // A shame to convert it to a string then back to
049 // a class later, but ...
050
051 bs.setClassName(beanClass.getName());
052 bs.setPropertyName(propertyName);
053 bs.setLocation(location);
054
055 // Starting to like enums!
056
057 bs.setLifecycle(bean.lifecycle().getBeanLifecycle());
058
059 String initializer = bean.initializer();
060
061 if (HiveMind.isNonBlank(initializer))
062 bs.addInitializer(new LightweightBeanInitializer(initializer));
063
064 spec.addBeanSpecification(propertyName, bs);
065 }
066
067 }