mx4j/src/core/javax/management/openmbean ArrayType.java,1.12,1.13

Jeremy Boynes <[email protected]> Tue, 08 Feb 2005 07:32:38 +0000
Newsgroups gmane.comp.java.mx4j.cvs
Message-ID <[email protected]>
Update of /cvsroot/mx4j/mx4j/src/core/javax/management/openmbean
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31643/src/core/javax/management/openmbean

Modified Files:
	ArrayType.java 
Log Message:
Fix for [ 1115589 ] ArrayType.isValue does not handle multidimensional arrays
Added testcase

Index: ArrayType.java
===================================================================
RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/openmbean/ArrayType.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** ArrayType.java	13 Nov 2004 16:38:22 -0000	1.12
--- ArrayType.java	8 Feb 2005 07:32:33 -0000	1.13
***************
*** 10,14 ****
  
  import java.io.Serializable;
- import java.lang.reflect.Array;
  
  /**
--- 10,13 ----
***************
*** 94,134 ****
     public boolean isValue(Object object)
     {
-       boolean result = false;
- 
        if (object == null || !object.getClass().isArray())
        {
!          result = false;
!       }
!       else if (elementType instanceof SimpleType)
!       {
!          result = getClassName().equals(object.getClass().getName());
        }
!       else if (elementType instanceof CompositeType)
        {
!          try
!          {
!             Class elementClass = Thread.currentThread().getContextClassLoader().loadClass(getClassName());
!             if (elementClass.isAssignableFrom(object.getClass()))
!             {
!                if (dimension == 1)
!                {
!                   result = isValidCompositeDimension((CompositeData[])object);
!                }
!                else
!                {
!                   result = true;	// assume success
!                   for (int d = 0; d < dimension && result == true; d++)
!                   {
!                      result = isValidCompositeDimension((CompositeData[])Array.get(object, d));
!                   }
!                }
!             }
!          }
!          catch (ClassNotFoundException x)
!          {
!             result = false;
!          }
        }
!       else if (elementType instanceof TabularType)
        {
           try
--- 93,107 ----
     public boolean isValue(Object object)
     {
        if (object == null || !object.getClass().isArray())
        {
!          return false;
        }
! 
!       if (elementType instanceof SimpleType)
        {
!          return getClassName().equals(object.getClass().getName());
        }
! 
!       if (elementType instanceof TabularType || elementType instanceof CompositeType)
        {
           try
***************
*** 137,161 ****
              if (elementClass.isAssignableFrom(object.getClass()))
              {
!                if (dimension == 1)
!                {
!                   result = isValidTabularDimension((TabularData[])object);
!                }
!                else
!                {
!                   result = true;	// assume success
!                   for (int d = 0; d < dimension && result == true; d++)
!                   {
!                      result = isValidTabularDimension((TabularData[])Array.get(object, d));
!                   }
!                }
              }
           }
           catch (ClassNotFoundException x)
           {
!             result = false;
           }
        }
! 
!       return result;
     }
  
--- 110,122 ----
              if (elementClass.isAssignableFrom(object.getClass()))
              {
!                return checkElements((Object[]) object, dimension);
              }
           }
           catch (ClassNotFoundException x)
           {
!             return false;
           }
        }
!       return false;
     }
  
***************
*** 254,315 ****
     }
  
! 
!    /**
!     * Checks if the given array Has a null element.
!     * <p/>
!     * Note: Will try to traverse
!     * multi-dimension arrays
!     */
!    private static boolean arrayHasNull(Object[] obj)
!    {
! 
!       //TODO: Is this optimal?
! 
!       for (int i = 0; i < obj.length; i++)
        {
!          if (obj[i] == null)
           {
!             return true;
           }
! 
!          if (obj[i].getClass().isArray())
           {
!             if (arrayHasNull((Object[])obj[i]))
              {
!                return true;
              }
           }
! 
!       }
! 
!       return false;
! 
! 
!    }
! 
!    private boolean isValidCompositeDimension(CompositeData[] dimension)
!    {
!       boolean result = true; // assume success
! 
!       for (int i = 0; i < dimension.length && result == true; i++)
!       {
!          if (dimension[i] != null && !elementType.isValue(dimension[i]))
!             result = false;
!       }
! 
!       return result;
!    }
! 
!    private boolean isValidTabularDimension(TabularData[] dimension)
!    {
!       boolean result = true; // assume success
! 
!       for (int i = 0; i < dimension.length && result == true; i++)
!       {
!          if (dimension[i] != null && !elementType.isValue(dimension[i]))
!             result = false;
        }
- 
-       return result;
     }
  }
--- 215,242 ----
     }
  
!    private boolean checkElements(Object[] array, int dim) {
!       if (dim == 1)
        {
!          OpenType arrayType = getElementOpenType();
!          for (int i = 0; i < array.length; i++)
           {
!             Object o = array[i];
!             if (o != null && !arrayType.isValue(o))
!             {
!                return false;
!             }
           }
!          return true;
!       } else {
!          for (int i = 0; i < array.length; i++)
           {
!             Object o = array[i];
!             if (o != null && !checkElements((Object[])o, dim-1))
              {
!                return false;
              }
           }
!          return true;
        }
     }
  }



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click