Loading
copyPrimitiveProperty BeanMap example

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Copy Primitive Bean Properties with BeanMap


Ans)

Copy Primitive Bean Properties , PropertyUtils, BeanMap

The following class copies only Privimitive Data types or the Wrapper
classes , and ignores Custom Value Objects.

import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;

public class PrimitivePropertyCopier {

 private static final HashSet<Class<?>> WRAPPER_TYPES = getWrapperTypes();
 private static Logger log = Logger.getLogger(RequestTranslatorUtil.class);

 public static void copyNonMemberClassProperties (Object target, Object source) {
  BeanMap beanMapPojo = new BeanMap( target );
  BeanMap beanMap = new BeanMap( source );
     Set keys = beanMapPojo.keySet( );
     Iterator keyIterator = keys.iterator( );
     while( keyIterator.hasNext( ) ) {
   try {
      String propertyName = (String) keyIterator.next( );
          Class classType = beanMap.getType(propertyName);
        if (classType != null && (isWrapperType(classType)||
          classType.isPrimitive())) {
          if ( PropertyUtils.isWriteable(target, propertyName)) {
      try {
       Object value = beanMap.get( propertyName );
       if (value != null) {
          PropertyUtils.setProperty(target, propertyName, value);
       }
      } catch (Exception e) {
       // No need to worry about this , we can ignore.
       log.error("Could be Ignored " + e.getMessage());
      }
          } 
        }
         } catch (Exception e) {
    log.error("Could be Ignored " + e.getMessage());
    }
     }
   
     }

 /**
  * This method checks the Type Of Variable if that is Copyable or not.
  *
  * @param clazz
  * @return
  */
  public static boolean isWrapperType(Class<?> clazz)
     {
         return WRAPPER_TYPES.contains(clazz);
     }

     private static HashSet<Class<?>> getWrapperTypes()
     {
         HashSet<Class<?>> ret = new HashSet<Class<?>>();
         ret.add(String.class);
         ret.add(Boolean.class);
         ret.add(Character.class);
         ret.add(Byte.class);
         ret.add(Short.class);
         ret.add(Integer.class);
         ret.add(Long.class);
         ret.add(Float.class);
         ret.add(Double.class);
         ret.add(Void.class);
         ret.add(Calendar.class);
         return ret;
     }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

copyPrimitiveProperty BeanMap example


copyPrimitiveProperty BeanMap example

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free