[jboss-cvs] jboss-seam/src/main/org/jboss/seam/remoting/wrapper ...

Shane Bryzak Shane_Bryzak at symantec.com
Wed Aug 2 21:31:50 EDT 2006


  User: sbryzak2
  Date: 06/08/02 21:31:50

  Modified:    src/main/org/jboss/seam/remoting/wrapper  BeanWrapper.java
  Log:
  Support for get/set methods without corresponding field
  
  Revision  Changes    Path
  1.10      +59 -9     jboss-seam/src/main/org/jboss/seam/remoting/wrapper/BeanWrapper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: BeanWrapper.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/remoting/wrapper/BeanWrapper.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -b -r1.9 -r1.10
  --- BeanWrapper.java	26 Jul 2006 01:33:54 -0000	1.9
  +++ BeanWrapper.java	3 Aug 2006 01:31:50 -0000	1.10
  @@ -10,6 +10,8 @@
   import org.jboss.seam.Component;
   import org.jboss.seam.Seam;
   import org.jboss.seam.remoting.InterfaceGenerator;
  +import java.lang.reflect.Method;
  +import java.lang.reflect.*;
   
   /**
    * @author Shane Bryzak
  @@ -183,26 +185,73 @@
   
       out.write(BEAN_START_TAG_CLOSE);
   
  -    for (Field f : InterfaceGenerator.getAccessibleFields(cls))
  +    for (String propertyName : InterfaceGenerator.getAccessibleProperties(cls))
       {
  -      String fieldPath = path != null && path.length() > 0 ? String.format("%s.%s", path, f.getName()) : f.getName();
  +      String fieldPath = path != null && path.length() > 0 ? String.format("%s.%s", path, propertyName) : propertyName;
   
         // Also exclude fields listed using wildcard notation: [componentName].fieldName
  -      String wildCard = String.format("[%s].%s", componentName != null ? componentName : cls.getName(), f.getName());
  +      String wildCard = String.format("[%s].%s", componentName != null ? componentName : cls.getName(), propertyName);
   
         if (constraints == null || (!constraints.contains(fieldPath) && !constraints.contains(wildCard)))
         {
           out.write(MEMBER_START_TAG_OPEN);
  -        out.write(f.getName().getBytes());
  +        out.write(propertyName.getBytes());
           out.write(MEMBER_START_TAG_CLOSE);
   
  -        boolean accessible = f.isAccessible();
  +        Field f = null;
  +        try
  +        {
  +          f = cls.getField(propertyName);
  +        }
  +        catch (NoSuchFieldException ex) { }
  +
  +        boolean accessible = false;
           try
           {
             // Temporarily set the field's accessibility so we can read it
  +          if (f != null)
  +          {
  +            accessible = f.isAccessible();
             f.setAccessible(true);
  +            context.createWrapperFromObject(f.get(value),
  +                fieldPath).marshal(out);
  +          }
  +          else
  +          {
  +            Method accessor = null;
  +            try
  +            {
  +              accessor = cls.getMethod(String.format("get%s%s",
  +                  Character.toUpperCase(propertyName.charAt(0)),
  +                  propertyName.substring(1)));
  +            }
  +            catch (NoSuchMethodException ex)
  +            {
  +              try
  +              {
  +                accessor = cls.getMethod(String.format("is%s%s",
  +                    Character.toUpperCase(propertyName.charAt(0)),
  +                    propertyName.substring(1)));
  +              }
  +              catch (NoSuchMethodException ex2)
  +              {
  +                // uh oh... continue with the next one
  +                continue;
  +              }
  +            }
   
  -          context.createWrapperFromObject(f.get(value), fieldPath).marshal(out);
  +            try
  +            {
  +              context.createWrapperFromObject(accessor.invoke(value), fieldPath).
  +                  marshal(out);
  +            }
  +            catch (InvocationTargetException ex)
  +            {
  +              throw new RuntimeException(String.format(
  +                  "Failed to read property [%s] for object [%s]",
  +                  propertyName, value));
  +            }
  +          }
           }
           catch (IllegalAccessException ex)
           {
  @@ -210,6 +259,7 @@
           }
           finally
           {
  +          if (f != null)
             f.setAccessible(accessible);
           }
   
  
  
  



More information about the jboss-cvs-commits mailing list