8

I have a POJO, and a (currently not-yet-built) class that will return Lists of it. I'd like to automatically generate the code necessary for the POJO to be accessed as a Map. Is this a good idea, is it possible to do automatically, and do I need to do this manually for every POJO I want to treat this way?

Thanks, Andy

Andrew Toulouse
  • 1,271
  • 1
  • 10
  • 10

2 Answers2

16

You can use Commons BeanUtils BeanMap for this.

Map map = new BeanMap(someBean);

Update: since that's not an option due to some apparent library dependency problems in Android, here's a basic kickoff example how you could do it with little help of Reflection API:

public static Map<String, Object> mapProperties(Object bean) throws Exception {
    Map<String, Object> properties = new HashMap<>();
    for (Method method : bean.getClass().getDeclaredMethods()) {
        if (Modifier.isPublic(method.getModifiers())
            && method.getParameterTypes().length == 0
            && method.getReturnType() != void.class
            && method.getName().matches("^(get|is).+")
        ) {
            String name = method.getName().replaceAll("^(get|is)", "");
            name = Character.toLowerCase(name.charAt(0)) + (name.length() > 1 ? name.substring(1) : "");
            Object value = method.invoke(bean);
            properties.put(name, value);
        }
    }
    return properties;
}

If java.beans API were available, then you could just do:

public static Map<String, Object> mapProperties(Object bean) throws Exception {
    Map<String, Object> properties = new HashMap<>();
    for (PropertyDescriptor property : Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors()) {
        String name = property.getName();
        Object value = property.getReadMethod().invoke(bean);
        properties.put(name, value);
    }
    return properties;
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Holy crap! That looks super useful! Do you know anything about its performance implications? – Andrew Toulouse Jul 09 '10 at 18:48
  • 1
    Reflection has always a performance cost. Nothing to do against. But you can trust that the BeanUtils guys have optimized it as much as possible. It's a pretty popular library. – BalusC Jul 09 '10 at 18:54
  • Cool. Do I have to implement any sort of interface on my POJOs, or is using get* naming enough (I'm using immutable POJOs w/ private constructors instantiated by using a Builder)? – Andrew Toulouse Jul 09 '10 at 18:56
  • It just follows the [JavaBean™ specification](http://java.sun.com/javase/technologies/desktop/javabeans/docs/spec.html), yes. Not sure about private c'tors though, but a public no-arg constructor does logically not seem to be necessary to generate a map based on an existing instance. – BalusC Jul 09 '10 at 18:59
  • I agree. \*crosses fingers\*. Thanks for your help! – Andrew Toulouse Jul 09 '10 at 19:10
  • Hm. Android doesn't seem to like Commons BeanUtils. Importing BeanUtils causes a runtime error resolving org.apache.commons.collections.Transformer. Adding in Commons collections further causes issues because it can't be packaged into an apk for whatever reason. :( – Andrew Toulouse Jul 09 '10 at 22:07
  • Well, then write an utility method yourself with a little help of reflection. I've added a kickoff example. – BalusC Jul 09 '10 at 22:31
  • Note: method.getName().matches("^(get|is).+") and method.getName().replaceAll("^(get|is)", ""); make the above code a bit slow ... removing regex and using startsWith makes a lot of difference, ofcourse for several hundred calls. – jack_carver Mar 29 '12 at 20:07
  • @jack: True, it was just a basic kickoff example with minimal boilerplate. Note that it also doesn't handle propertynames starting with 2 or more capitals properly. Thanks for the advice. – BalusC Mar 29 '12 at 20:09
1

Here's my own implementation without any dependencies. Rather than make a copy of the object's state, it implements a live Map over the pojo. Android doesn't support java.beans, but you can use openbeans instead.

import java.beans.*;  // Or, import com.googlecode.openbeans.*
import java.util.*;

public class BeanMap extends AbstractMap<String, Object> {
    private static final Object[] NO_ARGS = new Object[] {};
    private HashMap<String, PropertyDescriptor> properties;
    private Object bean;

    public BeanMap(Object bean) throws IntrospectionException {
        this.bean = bean;
        properties = new HashMap<String, PropertyDescriptor>();
        BeanInfo info = Introspector.getBeanInfo(bean.getClass());
        for(PropertyDescriptor property : info.getPropertyDescriptors()) {
            properties.put(property.getName(), property);
        }
    }

    @Override public Object get(Object key) {
        PropertyDescriptor property = properties.get(key);
        if(property == null)
            return null;
        try {
            return property.getReadMethod().invoke(bean, NO_ARGS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override public Object put(String key, Object value) {
        PropertyDescriptor property = properties.get(key);
        try {
            return property.getWriteMethod().invoke(bean, new Object[] {value});
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override public Set<Map.Entry<String, Object>> entrySet() {
        HashSet<Map.Entry<String, Object>> result = new HashSet<Map.Entry<String, Object>>(properties.size() * 2);
        for(PropertyDescriptor property : properties.values()) {
            String key = property.getName();
            Object value;
            try {
                value = property.getReadMethod().invoke(bean, NO_ARGS);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            result.add(new PropertyEntry(key, value));
        }
        return Collections.unmodifiableSet(result);
    }

    @Override public int size() { return properties.size(); }

    @Override public boolean containsKey(Object key) { 
        return properties.containsKey(key);
    }

    class PropertyEntry extends AbstractMap.SimpleEntry<String, Object> {
        PropertyEntry(String key, Object value) {
            super(key, value);
        }

        @Override public Object setValue(Object value) {
            super.setValue(value);
            return BeanMap.this.put(getKey(), value);
        }
    }
}
mikeslattery
  • 4,039
  • 1
  • 19
  • 14