Coverage Report - org.trails.descriptor.annotation.AbstractAnnotationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAnnotationHandler
100% 
100% 
0
 
 1  
 package org.trails.descriptor.annotation;
 2  
 
 3  
 import java.lang.annotation.Annotation;
 4  
 import java.lang.reflect.Field;
 5  
 import java.lang.reflect.Method;
 6  
 
 7  
 import org.apache.commons.beanutils.PropertyUtils;
 8  
 
 9  
 /**
 10  
  * @author Chris Nelson
 11  
  */
 12  
 public abstract class AbstractAnnotationHandler
 13  
 {
 14  
 
 15  
         public AbstractAnnotationHandler()
 16  
         {
 17  7
                 super();
 18  
                 // TODO Auto-generated constructor stub
 19  7
         }
 20  
 
 21  
         /**
 22  
          * This method will check if an annotation property has a default value by
 23  
          * see if there is a field named DEFAULT_ + property name.  If it has a default
 24  
          * value, we only set the property on the target object if the annotation property is NOT set to
 25  
          * the default value.
 26  
          *
 27  
          * @param propertyDescriptorAnno
 28  
          * @param annotationMethod
 29  
          * @return
 30  
          */
 31  
         protected boolean isDefault(Annotation propertyDescriptorAnno, Method annotationMethod)
 32  
         {
 33  
                 try
 34  
                 {
 35  197
                         Field defaultField = propertyDescriptorAnno.getClass().getField("DEFAULT_" + annotationMethod.getName());
 36  25
                         if (defaultField != null)
 37  
                         {
 38  50
                                 if (annotationMethod.invoke(propertyDescriptorAnno).equals(
 39  25
                                         defaultField.get(propertyDescriptorAnno)))
 40  
                                 {
 41  14
                                         return true;
 42  
                                 }
 43  
                         }
 44  11
                         return false;
 45  172
                 } catch (Exception ex)
 46  
                 {
 47  172
                         return false;
 48  
                 }
 49  
         }
 50  
 
 51  
         /**
 52  
          * For each attribute of annotation, will search for a matching property on
 53  
          * the target and set it with the value of the attribute unless the attribute
 54  
          * is set to the "default" value
 55  
          *
 56  
          * @param annotation
 57  
          * @param descriptor
 58  
          */
 59  
         protected void setPropertiesFromAnnotation(Annotation annotation, Object target)
 60  
         {
 61  
                 /**
 62  
                  * !! This is how we get our properties migrated from our
 63  
                  * annotation to our property descriptor !!          
 64  
                  */                
 65  206
                 for (Method annotationMethod : annotation.getClass().getMethods())
 66  
                 {
 67  
                         try
 68  
                         {
 69  197
                                 if (!isDefault(annotation, annotationMethod))
 70  
                                 {
 71  276
                                         PropertyUtils.setProperty(target,
 72  183
                                                 annotationMethod.getName(),
 73  183
                                                 annotationMethod.invoke(annotation)
 74  
                                         );
 75  
                                 }
 76  
 
 77  128
                         } catch (Exception e)
 78  
                         {
 79  
                                 // ignored
 80  
                         }
 81  
                 }
 82  9
         }
 83  
 
 84  
 }