[jboss-cvs] JBossAS SVN: r68073 - in projects/microcontainer/trunk: metatype/src/main/org/jboss/metatype/api/types and 6 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Dec 8 19:31:08 EST 2007


Author: alesj
Date: 2007-12-08 19:31:08 -0500 (Sat, 08 Dec 2007)
New Revision: 68073

Added:
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/CollectionMetaType.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValue.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValueSupport.java
Modified:
   projects/microcontainer/trunk/deployers-vfs/src/tests/org/jboss/test/deployers/vfs/deployer/bean/test/BeanManagedDeploymentUnitTestCase.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/AbstractMetaType.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/ArrayMetaType.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/MetaType.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValueSupport.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/types/DefaultMetaTypeFactory.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/AbstractMetaTypeTest.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/factory/test/CollectionMetaTypeFactoryUnitTestCase.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/CollectionValueFactoryUnitTestCase.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java
Log:
Introduction of CollectionValue/MetaType.

Modified: projects/microcontainer/trunk/deployers-vfs/src/tests/org/jboss/test/deployers/vfs/deployer/bean/test/BeanManagedDeploymentUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/deployers-vfs/src/tests/org/jboss/test/deployers/vfs/deployer/bean/test/BeanManagedDeploymentUnitTestCase.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/deployers-vfs/src/tests/org/jboss/test/deployers/vfs/deployer/bean/test/BeanManagedDeploymentUnitTestCase.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -25,7 +25,6 @@
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
-
 import org.jboss.deployers.vfs.deployer.kernel.BeanDeployer;
 import org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer;
 import org.jboss.deployers.vfs.deployer.kernel.KernelDeploymentDeployer;
@@ -34,13 +33,10 @@
 import org.jboss.managed.api.ManagedObject;
 import org.jboss.managed.api.ManagedProperty;
 import org.jboss.managed.api.factory.ManagedObjectFactory;
-import org.jboss.managed.plugins.factory.AbstractManagedObjectFactory;
 import org.jboss.metatype.api.types.ArrayMetaType;
 import org.jboss.metatype.api.values.ArrayValue;
-import org.jboss.metatype.api.values.CompositeValue;
+import org.jboss.metatype.api.values.CollectionValue;
 import org.jboss.metatype.api.values.GenericValue;
-import org.jboss.metatype.api.values.MetaValue;
-import org.jboss.metatype.api.values.SimpleValue;
 
 /**
  * Tests of bean deployment ManagedObject/ManagedDeployment creation.
@@ -91,7 +87,8 @@
       log.info("beanFactories.ManagedProperties: "+beanFactoriesMO.getProperties());
       ManagedProperty properties = beanFactoriesMO.getProperty("properties");
       assertNotNull(properties);
-      ArrayValue<CompositeValue> propertiesArray = ArrayValue.class.cast(properties.getValue());
+      CollectionValue propertiesArray = CollectionValue.class.cast(properties.getValue());
+      assertNotNull(propertiesArray);
    }
 
    public void testBeanManagedDeployment()

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/AbstractMetaType.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/AbstractMetaType.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/AbstractMetaType.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -155,6 +155,11 @@
       return array;
    }
 
+   public boolean isCollection()
+   {
+      return false;
+   }
+
    public abstract boolean isValue(Object obj);
 
    /**
@@ -177,31 +182,34 @@
       if (description == null || description.trim().equals(""))
          throw new IllegalArgumentException("null or empty description");
 
-      // Calculate the underlying class and whether this is an array
-      String testClassName = getBaseClassName(className);
-      if (testClassName == null)
-         throw new IllegalArgumentException("Invalid array declaration (see the javadocs for java.lang.Class): " + className);
-      if (testClassName.equals(className) == false)
-         array = true;
+      if (isCollection() == false)
+      {
+         // Calculate the underlying class and whether this is an array
+         String testClassName = getBaseClassName(className);
+         if (testClassName == null)
+            throw new IllegalArgumentException("Invalid array declaration (see the javadocs for java.lang.Class): " + className);
+         if (testClassName.equals(className) == false)
+            array = true;
 
-      // Check the underlying class
-      boolean ok = false;
-      for (int i = 0; i < ALLOWED_CLASSNAMES.size(); i++)
-      {
-         if (testClassName.equals(ALLOWED_CLASSNAMES.get(i)))
+         // Check the underlying class
+         boolean ok = false;
+         for (int i = 0; i < ALLOWED_CLASSNAMES.size(); i++)
          {
-            ok = true;
-            break;
+            if (testClassName.equals(ALLOWED_CLASSNAMES.get(i)))
+            {
+               ok = true;
+               break;
+            }
          }
-      }
-      if (ok == false)
-      {
-         // Check for a primative array type
-         int index = className.lastIndexOf('[');
-         if (index == className.length()-2)
-            ok = ArrayMetaType.isPrimitiveEncoding(className.substring(index+1));
          if (ok == false)
-            throw new IllegalArgumentException("Not a MetaType allowed class name: " + className);
+         {
+            // Check for a primative array type
+            int index = className.lastIndexOf('[');
+            if (index == className.length()-2)
+               ok = ArrayMetaType.isPrimitiveEncoding(className.substring(index+1));
+            if (ok == false)
+               throw new IllegalArgumentException("Not a MetaType allowed class name: " + className);
+         }
       }
 
       // Looks ok

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/ArrayMetaType.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/ArrayMetaType.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/ArrayMetaType.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -412,7 +412,8 @@
       buffer.append(getTypeName());
       buffer.append(" dims=");
       buffer.append(dimension);
-      buffer.append(" elementType=" + elementType);
+      buffer.append(" elementType=");
+      buffer.append(elementType);
       cachedToString = buffer.toString();
       return cachedToString;
    }
@@ -422,6 +423,7 @@
     * 
     * @param elements the elements
     * @param dimension the dimension
+    * @return true if elements match, false otherwise
     */
    private boolean recursiveCheck(Object[] elements, int dimension)
    {

Added: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/CollectionMetaType.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/CollectionMetaType.java	                        (rev 0)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/CollectionMetaType.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -0,0 +1,159 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metatype.api.types;
+
+import java.io.Serializable;
+import java.util.Collection;
+
+import org.jboss.metatype.api.values.CollectionValue;
+
+/**
+ * CollectionMetaType.
+ *
+ * @param <T> exact type
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class CollectionMetaType<T extends Serializable> extends AbstractMetaType
+{
+   /** The serialVersionUID */
+   private static final long serialVersionUID = -2062790692152055156L;
+
+   /** The element type for the array */
+   private MetaType elementType;
+
+   /** Cached hash code */
+   private transient int cachedHashCode = Integer.MIN_VALUE;
+
+   /** Cached string representation */
+   private transient String cachedToString = null;
+
+   public CollectionMetaType(String className, MetaType<T> elementType)
+   {
+      super(className);
+      if (elementType == null)
+         throw new IllegalArgumentException("Null element type");
+      this.elementType = elementType;
+   }
+
+   public CollectionMetaType(String className, String description, MetaType<T> elementType)
+   {
+      super(className, description);
+      if (elementType == null)
+         throw new IllegalArgumentException("Null element type");
+      this.elementType = elementType;
+   }
+
+   public CollectionMetaType(String className, String typeName, String description, MetaType<T> elementType)
+   {
+      super(className, typeName, description);
+      if (elementType == null)
+         throw new IllegalArgumentException("Null element type");
+      this.elementType = elementType;
+   }
+
+   public boolean isCollection()
+   {
+      return true;
+   }
+
+   /**
+    * Get collection meta type.
+    *
+    * @param collectionType the element meta type
+    * @param elementType the element meta type
+    * @return collection meta type
+    */
+   public static <E extends Serializable> CollectionMetaType<E> getCollectionType(String collectionType, MetaType<E> elementType)
+   {
+      return new CollectionMetaType<E>(collectionType, elementType);
+   }
+
+   /**
+    * Get the meta type of the array elements
+    *
+    * @return the element type
+    */
+   public MetaType getElementType()
+   {
+      return elementType;
+   }
+
+   @Override
+   @SuppressWarnings("unchecked")
+   public boolean isValue(Object obj)
+   {
+      if (obj == null)
+         return false;
+
+      if (obj instanceof Collection == false && (obj instanceof CollectionValue) == false)
+         return false;
+
+      if (obj instanceof CollectionValue)
+      {
+         CollectionValue cv = (CollectionValue)obj;
+         return equals(cv.getMetaType());
+      }
+
+      Collection collection = (Collection)obj;
+      for(Object element : collection)
+      {
+         if (elementType.isValue(element) == false)
+            return false;
+      }
+
+      return true;
+   }
+
+   @Override
+   public boolean equals(Object obj)
+   {
+      if (this == obj)
+         return true;
+      if (obj == null || obj instanceof CollectionMetaType == false)
+         return false;
+      CollectionMetaType other = (CollectionMetaType) obj;
+      return getTypeName().equals(other.getTypeName()) && getElementType().equals(other.getElementType());
+   }
+
+   @Override
+   public int hashCode()
+   {
+      if (cachedHashCode != Integer.MIN_VALUE)
+         return cachedHashCode;
+      cachedHashCode = getTypeName().hashCode() + getElementType().hashCode();
+      return cachedHashCode;
+   }
+
+   @Override
+   public String toString()
+   {
+      if (cachedToString != null)
+         return cachedToString;
+      StringBuilder buffer = new StringBuilder(CollectionMetaType.class.getSimpleName());
+      buffer.append("{type=");
+      buffer.append(getTypeName());
+      buffer.append(" elementType=");
+      buffer.append(elementType);
+      cachedToString = buffer.toString();
+      return cachedToString;
+   }
+}

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/MetaType.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/MetaType.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/MetaType.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -125,6 +125,13 @@
    boolean isArray();
 
    /**
+    * Retrieve whether the class name of the type is a collection
+    *
+    * @return true when it is a collection or false otherwise
+    */
+   boolean isCollection();
+
+   /**
     * Retrieve whether the class name of the type is an enum
     *
     * @return true when it is an enum or false otherwise

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValueSupport.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValueSupport.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValueSupport.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -239,10 +239,10 @@
          return index < length;
       }
 
+      @SuppressWarnings("unchecked")
       public T next()
       {
-         Object next = Array.get(array, index ++);
-         return (T) next;
+         return (T) Array.get(array, index ++);
       }
 
       public void remove()

Added: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValue.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValue.java	                        (rev 0)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValue.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -0,0 +1,47 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metatype.api.values;
+
+import org.jboss.metatype.api.types.CollectionMetaType;
+
+/**
+ * CollectionValue.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface CollectionValue extends MetaValue, Iterable<MetaValue>
+{
+   CollectionMetaType getMetaType();
+
+   /**
+    * Get the underlying elements.
+    *
+    * @return the underlying elements
+    */
+   public MetaValue[] getElements();
+
+   /**
+    * Get the size of the collection.
+    * @return size of the colelction.
+    */
+   public int getSize();
+}

Added: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValueSupport.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValueSupport.java	                        (rev 0)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValueSupport.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -0,0 +1,189 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metatype.api.values;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.jboss.metatype.api.types.CollectionMetaType;
+
+/**
+ * CollectionValueSupport.
+ *
+ * TODO tests
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class CollectionValueSupport extends AbstractMetaValue implements CollectionValue
+{
+   /** The serialVersionUID */
+   private static final long serialVersionUID = 1131827130033538066L;
+
+   /** The collection meta type */
+   private CollectionMetaType metaType;
+
+   /** The elements */
+   private MetaValue[] elements;
+
+   /**
+    * Create a new CollectionValueSupport.
+    *
+    * @param metaType the collection meta type
+    * @throws IllegalArgumentException for a null array MetaType
+    */
+   public CollectionValueSupport(CollectionMetaType metaType)
+   {
+      if (metaType == null)
+         throw new IllegalArgumentException("Null collection meta type");
+      this.metaType = metaType;
+   }
+
+   /**
+    * Create a new ArrayValueSupport.
+    *
+    * @param metaType the collection meta type
+    * @param elements the elements
+    * @throws IllegalArgumentException for a null array MetaType
+    */
+   public CollectionValueSupport(CollectionMetaType metaType, MetaValue[] elements)
+   {
+      this(metaType);
+      this.elements = elements;
+   }
+
+   public CollectionMetaType getMetaType()
+   {
+      return metaType;
+   }
+
+   /**
+    * Get the value.
+    *
+    * @return the value.
+    */
+   public MetaValue[] getElements()
+   {
+      return elements;
+   }
+
+   /**
+    * Get the size of the collection.
+    * @return size of the collection.
+    */
+   public int getSize()
+   {
+      return elements != null ? elements.length : 0;
+   }
+
+   public Iterator<MetaValue> iterator()
+   {
+      return new ElementsIterator(elements);
+   }
+
+   /**
+    * Set the value.
+    *
+    * @param elements the elements.
+    */
+   public void setElements(MetaValue[] elements)
+   {
+      this.elements = elements;
+   }
+
+   @Override
+   @SuppressWarnings("unchecked")
+   public boolean equals(Object obj)
+   {
+      if (obj == this)
+         return true;
+
+      if (obj == null || obj instanceof CollectionValue == false)
+         return false;
+
+      CollectionValue other = (CollectionValue) obj;
+      if (metaType.equals(other.getMetaType()) == false)
+         return false;
+
+      Object[] otherElements = other.getElements();
+      if (elements == null)
+         return otherElements == null;
+
+      return otherElements != null && Arrays.equals(elements, otherElements);
+   }
+
+   @Override
+   public int hashCode()
+   {
+      if (elements == null)
+         return 0;
+      return elements.hashCode();
+   }
+
+   @Override
+   public String toString()
+   {
+      return metaType + ": " + elements;
+   }
+
+   @Override
+   @SuppressWarnings("unchecked")
+   public MetaValue clone()
+   {
+      CollectionValueSupport result = (CollectionValueSupport) super.clone();
+      int size = getSize();
+      if (size > 0)
+      {
+         result.elements = new MetaValue[size];
+         System.arraycopy(elements, 0, result.elements, 0, size);
+      }
+      return result;
+   }
+
+   private static class ElementsIterator implements Iterator<MetaValue>
+   {
+      private int index;
+      private int length;
+      private MetaValue[] elements;
+
+      ElementsIterator(MetaValue[] elements)
+      {
+         this.elements = elements;
+         this.index = 0;
+         this.length = elements != null ? elements.length : 0;
+      }
+
+      public boolean hasNext()
+      {
+         return index < length;
+      }
+
+      public MetaValue next()
+      {
+         return elements[index++];
+      }
+
+      public void remove()
+      {
+         throw new UnsupportedOperationException();
+      }
+
+   }
+}

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/types/DefaultMetaTypeFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/types/DefaultMetaTypeFactory.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/types/DefaultMetaTypeFactory.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -48,6 +48,7 @@
 import org.jboss.metatype.api.types.MetaTypeFactory;
 import org.jboss.metatype.api.types.SimpleMetaType;
 import org.jboss.metatype.api.types.TableMetaType;
+import org.jboss.metatype.api.types.CollectionMetaType;
 import org.jboss.metatype.spi.types.MetaTypeBuilder;
 import org.jboss.reflect.spi.ArrayInfo;
 import org.jboss.reflect.spi.ClassInfo;
@@ -264,7 +265,8 @@
     * @param typeInfo the type info
     * @return the metatype
     */
-   public ArrayMetaType generateCollection(ClassInfo typeInfo)
+   @SuppressWarnings("unchecked")
+   public CollectionMetaType generateCollection(ClassInfo typeInfo)
    {
       TypeInfo elementType = objectTypeInfo;
       
@@ -273,7 +275,7 @@
          elementType = types[0];
       
       MetaType elementMetaType = resolve(elementType);
-      return new ArrayMetaType(1, elementMetaType);
+      return new CollectionMetaType(typeInfo.getName(), elementMetaType);
    }
    
    /**

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-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -47,6 +47,7 @@
 import org.jboss.metatype.api.types.MetaTypeFactory;
 import org.jboss.metatype.api.types.SimpleMetaType;
 import org.jboss.metatype.api.types.TableMetaType;
+import org.jboss.metatype.api.types.CollectionMetaType;
 import org.jboss.metatype.api.values.ArrayValue;
 import org.jboss.metatype.api.values.ArrayValueSupport;
 import org.jboss.metatype.api.values.CompositeValue;
@@ -61,6 +62,8 @@
 import org.jboss.metatype.api.values.SimpleValueSupport;
 import org.jboss.metatype.api.values.TableValue;
 import org.jboss.metatype.api.values.TableValueSupport;
+import org.jboss.metatype.api.values.CollectionValue;
+import org.jboss.metatype.api.values.CollectionValueSupport;
 import org.jboss.metatype.plugins.types.DefaultMetaTypeFactory;
 import org.jboss.metatype.spi.values.MetaValueBuilder;
 import org.jboss.reflect.spi.ArrayInfo;
@@ -81,7 +84,7 @@
    
    /** The configuration */
    private static Configuration configuration;
-   
+
    static
    {
       configuration = AccessController.doPrivileged(new PrivilegedAction<Configuration>()
@@ -105,6 +108,9 @@
    /** The builders */
    private Map<Class, WeakReference<MetaValueBuilder>> builders = new WeakHashMap<Class, WeakReference<MetaValueBuilder>>();
  
+   /** The Object type info */
+   private static final TypeInfo OBJECT_TYPE_INFO = configuration.getTypeInfo(Object.class);
+
    public void setBuilder(Class<?> clazz, MetaValueBuilder builder)
    {
       synchronized (builders)
@@ -153,7 +159,7 @@
     * @param type the type
     * @param value the value
     * @param mapping the mapping
-    * @return the enum value
+    * @return the generic value
     */
    public static GenericValue createGenericValue(GenericMetaType type, Object value, Map<Object, MetaValue> mapping)
    {
@@ -169,6 +175,34 @@
    }
 
    /**
+    * Create a collection value
+    *
+    * @param type the type
+    * @param value the value
+    * @param mapping the mapping
+    * @return the generic value
+    */
+   public CollectionValue createCollectionValue(CollectionMetaType type, Object value, Map<Object, MetaValue> mapping)
+   {
+      if (value == null)
+         return null;
+
+      Collection collection = (Collection)value;
+      MetaValue[] elements = new MetaValue[collection.size()];
+      int i = 0;
+      for(Object ce : collection)
+      {
+         // recalculate element info, since usually more deterministic
+         TypeInfo typeInfo = configuration.getTypeInfo(ce.getClass());
+         MetaType metaType = metaTypeFactory.resolve(typeInfo);
+         elements[i++] = internalCreate(ce, typeInfo, metaType);             
+      }
+      CollectionValue result = new CollectionValueSupport(type, elements);
+      mapping.put(value, result);
+      return result;
+   }
+
+   /**
     * Transform a primitive array into an Object[]. Converts
     * a primitive array like char[] to Object[]. 
     * 
@@ -271,11 +305,6 @@
          else
             oldArray = (Object[]) value;
       }
-      else if (classInfo.isCollection())
-      {
-         Collection c = (Collection) value;
-         oldArray = c.toArray();
-      }
       else
          throw new UnsupportedOperationException("Cannot construct array for " + value.getClass());
       
@@ -505,6 +534,11 @@
          CompositeValue compositeValue = (CompositeValue)metaValue;
          return unwrapComposite(compositeValue, type);
       }
+      else if (metaType.isCollection())
+      {
+         CollectionValue collectionValue = (CollectionValue)metaValue;
+         return unwrapCollection(collectionValue, type);
+      }
 
       throw new IllegalArgumentException("Unsupported meta value: " + metaValue);
    }
@@ -621,6 +655,46 @@
    }
 
    /**
+    * Unwrap collection meta value.
+    *
+    * @param collectionValue the collection value
+    * @param type the type info
+    * @return unwrapped collection
+    */
+   protected Object unwrapCollection(CollectionValue collectionValue, TypeInfo type)
+   {
+      try
+      {
+         BeanInfo collectionInfo;
+         if (type != null)
+         {
+            collectionInfo = configuration.getBeanInfo(type);
+         }
+         else
+         {
+            MetaType metaType = collectionValue.getMetaType();
+            collectionInfo = configuration.getBeanInfo(metaType.getTypeName(), Thread.currentThread().getContextClassLoader());
+         }
+         Collection collection = (Collection)createNewInstance(collectionInfo);
+         Iterator<MetaValue> iter = collectionValue.iterator();
+         while (iter.hasNext())
+         {
+            MetaValue metaValue = iter.next();
+            TypeInfo componentType = collectionInfo.getClassInfo().getComponentType();
+            // try better
+            if (OBJECT_TYPE_INFO.equals(componentType))
+               componentType = getTypeInfo(metaValue.getMetaType(), null);
+            collection.add(unwrap(metaValue, componentType));
+         }
+         return collection;
+      }
+      catch (Throwable t)
+      {
+         throw new UndeclaredThrowableException(t);
+      }
+   }
+
+   /**
     * Create new instance.
     *
     * @param beanInfo the bean info
@@ -775,7 +849,9 @@
                result = createTableValue((TableMetaType) metaType, (Map) value, mapping);
             else if (metaType.isGeneric())
                result = createGenericValue((GenericMetaType) metaType, value, mapping);
-            else 
+            else if (metaType.isCollection())
+               result = createCollectionValue((CollectionMetaType) metaType, value, mapping);
+            else
                throw new IllegalStateException("Unknown metaType: " + metaType);
          }
          return result;

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/AbstractMetaTypeTest.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/AbstractMetaTypeTest.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/AbstractMetaTypeTest.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -30,6 +30,7 @@
 import org.jboss.metatype.api.types.MetaType;
 import org.jboss.metatype.api.types.SimpleMetaType;
 import org.jboss.metatype.api.types.TableMetaType;
+import org.jboss.metatype.api.types.CollectionMetaType;
 import org.jboss.metatype.api.values.MetaValue;
 import org.jboss.metatype.api.values.SimpleValue;
 import org.jboss.metatype.api.values.SimpleValueSupport;
@@ -298,6 +299,19 @@
    }
 
    /**
+    * Test an array type
+    *
+    * @param expected the expected
+    * @param actual the actual
+    * @throws Exception for any problem
+    */
+   protected void testCollection(CollectionMetaType expected, CollectionMetaType actual) throws Exception
+   {
+      getLog().debug("Collection MetaType: className=" + actual.getClassName() + " typeName=" + actual.getTypeName() + " description=" + actual.getDescription());
+      assertEquals(expected, actual);
+   }
+
+   /**
     * Test a table type
     * 
     * @param expected the expected

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/factory/test/CollectionMetaTypeFactoryUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/factory/test/CollectionMetaTypeFactoryUnitTestCase.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/factory/test/CollectionMetaTypeFactoryUnitTestCase.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -28,8 +28,7 @@
 import java.util.Set;
 
 import junit.framework.Test;
-
-import org.jboss.metatype.api.types.ArrayMetaType;
+import org.jboss.metatype.api.types.CollectionMetaType;
 import org.jboss.metatype.api.types.MetaType;
 import org.jboss.test.metatype.types.factory.support.TestSimpleComposite;
 
@@ -128,7 +127,7 @@
     */
    public void testSimpleCollection() throws Exception
    {
-      testCollection("simpleCollection", String.class);
+      testCollection("simpleCollection", String.class, Collection.class);
    }
 
    /**
@@ -138,7 +137,7 @@
     */
    public void testSimpleCompositeCollection() throws Exception
    {
-      testCollection("simpleCompositeCollection", TestSimpleComposite.class);
+      testCollection("simpleCompositeCollection", TestSimpleComposite.class, Collection.class);
    }
 
    /**
@@ -148,7 +147,7 @@
     */
    public void testSimpleList() throws Exception
    {
-      testCollection("simpleList", String.class);
+      testCollection("simpleList", String.class, List.class);
    }
 
    /**
@@ -158,7 +157,7 @@
     */
    public void testSimpleCompositeList() throws Exception
    {
-      testCollection("simpleCompositeList", TestSimpleComposite.class);
+      testCollection("simpleCompositeList", TestSimpleComposite.class, List.class);
    }
 
    /**
@@ -168,7 +167,7 @@
     */
    public void testSimpleSet() throws Exception
    {
-      testCollection("simpleSet", String.class);
+      testCollection("simpleSet", String.class, Set.class);
    }
 
    /**
@@ -178,7 +177,7 @@
     */
    public void testSimpleCompositeSet() throws Exception
    {
-      testCollection("simpleCompositeSet", TestSimpleComposite.class);
+      testCollection("simpleCompositeSet", TestSimpleComposite.class, Set.class);
    }
    
    /**
@@ -186,16 +185,18 @@
     * 
     * @param methodName the method name to lookup the connection signature
     * @param elementClass the expected element type of the collection
+    * @param collectionClass the expected type of the collection
     * @throws Exception for any problem
     */
-   protected void testCollection(String methodName, Type elementClass) throws Exception
+   @SuppressWarnings("unchecked")
+   protected void testCollection(String methodName, Type elementClass, Class collectionClass) throws Exception
    {
-      Method method = getClass().getMethod(methodName, null);
+      Method method = getClass().getMethod(methodName);
       Type collectionType = method.getGenericReturnType();
       MetaType result = resolve(collectionType);
-      ArrayMetaType actual = assertInstanceOf(result, ArrayMetaType.class);
+      CollectionMetaType actual = assertInstanceOf(result, CollectionMetaType.class);
       MetaType elementType = resolve(elementClass);
-      ArrayMetaType expected = new ArrayMetaType(1, elementType);
-      testArray(expected, actual);
+      CollectionMetaType expected = new CollectionMetaType(collectionClass.getName(), elementType);
+      testCollection(expected, actual);
    }
 }

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/CollectionValueFactoryUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/CollectionValueFactoryUnitTestCase.java	2007-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/CollectionValueFactoryUnitTestCase.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -29,18 +29,17 @@
 import java.util.List;
 import java.util.Set;
 
-import org.jboss.metatype.api.types.ArrayMetaType;
+import junit.framework.Test;
+import org.jboss.metatype.api.types.CollectionMetaType;
 import org.jboss.metatype.api.types.CompositeMetaType;
-import org.jboss.metatype.api.values.ArrayValue;
-import org.jboss.metatype.api.values.ArrayValueSupport;
+import org.jboss.metatype.api.values.CollectionValue;
+import org.jboss.metatype.api.values.CollectionValueSupport;
 import org.jboss.metatype.api.values.CompositeValue;
 import org.jboss.metatype.api.values.CompositeValueSupport;
 import org.jboss.metatype.api.values.MetaValue;
 import org.jboss.metatype.api.values.SimpleValueSupport;
 import org.jboss.test.metatype.values.factory.support.TestSimpleComposite;
 
-import junit.framework.Test;
-
 /**
  * CollectionValueFactoryUnitTestCase.
  * 
@@ -216,15 +215,15 @@
     */
    public void testSimple(String methodName, Collection<String> collection) throws Exception
    {
-      Method method = getClass().getMethod(methodName, null); 
+      Method method = getClass().getMethod(methodName);
       Type type = method.getGenericReturnType();
 
-      ArrayMetaType arrayType = assertInstanceOf(resolve(type), ArrayMetaType.class);
+      CollectionMetaType arrayType = assertInstanceOf(resolve(type), CollectionMetaType.class);
       MetaValue[] metaArray = { SimpleValueSupport.wrap("Hello"), SimpleValueSupport.wrap("Goodbye") };
-      ArrayValueSupport expected = new ArrayValueSupport(arrayType, metaArray);
+      CollectionValueSupport expected = new CollectionValueSupport(arrayType, metaArray);
       
       MetaValue result = createMetaValue(collection, type);
-      ArrayValue actual = assertInstanceOf(result, ArrayValue.class);
+      CollectionValue actual = assertInstanceOf(result, CollectionValue.class);
       getLog().debug("Collection Value: " + actual);
       assertEquals(expected, actual);
    }
@@ -238,10 +237,10 @@
     */
    public void testComposite(String methodName, Collection<TestSimpleComposite> collection) throws Exception
    {
-      Method method = getClass().getMethod(methodName, null); 
+      Method method = getClass().getMethod(methodName);
       Type type = method.getGenericReturnType();
       
-      ArrayMetaType arrayType = assertInstanceOf(resolve(type), ArrayMetaType.class);
+      CollectionMetaType arrayType = assertInstanceOf(resolve(type), CollectionMetaType.class);
       CompositeMetaType compositeType = assertInstanceOf(resolve(TestSimpleComposite.class), CompositeMetaType.class);
       String[] itemNames = { "something" };
       MetaValue[] itemValues = { SimpleValueSupport.wrap("Hello") };
@@ -249,10 +248,10 @@
       itemValues = new MetaValue[] { SimpleValueSupport.wrap("Goodbye") };
       CompositeValue goodbyeValue = new CompositeValueSupport(compositeType, itemNames, itemValues);
       MetaValue[] metaArray = { helloValue, goodbyeValue };
-      ArrayValueSupport expected = new ArrayValueSupport(arrayType, metaArray);
+      CollectionValueSupport expected = new CollectionValueSupport(arrayType, metaArray);
       
       MetaValue result = createMetaValue(collection, type);
-      ArrayValue actual = assertInstanceOf(result, ArrayValue.class);
+      CollectionValue actual = assertInstanceOf(result, CollectionValue.class);
       getLog().debug("Collection Value: " + actual);
       assertEquals(expected, actual);
    }

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-12-08 21:10:47 UTC (rev 68072)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java	2007-12-09 00:31:08 UTC (rev 68073)
@@ -21,8 +21,11 @@
 */
 package org.jboss.test.metatype.values.factory.test;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.Date;
-import java.util.Arrays;
 
 import junit.framework.Test;
 import org.jboss.metatype.api.values.MetaValue;
@@ -88,6 +91,30 @@
       checkSingle(new TestRecursiveSimpleComposite("something", composite), false);
    }
 
+   public void testCollectionUnwrap() throws Exception
+   {
+      Integer i1 = 123;
+      Integer i2 = 123;
+      checkCollection(new ArrayList<Integer>(), i1, i2);
+      checkCollection(new HashSet<Integer>(), i1, i2);
+
+      TestEnum one = TestEnum.ONE;
+      TestEnum two = TestEnum.TWO;
+      TestEnum three = TestEnum.THREE;
+      checkCollection(new ArrayList<TestEnum>(), one, two, three, one);
+      checkCollection(new HashSet<TestEnum>(), one, two, three, one);
+
+      TestGeneric g1 = new TestGeneric("123");
+      TestGeneric g2 = new TestGeneric("123");
+      checkCollection(new ArrayList<TestGeneric>(), g1, g2);
+      checkCollection(new HashSet<TestGeneric>(), g1, g2);
+
+      TestSimpleComposite c1 = new TestSimpleComposite("123");
+      TestSimpleComposite c2 = new TestSimpleComposite("123");
+      checkCollection(new ArrayList<TestSimpleComposite>(), c1, c2);
+      checkCollection(new HashSet<TestSimpleComposite>(), c1, c2);
+   }
+
    public void testArrayUnwrap() throws Exception
    {
       short[] shorts = new short[128];
@@ -210,6 +237,13 @@
       assertEquals(object, unwrapped);
    }
 
+   protected <T> void checkCollection(Collection<T> collection, T... params) throws Exception
+   {
+      collection.addAll(Arrays.asList(params));
+      checkSingle(collection, true);
+      checkSingle(collection, false);
+   }
+
    protected void checkArray(Object object, boolean typeInfoFromObject, Asserter asserter)
    {
       MetaValue metaValue = createMetaValue(object);




More information about the jboss-cvs-commits mailing list