[weld-commits] Weld SVN: r6367 - extensions/trunk/src/main/java/org/jboss/weld/extensions/util.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Tue Jun 1 07:51:42 EDT 2010


Author: shane.bryzak at jboss.com
Date: 2010-06-01 07:51:42 -0400 (Tue, 01 Jun 2010)
New Revision: 6367

Modified:
   extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AbstractBeanProperty.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotatedBeanProperty.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/util/TypedBeanProperty.java
Log:
refactor bean property util classes to do work in constructor


Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AbstractBeanProperty.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AbstractBeanProperty.java	2010-06-01 09:56:01 UTC (rev 6366)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AbstractBeanProperty.java	2010-06-01 11:51:42 UTC (rev 6367)
@@ -11,9 +11,27 @@
  * 
  * @author Shane Bryzak
  */
-public abstract class AbstractBeanProperty
+public class AbstractBeanProperty
 {
    /**
+    * Subclasses should provide an implementation of FieldMatcher to determine 
+    * whether a Field contains the bean property 
+    */
+   interface FieldMatcher 
+   {
+      boolean matches(Field f);
+   }
+   
+   /**
+    * Subclasses should provide an implementation of MethodMatcher to determine
+    * whether a method provides the bean property
+    */
+   interface MethodMatcher
+   {
+      boolean matches(Method m);
+   }
+   
+   /**
     * Property field
     */
    private Field propertyField;
@@ -31,18 +49,11 @@
    /**
     * Property name
     */
-   private String name;
+   private String name;   
    
    /**
-    * The class containing the property
+    * Flag indicating whether the bean property value is contained in a field 
     */
-   private Class<?> targetClass;
-   
-   /**
-    * Flag indicating whether the target class has been scanned
-    */
-   private boolean scanned = false;   
-   
    private boolean isFieldProperty;
    
    /**
@@ -50,27 +61,26 @@
     */
    private boolean valid = false;
    
-   private Type propertyType;   
+   private Type propertyType;
    
+   private FieldMatcher fieldMatcher;
+   
+   private MethodMatcher methodMatcher;
+   
    /**
     * 
     * @param targetClass
     */
-   public AbstractBeanProperty(Class<?> targetClass)
+   public AbstractBeanProperty(Class<?> targetClass, FieldMatcher fieldMatcher, 
+         MethodMatcher methodMatcher)
    {
-      this.targetClass = targetClass;
-   }
-   
-   protected void scan()
-   {
-      if (scanned) return;
+      this.fieldMatcher = fieldMatcher;
+      this.methodMatcher = methodMatcher;
       
-      scanned = true;
-      
       // First check declared fields
       for (Field f : targetClass.getDeclaredFields())
       {
-         if (fieldMatches(f))
+         if (fieldMatcher.matches(f))
          {
             setupFieldProperty(f);            
             valid = true;
@@ -81,7 +91,7 @@
       // Then check public fields, in case it's inherited
       for (Field f : targetClass.getFields())
       {
-         if (fieldMatches(f)) 
+         if (fieldMatcher.matches(f)) 
          {
             setupFieldProperty(f);
             valid = true;
@@ -92,7 +102,7 @@
       // Then check public methods (we ignore private methods)
       for (Method m : targetClass.getMethods())
       {
-         if (methodMatches(m))
+         if (methodMatcher.matches(m))
          {
             String methodName = m.getName();
             
@@ -119,12 +129,18 @@
                      "Method: " + m + " in class: " + targetClass);
             }
          }
-      }        
+      }      
    }
    
-   protected abstract boolean fieldMatches(Field f);
+   public FieldMatcher getFieldMatcher()
+   {
+      return fieldMatcher;
+   }
    
-   protected abstract boolean methodMatches(Method m);
+   public MethodMatcher getMethodMatcher()
+   {
+      return methodMatcher;
+   }
       
    private void setupFieldProperty(Field propertyField)
    {
@@ -146,8 +162,6 @@
     */
    public void setValue(Object bean, Object value) throws Exception
    {
-      scan();
-      
       if (isFieldProperty)
       {
          setFieldValue(propertyField, bean, value);        
@@ -169,8 +183,6 @@
     */
    public Object getValue(Object bean) throws Exception
    {
-      scan();
-      
       if (isFieldProperty)
       {
          return getFieldValue(propertyField, bean);  
@@ -188,7 +200,6 @@
     */
    public Type getPropertyType()
    {
-      scan();
       return propertyType;
    }
    
@@ -200,7 +211,6 @@
     */
    public boolean isValid()
    {
-      scan();
       return valid;
    }      
    
@@ -213,8 +223,7 @@
     * @return The name of the property
     */
    public String getName()
-   {
-      scan();      
+   {  
       return name;
    }
    
@@ -240,7 +249,7 @@
    
    private String buildGetFieldValueErrorMessage(Field field, Object obj)
    {
-      return String.format("Exception reading [%s] field from object [%s].",
+      return String.format("Exception reading [%s] field from object [%s].", 
             field.getName(), obj);
    }
    

Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotatedBeanProperty.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotatedBeanProperty.java	2010-06-01 09:56:01 UTC (rev 6366)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotatedBeanProperty.java	2010-06-01 11:51:42 UTC (rev 6367)
@@ -14,76 +14,112 @@
  */
 public class AnnotatedBeanProperty<T extends Annotation> extends AbstractBeanProperty
 {   
-   private T annotation;
+   private T annotation;   
    
-   private Class<T> annotationClass;
+   public interface AnnotationMatcher
+   {
+      boolean matches(Annotation annotation);
+   }
    
-   /**
-    * Default constructor
-    * 
-    * @param cls The class to scan for the property
-    * @param annotationClass The annotation class to scan for. Specified attribute
-    * values may be scanned for by providing an implementation of the isMatch() method. 
-    */
-   public AnnotatedBeanProperty(Class<?> cls, Class<T> annotationClass)
-   {            
-      super(cls);
-      this.annotationClass = annotationClass;
-   }   
+   public static class DefaultAnnotationMatcher implements AnnotationMatcher
+   {
+      public boolean matches(Annotation annotation)
+      {
+         return true;
+      }
+   }
    
-   protected boolean fieldMatches(Field f)
+   private static class AnnotatedFieldMatcher implements FieldMatcher
    {      
-      if (f.isAnnotationPresent(annotationClass) && 
-            annotationMatches(f.getAnnotation(annotationClass)))
-      {      
-         this.annotation = f.getAnnotation(annotationClass);
-         return true;
+      private Class<? extends Annotation> annotationClass;
+      private AnnotationMatcher matcher;
+      private Annotation match;
+      
+      public AnnotatedFieldMatcher(Class<? extends Annotation> annotationClass, 
+            AnnotationMatcher matcher)
+      {
+         this.annotationClass = annotationClass;
+         this.matcher = matcher;
       }
-      else
+      
+      public boolean matches(Field f)
       {
+         if (f.isAnnotationPresent(annotationClass) && 
+               matcher.matches(f.getAnnotation(annotationClass)))
+         {
+            this.match = f.getAnnotation(annotationClass);
+            return true;
+         }
          return false;
       }
+      
+      public Annotation getMatch()
+      {
+         return match;
+      }
    }
    
-   protected boolean methodMatches(Method m)
+   private static class AnnotatedMethodMatcher implements MethodMatcher
    {
-      if (m.isAnnotationPresent(annotationClass) &&
-            annotationMatches(m.getAnnotation(annotationClass)))
+      private Class<? extends Annotation> annotationClass;
+      private AnnotationMatcher matcher;
+      private Annotation match;
+      
+      public AnnotatedMethodMatcher(Class<? extends Annotation> annotationClass,
+            AnnotationMatcher matcher)
       {
-         this.annotation = m.getAnnotation(annotationClass);
-         return true;
+         this.annotationClass = annotationClass;
+         this.matcher = matcher;
       }
-      else
+      
+      public boolean matches(Method m)
       {
+         if (m.isAnnotationPresent(annotationClass) &&
+               matcher.matches(m.getAnnotation(annotationClass)))
+         {
+            this.match = m.getAnnotation(annotationClass);
+            return true;
+         }
          return false;
       }
+      
+      public Annotation getMatch()
+      {
+         return match;
+      }
    }
    
    /**
-    * This method may be overridden by a subclass. It can be used to scan 
-    * for an annotation with particular attribute values, or to allow a match
-    * based on more complex logic.  
+    * Default constructor
     * 
-    * @param annotation The potential match
-    * @return true if the specified annotation is a match
+    * @param cls The class to scan for the property
+    * @param annotationClass The annotation class to scan for. Specified attribute
+    * values may be scanned for by providing an implementation of the isMatch() method. 
     */
-   protected boolean annotationMatches(T annotation)
-   {
-      return true;
-   }
+   @SuppressWarnings("unchecked")
+   public AnnotatedBeanProperty(Class<?> cls, Class<T> annotationClass, 
+         AnnotationMatcher annotationMatcher)
+   {            
+      super(cls, new AnnotatedFieldMatcher(annotationClass, annotationMatcher != null ? annotationMatcher : new DefaultAnnotationMatcher()), 
+            new AnnotatedMethodMatcher(annotationClass, annotationMatcher != null ? annotationMatcher : new DefaultAnnotationMatcher()));
+      
+      if (((AnnotatedFieldMatcher) getFieldMatcher()).getMatch() != null)
+      {
+         this.annotation = (T) ((AnnotatedFieldMatcher) getFieldMatcher()).getMatch();
+      }
+      else if (((AnnotatedMethodMatcher) getMethodMatcher()).getMatch() != null)
+      {
+         this.annotation = (T) ((AnnotatedMethodMatcher) getMethodMatcher()).getMatch();
+      }
+   }   
    
    /**
-    * Returns the annotation type
+    * Returns the annotation instance
     * 
-    * @return The annotation type
+    * @return The annotation instance
     */
    public T getAnnotation()
    {
-      scan();
       return annotation;
-   }
-
-    
-   
- 
+   } 
 }
\ No newline at end of file

Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/TypedBeanProperty.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/TypedBeanProperty.java	2010-06-01 09:56:01 UTC (rev 6366)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/TypedBeanProperty.java	2010-06-01 11:51:42 UTC (rev 6367)
@@ -10,30 +10,49 @@
  * @author Shane Bryzak
  */
 public class TypedBeanProperty extends AbstractBeanProperty
-{
-   private Class<?> propertyClass;
-   
-   public TypedBeanProperty(Class<?> cls, Class<?> propertyClass)
-   {            
-      super(cls);
+{   
+   private static class TypedFieldMatcher implements FieldMatcher
+   {
+      private Class<?> propertyClass;
       
-      if (propertyClass == null)
+      public TypedFieldMatcher(Class<?> propertyClass)
       {
-         throw new IllegalArgumentException("propertyClass can not be null.");
+         if (propertyClass == null)
+         {
+            throw new IllegalArgumentException("propertyClass can not be null.");
+         }
+         
+         this.propertyClass = propertyClass;
       }
       
-      this.propertyClass = propertyClass;
+      public boolean matches(Field f)
+      {
+         return propertyClass.equals(f.getType());
+      }      
    }
-
-   @Override
-   protected boolean fieldMatches(Field f)
+   
+   private static class TypedMethodMatcher implements MethodMatcher
    {
-      return propertyClass.equals(f.getType());
+      private Class<?> propertyClass;
+      
+      public TypedMethodMatcher(Class<?> propertyClass)
+      {
+         if (propertyClass == null)
+         {
+            throw new IllegalArgumentException("propertyClass can not be null.");
+         }
+         
+         this.propertyClass = propertyClass;
+      }
+      
+      public boolean matches(Method m)
+      {
+         return propertyClass.equals(m.getReturnType());
+      }      
    }
-
-   @Override
-   protected boolean methodMatches(Method m)
-   {
-      return propertyClass.equals(m.getReturnType());
-   }   
+   
+   public TypedBeanProperty(Class<?> cls, Class<?> propertyClass)
+   {            
+      super(cls, new TypedFieldMatcher(propertyClass), new TypedMethodMatcher(propertyClass));
+   }
 }



More information about the weld-commits mailing list