Get Mapped Property : Bean Utils « Apache Common « Java

Get Mapped Property

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.lang.reflect.Method;

import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.HashBag;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import org.apache.commons.lang.StringUtils;

public class TasteOfThingsV1 {

  private static Map testMap;
  private static TestBean testBean;

  public static void main(String args[]) throws Exception {

    HashBag myBag = new HashBag(testMap.values());

    System.err.println("How many Boxes? " + myBag.getCount("Boxes"));
    myBag.add("Boxes", 5);
    System.err.println("How many Boxes now? " + myBag.getCount("Boxes"));

    Method method =
      testBean.getClass().getDeclaredMethod("getTestMap", new Class[0]);
    HashMap reflectionMap =
      (HashMap)method.invoke(testBean, new Object[0]);
    System.err.println("The value of the 'squ' key using reflection: " +

    String squ = BeanUtils.getMappedProperty(testBean, "testMap", "squ");
    squ = StringUtils.capitalize(squ);

    PropertyUtils.setMappedProperty(testBean, "testMap", "squ", squ);

    System.err.println("The value of the 'squ' key is: " +
      BeanUtils.getMappedProperty(testBean, "testMap", "squ"));

    String box = (String)testMap.get("box");
    String caps =
      Character.toTitleCase(box.charAt(0)) +
      box.substring(1, box.length());
    System.err.println("Capitalizing boxes by Java: " + caps);

  private static void prepareData() {
    testMap = new HashMap();
    testMap.put("box", "boxes");
    testMap.put("squ", "squares");
    testMap.put("rect", "rectangles");
    testMap.put("cir", "circles");

    testBean = new TestBean();


import java.util.Map;
import java.util.HashMap;

public class TestBean {
  private Map testMap;

  public Map getTestMap() {
    return this.testMap;
  public void setTestMap(Map testMap) {
    this.testMap = testMap;
       877 k)

Related examples in the same category

1.BeanUtil Array Utils Example
2.BeanUtils Collections 2
3.BeanUtils Date Utils
4.BeanUtils get Property from Dynamic Beans
5.Use Bean Utils to get property value from object
6.Bean Utils: deal with collections