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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Oct 19 07:48:37 EDT 2007


Author: alesj
Date: 2007-10-19 07:48:37 -0400 (Fri, 19 Oct 2007)
New Revision: 66278

Added:
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestRecursiveSimpleComposite.java
Modified:
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/WritethroughManagedPropertyImpl.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/CompositeMetaType.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/support/TestSimpleComposite.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java
Log:
Composite unwrap - currently needs default constructor.
Todo on instance creation - some 'Instantiator' mapping?

Modified: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/WritethroughManagedPropertyImpl.java
===================================================================
--- projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/WritethroughManagedPropertyImpl.java	2007-10-19 10:22:08 UTC (rev 66277)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/WritethroughManagedPropertyImpl.java	2007-10-19 11:48:37 UTC (rev 66278)
@@ -113,7 +113,7 @@
             else
             {
                MetaType metaType = metaValue.getMetaType();
-               setValue = (metaType.isTable() == false && metaType.isComposite() == false);
+               setValue = (metaType.isTable() == false);
             }
             if (setValue)
             {

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/CompositeMetaType.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/CompositeMetaType.java	2007-10-19 10:22:08 UTC (rev 66277)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/CompositeMetaType.java	2007-10-19 11:48:37 UTC (rev 66278)
@@ -63,7 +63,7 @@
    Set<String> itemSet();
 
    /**
-    * Retrieve an unmodifiable Set view of all the item names in ascending order.
+    * Retrieve an unmodifiable Set view of all the key names in ascending order.
     *
     * @return the Set
     */

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-19 10:22:08 UTC (rev 66277)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java	2007-10-19 11:48:37 UTC (rev 66278)
@@ -36,6 +36,7 @@
 import java.util.WeakHashMap;
 
 import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.beans.info.spi.PropertyInfo;
 import org.jboss.config.plugins.property.PropertyConfiguration;
 import org.jboss.config.spi.Configuration;
 import org.jboss.metatype.api.types.ArrayMetaType;
@@ -460,7 +461,7 @@
          return null;
 
       MetaType metaType = metaValue.getMetaType();
-      if (metaType.isTable() || metaType.isComposite())
+      if (metaType.isTable())
          throw new IllegalArgumentException("Cannot get value from " + metaValue + ", unsupported.");
 
       if (metaType.isSimple())
@@ -496,6 +497,11 @@
          }
          return array;
       }
+      else if (metaType.isComposite())
+      {
+         CompositeValue compositeValue = (CompositeValue)metaValue;
+         return unwrapComposite(compositeValue, type);
+      }
 
       throw new IllegalArgumentException("Unsupported meta value: " + metaValue);
    }
@@ -543,6 +549,55 @@
    }
 
    /**
+    * Unwrap composite.
+    *
+    * @param compositeValue the composite value
+    * @param typeInfo expected type info
+    * @return unwrapped value
+    */
+   protected Object unwrapComposite(CompositeValue compositeValue, TypeInfo typeInfo)
+   {
+      CompositeMetaType compositeMetaType = compositeValue.getMetaType();
+      String typeName = compositeMetaType.getTypeName();
+      ClassLoader cl;
+      if (typeInfo != null)
+         cl = typeInfo.getType().getClassLoader();
+      else
+         cl = Thread.currentThread().getContextClassLoader();
+      
+      try
+      {
+         BeanInfo beanInfo = configuration.getBeanInfo(typeName, cl);
+         Object bean = createNewInstance(beanInfo);
+         for (String name : compositeMetaType.keySet())
+         {
+            MetaValue itemValue = compositeValue.get(name);
+            PropertyInfo propertyInfo = beanInfo.getProperty(name);
+            Object value = unwrap(itemValue, propertyInfo.getType());
+            propertyInfo.set(bean, value);
+         }
+         return bean;
+      }
+      catch (Throwable t)
+      {
+         throw new UndeclaredThrowableException(t);
+      }
+   }
+
+   /**
+    * Create new instance.
+    *
+    * @param beanInfo the bean info
+    * @return new instance
+    * @throws Throwable for any error
+    */
+   protected Object createNewInstance(BeanInfo beanInfo) throws Throwable
+   {
+      // todo - some 'instantiator' map?
+      return beanInfo.newInstance();
+   }
+
+   /**
     * Get new array instance.
     *
     * @param typeInfo the type info

Added: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestRecursiveSimpleComposite.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestRecursiveSimpleComposite.java	                        (rev 0)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestRecursiveSimpleComposite.java	2007-10-19 11:48:37 UTC (rev 66278)
@@ -0,0 +1,109 @@
+/*
+* 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.test.metatype.values.factory.support;
+
+/**
+ * TestSimpleComposite.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class TestRecursiveSimpleComposite
+{
+   private String something;
+   private TestSimpleComposite composite;
+
+   /**
+    * Default constructor.
+    * Needed with unwrap.
+    */
+   public TestRecursiveSimpleComposite()
+   {
+   }
+
+   /**
+    * Create a new TestSimpleComposite.
+    *
+    * @param something the something
+    * @param composite the composite
+    */
+   public TestRecursiveSimpleComposite(String something, TestSimpleComposite composite)
+   {
+      this.something = something;
+      this.composite = composite;
+   }
+
+   /**
+    * Get the something.
+    *
+    * @return the something.
+    */
+   public String getSomething()
+   {
+      return something;
+   }
+
+   /**
+    * Set the something.
+    *
+    * @param something the something.
+    */
+   public void setSomething(String something)
+   {
+      this.something = something;
+   }
+
+   /**
+    * Get the composite.
+    *
+    * @return the composite
+    */
+   public TestSimpleComposite getComposite()
+   {
+      return composite;
+   }
+
+   /**
+    * Set the composite.
+    *
+    * @param composite the composite
+    */
+   public void setComposite(TestSimpleComposite composite)
+   {
+      this.composite = composite;
+   }
+
+   public boolean equals(Object obj)
+   {
+      if (obj == this)
+         return true;
+      if (obj == null || obj instanceof TestRecursiveSimpleComposite == false)
+         return false;
+
+      TestRecursiveSimpleComposite other = (TestRecursiveSimpleComposite) obj;
+      return something.equals(other.something) && composite.equals(other.composite);
+   }
+
+   public int hashCode()
+   {
+      return something.hashCode() + 11 * composite.hashCode();
+   }
+}

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestSimpleComposite.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestSimpleComposite.java	2007-10-19 10:22:08 UTC (rev 66277)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestSimpleComposite.java	2007-10-19 11:48:37 UTC (rev 66278)
@@ -32,6 +32,14 @@
    private String something;
 
    /**
+    * Default constructor.
+    * Needed with unwrap.
+    */
+   public TestSimpleComposite()
+   {
+   }
+
+   /**
     * Create a new TestSimpleComposite.
     * 
     * @param something the something

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-19 10:22:08 UTC (rev 66277)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java	2007-10-19 11:48:37 UTC (rev 66278)
@@ -28,6 +28,8 @@
 import org.jboss.metatype.api.values.MetaValue;
 import org.jboss.test.metatype.values.factory.support.TestEnum;
 import org.jboss.test.metatype.values.factory.support.TestGeneric;
+import org.jboss.test.metatype.values.factory.support.TestSimpleComposite;
+import org.jboss.test.metatype.values.factory.support.TestRecursiveSimpleComposite;
 
 /**
  * UnwrapValueUnitTestCase.
@@ -76,12 +78,24 @@
       checkSingle(new TestGeneric("123"), false);
    }
 
+   public void testCompositeUnwrap() throws Exception
+   {
+      TestSimpleComposite composite = new TestSimpleComposite("something");
+      checkSingle(composite, true);
+      checkSingle(composite, false);
+
+      checkSingle(new TestRecursiveSimpleComposite("something", composite), true);         
+      checkSingle(new TestRecursiveSimpleComposite("something", composite), false);
+   }
+
    public void testArrayUnwrap() throws Exception
    {
       short[] shorts = new short[128];
       Double[] doubles = new Double[128];
       TestEnum[] enums = new TestEnum[128];
       TestGeneric[] generics = new TestGeneric[128];
+      TestSimpleComposite[] composits = new TestSimpleComposite[128];
+      TestRecursiveSimpleComposite[] recursiveComposites = new TestRecursiveSimpleComposite[128];
       Integer[][] integers = new Integer[128][128];
       Integer[][][] triple = new Integer[10][10][10];
       for(int i = 0; i < 128; i++)
@@ -90,6 +104,8 @@
          doubles[i] = i / Math.PI;
          enums[i] = TestEnum.values()[i % 3];
          generics[i] = new TestGeneric("#" + i);
+         composits[i] = new TestSimpleComposite("#" + i);
+         recursiveComposites[i] = new TestRecursiveSimpleComposite("#" + i, composits[i]);
          for(int j = 0; j < 128; j++)
          {
             integers[i][j] = 128 * i + j;
@@ -128,36 +144,26 @@
          }
       });
 
-      checkArray(enums, true, new Asserter()
+      Asserter objectsAsserter = 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, 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(enums, true, objectsAsserter);
+      checkArray(enums, false, objectsAsserter);
 
+      checkArray(generics, true, objectsAsserter);
+      checkArray(generics, false, objectsAsserter);
+
+      checkArray(composits, true, objectsAsserter);
+      checkArray(composits, false, objectsAsserter);
+
+      checkArray(recursiveComposites, true, objectsAsserter);
+      checkArray(recursiveComposites, false, objectsAsserter);
+
       Asserter integersAsserter = new Asserter()
       {
          public boolean assertArray(final Object original, final Object unwrapped)




More information about the jboss-cvs-commits mailing list