[jboss-cvs] JBossAS SVN: r64496 - in projects/microcontainer/trunk/metatype/src: tests/org/jboss/test/metatype/types/factory/test and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Aug 7 23:27:13 EDT 2007


Author: scott.stark at jboss.org
Date: 2007-08-07 23:27:13 -0400 (Tue, 07 Aug 2007)
New Revision: 64496

Modified:
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/factory/test/ArrayMetaTypeFactoryUnitTestCase.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/ArrayValueFactoryUnitTestCase.java
Log:
JBMICROCONT-202, handle primative arrays better in DefaultMetaValueFactory.

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java	2007-08-08 02:57:38 UTC (rev 64495)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java	2007-08-08 03:27:13 UTC (rev 64496)
@@ -23,6 +23,7 @@
 
 import java.io.Serializable;
 import java.lang.ref.WeakReference;
+import java.lang.reflect.Array;
 import java.lang.reflect.Type;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
@@ -60,6 +61,7 @@
 import org.jboss.metatype.api.values.TableValueSupport;
 import org.jboss.metatype.plugins.types.DefaultMetaTypeFactory;
 import org.jboss.metatype.spi.values.MetaValueBuilder;
+import org.jboss.reflect.spi.ArrayInfo;
 import org.jboss.reflect.spi.ClassInfo;
 import org.jboss.reflect.spi.TypeInfo;
 
@@ -165,6 +167,71 @@
    }
 
    /**
+    * Transform a primitive array into an Object[]. Converts
+    * a primitive array like char[] to Object[]. 
+    * 
+    * @param type - the primitive array class type info.
+    * @param value - the primitive array instance.
+    * @return
+    */
+   public static Object[] convertPrimativeArray(TypeInfo type, Object value)
+   {
+      Object[] oa = null;
+      if( type instanceof ArrayInfo )
+      {
+         // Get the Object form of the element
+         ArrayInfo arrayInfo = ArrayInfo.class.cast(type);
+         TypeInfo etype = arrayInfo.getComponentType();
+         int size = Array.getLength(value);
+         oa = new Object[size];
+         for(int n = 0; n < size; n ++)
+         {
+            Object nvalue = Array.get(value, n);
+            // Recursively convert nested array elements
+            if (etype.isArray())
+            {
+               oa[n] = convertPrimativeArray(etype, nvalue);
+            }
+            oa[n] = nvalue;
+         }
+      }
+      else
+      {
+         oa = (Object[]) value;
+      }
+      
+      return oa;      
+   }
+   public static Object[] convertPrimativeArray(Object value)
+   {
+      Object[] oa = null;
+      Class type = value.getClass();
+      if( type.isArray() )
+      {
+         // Get the Object form of the element
+         Class etype = type.getComponentType();
+         int size = Array.getLength(value);
+         oa = new Object[size];
+         for(int n = 0; n < size; n ++)
+         {
+            Object nvalue = Array.get(value, n);
+            // Recursively convert nested array elements
+            if (etype.isArray())
+            {
+               oa[n] = convertPrimativeArray(nvalue);
+            }
+            oa[n] = nvalue;
+         }
+      }
+      else
+      {
+         oa = (Object[]) value;
+      }
+      
+      return oa;      
+   }
+
+   /**
     * Create an array value
     * 
     * @param type the type
@@ -198,7 +265,21 @@
       
       ClassInfo classInfo = configuration.getClassInfo(value.getClass());
       if (classInfo.isArray())
-         oldArray = (Object[]) value;
+      {
+         // See if this is a primitive array
+         ArrayInfo arrayInfo = ArrayInfo.class.cast(classInfo);
+         TypeInfo compInfo = arrayInfo.getComponentType();
+         while(compInfo instanceof ArrayInfo)
+         {
+            arrayInfo = ArrayInfo.class.cast(compInfo);
+            compInfo = arrayInfo.getComponentType();
+         }
+         // Translate 
+         if (compInfo.isPrimitive())
+            oldArray = convertPrimativeArray(classInfo, value);
+         else
+            oldArray = (Object[]) value;
+      }
       else if (classInfo.isCollection())
       {
          Collection c = (Collection) value;
@@ -230,9 +311,13 @@
       
       if (dimension > 1)
       {
+         Object[] nestedOld;
          for (int i = 0; i < oldArray.length; ++i)
          {
-            Object[] nestedOld = (Object[]) oldArray[i];
+            if ( !(oldArray[i] instanceof Object[]) )
+               nestedOld = convertPrimativeArray(oldArray[i]);
+            else
+               nestedOld = (Object[]) oldArray[i];
             Object[] result = createArray(elementType, componentType.getComponentType(), dimension-1, nestedOld);
             newArray[i] = result;
          }

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/factory/test/ArrayMetaTypeFactoryUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/factory/test/ArrayMetaTypeFactoryUnitTestCase.java	2007-08-08 02:57:38 UTC (rev 64495)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/factory/test/ArrayMetaTypeFactoryUnitTestCase.java	2007-08-08 03:27:13 UTC (rev 64496)
@@ -70,6 +70,26 @@
       testArray(expected, actual);
    }
 
+   public void testCharArray()
+      throws Exception
+   {
+      char[] array = {'H', 'e', 'l', 'l', 'o'};
+      MetaType result = resolve(array.getClass());
+
+      ArrayMetaType actual = assertInstanceOf(result, ArrayMetaType.class);
+      ArrayMetaType expected = new ArrayMetaType(1, SimpleMetaType.CHARACTER);
+      testArray(expected, actual);
+   }
+   public void testChar2DArray()
+      throws Exception
+   {
+      char[][] array = {{'H', 'e', 'l', 'l', 'o'}};
+      MetaType result = resolve(array.getClass());
+      ArrayMetaType actual = assertInstanceOf(result, ArrayMetaType.class);
+      ArrayMetaType expected = new ArrayMetaType(2, SimpleMetaType.CHARACTER);
+      testArray(expected, actual);
+   }
+
    /**
     * Test the correct meta type is generated for a composite array
     * 

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/ArrayValueFactoryUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/ArrayValueFactoryUnitTestCase.java	2007-08-08 02:57:38 UTC (rev 64495)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/ArrayValueFactoryUnitTestCase.java	2007-08-08 03:27:13 UTC (rev 64496)
@@ -95,9 +95,32 @@
       MetaValue result = createMetaValue(array);
       ArrayValue actual = assertInstanceOf(result, ArrayValue.class);
       getLog().debug("Array Value: " + actual);
-      assertEquals(expected, actual);      
+      assertEquals(expected, actual);
    }
+   public void test2DCharArray()
+   {
+      char[][] array = {"Hello".toCharArray(), "World".toCharArray()};
+      ArrayMetaType arrayType = assertInstanceOf(resolve(array.getClass()), ArrayMetaType.class);
+      MetaValue[][] metaArray = { {SimpleValueSupport.wrap('H'),
+            SimpleValueSupport.wrap('e'),
+            SimpleValueSupport.wrap('l'),
+            SimpleValueSupport.wrap('l'),
+            SimpleValueSupport.wrap('o')},
 
+            {SimpleValueSupport.wrap('W'),
+            SimpleValueSupport.wrap('o'),
+            SimpleValueSupport.wrap('r'),
+            SimpleValueSupport.wrap('l'),
+            SimpleValueSupport.wrap('d')}               
+      };
+      ArrayValueSupport expected = new ArrayValueSupport(arrayType, metaArray);
+
+      MetaValue result = createMetaValue(array);
+      ArrayValue actual = assertInstanceOf(result, ArrayValue.class);
+      getLog().debug("Array Value: " + actual);
+      assertEquals(expected, actual);
+   }
+
    /**
     * Test the correct value is generated for a composite array
     * 
@@ -141,4 +164,5 @@
       getLog().debug("Array Value: " + actual);
       assertEquals(expected, actual);
    }
+
 }




More information about the jboss-cvs-commits mailing list