001    package org.tynamo.descriptor.annotation;
002    
003    import java.lang.annotation.Annotation;
004    import java.lang.reflect.Field;
005    import java.lang.reflect.Method;
006    
007    import org.apache.commons.beanutils.PropertyUtils;
008    
009    /**
010     * @author Chris Nelson
011     */
012    public abstract class AbstractAnnotationHandler
013    {
014    
015            /**
016             * This method will check if an annotation property has a default value by
017             * see if there is a field named DEFAULT_ + property name.  If it has a default
018             * value, we only set the property on the target object if the annotation property is NOT set to
019             * the default value.
020             *
021             * @param propertyDescriptorAnno
022             * @param annotationMethod
023             * @return
024             */
025            protected boolean isDefault(Annotation propertyDescriptorAnno, Method annotationMethod)
026            {
027                    try
028                    {
029                            Field defaultField = propertyDescriptorAnno.getClass().getField("DEFAULT_" + annotationMethod.getName());
030                            if (defaultField != null)
031                            {
032                                    if (annotationMethod.invoke(propertyDescriptorAnno).equals(
033                                            defaultField.get(propertyDescriptorAnno)))
034                                    {
035                                            return true;
036                                    }
037                            }
038                            return false;
039                    } catch (Exception ex)
040                    {
041                            return false;
042                    }
043            }
044    
045            /**
046             * For each attribute of annotation, will search for a matching property on
047             * the target and set it with the value of the attribute unless the attribute
048             * is set to the "default" value
049             *
050             * @param annotation
051             * @param descriptor
052             */
053            protected void setPropertiesFromAnnotation(Annotation annotation, Object target)
054            {
055                    /**
056                     * !! This is how we get our properties migrated from our
057                     * annotation to our property descriptor !!      
058                     */             
059                    for (Method annotationMethod : annotation.getClass().getMethods())
060                    {
061                            try
062                            {
063                                    if (!isDefault(annotation, annotationMethod))
064                                    {
065                                            PropertyUtils.setProperty(target,
066                                                    annotationMethod.getName(),
067                                                    annotationMethod.invoke(annotation)
068                                            );
069                                    }
070    
071                            } catch (Exception e)
072                            {
073                                    // ignored
074                            }
075                    }
076            }
077    
078    }