Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * 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.xb.builder.runtime;
 
 import java.util.Map;
 
 
MapPropertyHandler.

Author(s):
Alexey Loubyansky
Version:
$Revision: 1.1 $
 
 {
    private final MapFactory mapFactory;
    private final MapPutAdapter mapPutAdapter;
   
Create a new MapPropertyHandler.

Parameters:
propertyInfo the property
propertyType the property type
Throws:
java.lang.IllegalArgumentException for a null qName or property
 
    public MapPropertyHandler(Configuration configPropertyInfo propertyInfoTypeInfo propertyTypeboolean wrapped)
    {
       super(propertyInfopropertyType);
 
       if(wrapped)
       {
           = null;
       }
       else
       {
          ClassInfo classInfo = (ClassInfopropertyType;
          if (Modifier.isAbstract(classInfo.getModifiers()))
          {
              = .;
          }
          else
          {
             ConstructorInfo constructor = classInfo.getDeclaredConstructor(null);
             if (constructor == null)
             {
                for (ConstructorInfo ctor : classInfo.getDeclaredConstructors())
                {
                   if (ctor.getParameterTypes().length == 0)
                   {
                      .warn("ClassInfo.getDeclaredConstructor(null) didn't work for " + classInfo.getName()
                            + ", found the default ctor in ClassInfo.getDeclaredConstructors()");
                      constructor = ctor;
                      break;
                   }
                }
 
                if (constructor == null)
                {
                   throw new RuntimeException("Default constructor not found for " + classInfo.getName());
                }
             }
              = new CtorMapFactory(constructor);
          }
       }
       
       JBossXmlMapEntry entry = propertyInfo.getUnderlyingAnnotation(JBossXmlMapEntry.class);
       if(entry == null)
         entry =((ClassInfo)propertyType).getUnderlyingAnnotation(JBossXmlMapEntry.class);
      
      if(entry != null && !JBossXmlMapEntry.DEFAULT.class.equals(entry.type()))
      {
         BeanInfo entryBean = config.getBeanInfo(entry.type());
          = new CustomMapEntryPutAdapter(entryBean);
      }
      else
   }
   @SuppressWarnings("unchecked")
   public void handle(PropertyInfo propertyInfoTypeInfo propertyTypeObject parentObject childQName qName)
   {
      if()
         .trace("handle entry " + qName + ", property=" + propertyInfo.getName() + ", parent=" + parent + ", child=" + child);
      
      BeanAdapter beanAdapter = (BeanAdapterparent;
      
      Map<ObjectObjectm = null;
      if( == null)
      {
         // it's wrapped, so the parent expected to be a map
         m = (Map<ObjectObject>) beanAdapter.getValue();
      }
      else
      {
         try
         {
            if (propertyInfo.getGetter() != null)
               m = (Map<ObjectObject>) beanAdapter.get(propertyInfo);
         }
         catch (Throwable t)
         {
            throw new RuntimeException("QName " + qName + " error getting map property " + propertyInfo.getName()
                  + " for " + BuilderUtil.toDebugString(parent), t);
         }
         // No map so create one
         if (m == null)
         {
            try
            {
               m = .createMap();
            }
            catch (Throwable t)
            {
               throw new RuntimeException("QName " + qName + " error creating map: " + propertyType.getName(), t);
            }
            try
            {
               beanAdapter.set(propertyInfom);
            }
            catch (Throwable t)
            {
               throw new RuntimeException("QName " + qName + " error setting map property " + propertyInfo.getName()
                     + " for " + BuilderUtil.toDebugString(parent) + " with value " + BuilderUtil.toDebugString(m), t);
            }
         }
      }
      
      try
      {
         .put(mchild);
      }
      catch (Throwable e)
      {
         throw new RuntimeException("QName " + qName + " error adding " + BuilderUtil.toDebugString(child) + " to map " + BuilderUtil.toDebugString(m), e);
      }
   }
   
   private static interface MapPutAdapter
   {
      void put(Map<Object,ObjectmapObject entrythrows Throwable;
   }
   
   private static class CustomMapEntryPutAdapter implements MapPutAdapter
   {
      private final PropertyInfo keyProp;
      private final PropertyInfo valueProp;
      
      CustomMapEntryPutAdapter(BeanInfo entryBean)
      {
         PropertyInfo keyProp = null;
         PropertyInfo valueProp = null;
         for(PropertyInfo prop : entryBean.getProperties())
         {
            JBossXmlMapKey key = prop.getUnderlyingAnnotation(JBossXmlMapKey.class);
            if(key != null)
            {
               if(keyProp != null)
                  throw new IllegalStateException(
                        "Found two properties in entry type " + entryBean.getName() +
                        " annotated with @JBossXmlMapKey: " +
                        keyProp.getName() + " and " + prop.getName());
               keyProp = prop;
            }
            JBossXmlMapValue value = prop.getUnderlyingAnnotation(JBossXmlMapValue.class);
            if(value != null)
            {
               if(valueProp != null)
                  throw new IllegalStateException(
                        "Found two properties in entry type " + entryBean.getName() +
                        " annotated with @JBossXmlMapValue: " +
                        valueProp.getName() + " and " + prop.getName());
               valueProp = prop;
            }
         }
         if(keyProp == null)
            throw new IllegalStateException(
                  "Entry type " + entryBean.getName() +
                  " doesn't have any property annotated with @JBossXmlMapKey.");
         
         this. = keyProp;
         this. = valueProp;
      }
      
      public void put(Map<ObjectObjectmapObject entrythrows Throwable
      {
         Object key = .get(entry);
         Object value = entry;
         if( != null)
            value = .get(entry);
         map.put(keyvalue);
      }
   }
   
   private static class DefaultMapEntryPutAdapter implements MapPutAdapter
   {
      static final MapPutAdapter INSTANCE = new DefaultMapEntryPutAdapter();
      
      public void put(Map<ObjectObjectmapObject entry)
      {
         if(!(entry instanceof DefaultMapEntry))
            throw new IllegalStateException("Expected DefaultMapEntry but got " + entry);
         DefaultMapEntry defEntry = (DefaultMapEntryentry;
         map.put(defEntry.getKey(), defEntry.getValue());
      }      
   }
   
   private static interface MapFactory
   {
      Map<ObjectObjectcreateMap() throws Throwable;
   }
   
   private static class HashMapFactory implements MapFactory
   {
      static final MapFactory INSTANCE = new HashMapFactory();
      
      @SuppressWarnings("unchecked")
      public Map<ObjectObjectcreateMap()
      {
         return new HashMap<ObjectObject>();
      }  
   }
   
   private static class CtorMapFactory implements MapFactory
   {
      private final ConstructorInfo ctor;
      
      {
         this. = ctor;
      }
      
      @SuppressWarnings("unchecked")
      public Map<ObjectObjectcreateMap() throws Throwable
      {
         return (Map.newInstance(null);
      }      
   }
New to GrepCode? Check out our FAQ X