[jboss-cvs] JBossAS SVN: r66215 - in projects/microcontainer/trunk/metatype/src: main/org/jboss/metatype/api/values and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Oct 17 08:39:08 EDT 2007


Author: alesj
Date: 2007-10-17 08:39:07 -0400 (Wed, 17 Oct 2007)
New Revision: 66215

Modified:
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/EnumMetaType.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/AbstractMetaValueFactoryTest.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java
Log:
Support MetaValue unwrap with no given TypeInfo.

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/EnumMetaType.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/EnumMetaType.java	2007-10-17 12:33:52 UTC (rev 66214)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/EnumMetaType.java	2007-10-17 12:39:07 UTC (rev 66215)
@@ -57,22 +57,35 @@
    /**
     * Create a new EnumMetaType from the Enum values.
     * 
-    * @param className the class name
     * @param validValues the valid Enum values
     */
    public EnumMetaType(Enum[] validValues)
    {
-      super(String.class.getName(), validValues[0].getClass().getName(), validValues[0].getClass().getName());
-      if (validValues == null)
-         throw new IllegalArgumentException("Null valid values");
+      super(
+            String.class.getName(),
+            isValid(validValues) ? validValues[0].getClass().getName() : null,
+            isValid(validValues) ? validValues[0].getClass().getName() : null
+      );
+      if (isValid(validValues) == false)
+         throw new IllegalArgumentException("Null or empty valid values");
       ArrayList<String> values = new ArrayList<String>();
       for (Enum e : validValues)
          values.add(e.name());
       this.validValues = values;
    }
-   
-   
+
    /**
+    * Are enums valid.
+    *
+    * @param values the enums
+    * @return true if not null and not empty
+    */
+   protected static boolean isValid(Enum[] values)
+   {
+      return values != null && values.length > 0;
+   }
+
+   /**
     * Get the valid values
     * 
     * @return the valid values

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java	2007-10-17 12:33:52 UTC (rev 66214)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java	2007-10-17 12:39:07 UTC (rev 66215)
@@ -87,6 +87,15 @@
     * Supports simple and generic meta value.
     *
     * @param metaValue meta value
+    * @return meta value's value
+    */
+   public abstract Object unwrap(MetaValue metaValue);
+
+   /**
+    * Unwrap meta value.
+    * Supports simple and generic meta value.
+    *
+    * @param metaValue meta value
     * @param type the type
     * @return meta value's value
     */

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-10-17 12:33:52 UTC (rev 66214)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java	2007-10-17 12:39:07 UTC (rev 66215)
@@ -430,33 +430,51 @@
       return internalCreate(value, type, null);
    }
 
+   public Object unwrap(MetaValue metaValue)
+   {
+      return internalUnwrap(metaValue, null);
+   }
+
    public Object unwrap(MetaValue metaValue, Type type)
    {
       TypeInfo typeInfo = configuration.getTypeInfo(type);
-      return unwrap(metaValue, typeInfo);
+      return internalUnwrap(metaValue, typeInfo);
    }
 
    public Object unwrap(MetaValue metaValue, TypeInfo type)
    {
+      return internalUnwrap(metaValue, type);
+   }
+
+   protected Object internalUnwrap(MetaValue metaValue, TypeInfo type)
+   {
       if (metaValue == null)
          return null;
 
-      if (type == null)
-         throw new IllegalArgumentException("Null type info.");
-
       MetaType metaType = metaValue.getMetaType();
       if (metaType.isTable() || metaType.isComposite())
          throw new IllegalArgumentException("Cannot get value from " + metaValue + ", unsupported.");
 
       if (metaType.isSimple())
+      {
          return convertValue(((SimpleValue)metaValue).getValue(), type);
+      }
       else if (metaType.isEnum())
-         return convertValue(((EnumValue)metaValue).getValue(), type);
+      {
+         EnumValue enumValue = ((EnumValue)metaValue);
+         if (type == null)
+            type = getTypeInfo(metaType, null);
+         return convertValue(enumValue.getValue(), type);
+      }
       else if (metaType.isGeneric())
+      {
          return convertValue(((GenericValue)metaValue).getValue(), type);
+      }
       else if (metaType.isArray())
       {
          ArrayValue arrayValue = (ArrayValue)metaValue;
+         if (type == null)
+            type= getTypeInfo(metaType, arrayValue.getValue());
          Object array = newArrayInstance(type, arrayValue.getLength());
          for (int i = 0; i < Array.getLength(array); i++)
          {
@@ -488,7 +506,7 @@
       if (type instanceof ClassInfo)
          elementType = ((ClassInfo)type).getComponentType();
       else
-         elementType = getClassInfo(element.getMetaType(), array);
+         elementType = getTypeInfo(element.getMetaType(), array);
       return unwrap(element, elementType);
    }
 
@@ -545,18 +563,47 @@
     * @param array the array to fill
     * @return class info
     */
-   protected ClassInfo getClassInfo(MetaType metaType, Object array)
+   protected TypeInfo getTypeInfo(MetaType metaType, Object array)
    {
       if (metaType == null)
          throw new IllegalArgumentException("Null meta type, cannot determine class name.");
       if (array == null)
          throw new IllegalArgumentException("Null array, cannot determine classloader.");
 
+      // get the classloader from the array we plan to fill
+      ClassLoader cl = array.getClass().getClassLoader();
+      return getTypeInfo(metaType, cl);
+   }
+
+   /**
+    * Get the class info from meta type.
+    *
+    * @param metaType the meta type
+    * @param cl the classloader
+    * @return class info
+    */
+   protected TypeInfo getTypeInfo(MetaType metaType, ClassLoader cl)
+   {
+      if (cl == null)
+         cl = Thread.currentThread().getContextClassLoader();
+
       try
       {
-         // get the classloader from the array we plan to fill
-         ClassLoader cl = array.getClass().getClassLoader();
-         return configuration.getClassInfo(metaType.getClassName(), cl);
+         if (metaType.isArray())
+         {
+            ArrayMetaType arrayMetaType = (ArrayMetaType)metaType;
+            MetaType elementMetaType = arrayMetaType.getElementType();
+            TypeInfo elementTypeInfo = configuration.getClassInfo(elementMetaType.getTypeName(), cl);
+            int dimension = arrayMetaType.getDimension() - 1; // minus 1, since we already use first in next line
+            TypeInfo typeInfo = elementTypeInfo.getArrayType();
+            while(dimension > 0)
+            {
+               typeInfo = typeInfo.getArrayType();
+               dimension--;
+            }
+            return typeInfo;
+         }
+         return configuration.getClassInfo(metaType.getTypeName(), cl);
       }
       catch (ClassNotFoundException e)
       {

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/AbstractMetaValueFactoryTest.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/AbstractMetaValueFactoryTest.java	2007-10-17 12:33:52 UTC (rev 66214)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/AbstractMetaValueFactoryTest.java	2007-10-17 12:39:07 UTC (rev 66215)
@@ -105,9 +105,20 @@
     * Unwrap meta value.
     *
     * @param value the meta value
-    * @param type expecyed type
     * @return unwrapped 
     */
+   protected Object unwrapMetaValue(MetaValue value)
+   {
+      return metaValueFactory.unwrap(value);
+   }
+
+   /**
+    * Unwrap meta value.
+    *
+    * @param value the meta value
+    * @param type expecyed type
+    * @return unwrapped
+    */
    protected Object unwrapMetaValue(MetaValue value, Type type)
    {
       return metaValueFactory.unwrap(value, type);

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java	2007-10-17 12:33:52 UTC (rev 66214)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java	2007-10-17 12:39:07 UTC (rev 66215)
@@ -58,18 +58,22 @@
 
    public void testSimpleUnwrap() throws Exception
    {
-      checkSingle(123);
-      checkSingle(new Date());
+      checkSingle(123, true);
+      checkSingle(123, false);
+      checkSingle(new Date(), true);
+      checkSingle(new Date(), false);
    }
 
    public void testEnumUnwrap() throws Exception
    {
-      checkSingle(TestEnum.ONE);
+      checkSingle(TestEnum.ONE, true);
+      checkSingle(TestEnum.ONE, false);
    }
 
    public void testGenericUnwrap() throws Exception
    {
-      checkSingle(new TestGeneric("123"));
+      checkSingle(new TestGeneric("123"), true);
+      checkSingle(new TestGeneric("123"), false);
    }
 
    public void testArrayUnwrap() throws Exception
@@ -94,82 +98,126 @@
          }
       }
 
-      checkArray(shorts, new Asserter()
+      checkArray(shorts, true, new Asserter()
       {
          public boolean assertArray(final Object original, final Object unwrapped)
          {
             return Arrays.equals((short[])original, (short[])unwrapped);
          }
       });
+      checkArray(shorts, false, new Asserter()
+      {
+         public boolean assertArray(final Object original, final Object unwrapped)
+         {
+            short[] so = (short[])original;
+            Short[] SO = (Short[])unwrapped;
+            for (int i = 0; i < so.length; i++)
+               if (so[i] != SO[i])
+                  return false;
+            return true;
+         }
+      });
 
-      checkArray(doubles, new Asserter()
+      checkArray(doubles, true, new Asserter()
       {
          public boolean assertArray(final Object original, final Object unwrapped)
          {
             return Arrays.equals((Double[])original, (Double[])unwrapped);
          }
       });
+      checkArray(doubles, false, new Asserter()
+      {
+         public boolean assertArray(final Object original, final Object unwrapped)
+         {
+            return Arrays.equals((Double[])original, (Double[])unwrapped);
+         }
+      });
 
-      checkArray(enums, new Asserter()
+      checkArray(enums, true, new Asserter()
       {
          public boolean assertArray(final Object original, final Object unwrapped)
          {
             return Arrays.equals((Object[])original, (Object[])unwrapped);
          }
       });
+      checkArray(enums, false, new Asserter()
+      {
+         public boolean assertArray(final Object original, final Object unwrapped)
+         {
+            return Arrays.equals((Object[])original, (Object[])unwrapped);
+         }
+      });
 
-      checkArray(generics, new Asserter()
+      checkArray(generics, true, new Asserter()
       {
          public boolean assertArray(final Object original, final Object unwrapped)
          {
             return Arrays.equals((Object[])original, (Object[])unwrapped);
          }
       });
+      checkArray(generics, false, new Asserter()
+      {
+         public boolean assertArray(final Object original, final Object unwrapped)
+         {
+            return Arrays.equals((Object[])original, (Object[])unwrapped);
+         }
+      });
 
-      checkArray(integers, new Asserter()
+      Asserter integersAsserter = new Asserter()
       {
          public boolean assertArray(final Object original, final Object unwrapped)
          {
             Integer[][] first = (Integer[][])original;
             Integer[][] second = (Integer[][])unwrapped;
-            for(int i = 0; i < first.length; i++)
-               for(int j = 0; j < first[0].length; j++)
+            for (int i = 0; i < first.length; i++)
+               for (int j = 0; j < first[0].length; j++)
                   if (first[i][j].equals(second[i][j]) == false)
                      return false;
             return true;
          }
-      });
+      };
+      checkArray(integers, true, integersAsserter);
+      checkArray(integers, false, integersAsserter);
 
-      checkArray(triple, new Asserter()
+      Asserter tripleAsserter = new Asserter()
       {
          public boolean assertArray(final Object original, final Object unwrapped)
          {
             Integer[][][] first = (Integer[][][])original;
             Integer[][][] second = (Integer[][][])unwrapped;
-            for(int i = 0; i < first.length; i++)
-               for(int j = 0; j < first[0].length; j++)
-                  for(int k = 0; k < first[0][0].length; k++)
-                  if (first[i][j][k].equals(second[i][j][k]) == false)
-                     return false;
+            for (int i = 0; i < first.length; i++)
+               for (int j = 0; j < first[0].length; j++)
+                  for (int k = 0; k < first[0][0].length; k++)
+                     if (first[i][j][k].equals(second[i][j][k]) == false)
+                        return false;
             return true;
          }
-      });
+      };
+      checkArray(triple, true, tripleAsserter);
+      checkArray(triple, false, tripleAsserter);
    }
 
-   protected void checkSingle(Object object)
+   protected void checkSingle(Object object, boolean typeInfoFromObject)
    {
       MetaValue metaValue = createMetaValue(object);
       assertNotNull(metaValue);
-      Object unwrapped = unwrapMetaValue(metaValue, object.getClass());
+      Object unwrapped;
+      if (typeInfoFromObject)
+         unwrapped = unwrapMetaValue(metaValue, object.getClass());
+      else
+         unwrapped = unwrapMetaValue(metaValue);
       assertEquals(object, unwrapped);
    }
 
-   protected void checkArray(Object object, Asserter asserter)
+   protected void checkArray(Object object, boolean typeInfoFromObject, Asserter asserter)
    {
       MetaValue metaValue = createMetaValue(object);
       assertNotNull(metaValue);
-      Class<? extends Object> clazz = object.getClass();
-      Object unwrapped = unwrapMetaValue(metaValue, clazz);
+      Object unwrapped;
+      if (typeInfoFromObject)
+         unwrapped = unwrapMetaValue(metaValue, object.getClass());
+      else
+         unwrapped = unwrapMetaValue(metaValue);
       assertTrue("Different arrays.", asserter.assertArray(object, unwrapped));
    }
 




More information about the jboss-cvs-commits mailing list