View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer.simple;
8   
9   import org.mule.api.transformer.DiscoverableTransformer;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.transformer.AbstractTransformer;
12  import org.mule.transformer.types.DataTypeFactory;
13  import org.mule.util.BeanUtils;
14  
15  import java.util.Map;
16  
17  /**
18   * Conversts a simple bean object to a Map. every property on the bean will become an entry in the
19   * result {@link java.util.Map}. Note that only exposed bean properties with getter and setter methods will be
20   * added to the map.
21   */
22  public class BeanToMap extends AbstractTransformer implements DiscoverableTransformer
23  {
24  
25      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
26  
27      public BeanToMap()
28      {
29          registerSourceType(DataTypeFactory.OBJECT);
30          setReturnDataType(DataTypeFactory.create(Map.class));
31      }
32  
33      @Override
34      protected Object doTransform(Object src, String encoding) throws TransformerException
35      {
36          Map result = BeanUtils.describeBean(src);
37          return result;
38      }
39  
40      public int getPriorityWeighting()
41      {
42          return priorityWeighting;
43      }
44  
45      public void setPriorityWeighting(int weighting)
46      {
47          priorityWeighting = weighting;
48      }
49  
50      
51  }