Java examples for Reflection:Java Bean
get Nested Property of Java Bean
//package com.java2s; import java.beans.BeanInfo; import java.beans.IndexedPropertyDescriptor; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { private static final String INDEXED_DELIM = "["; private static final String INDEXED_DELIM2 = "]"; private static final String MAPPED_DELIM = "("; private static final String MAPPED_DELIM2 = ")"; private static final String NESTED_DELIM = "."; public static Object getNestedProperty(Object bean, String name) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { String[] properties = name.split("\\" + NESTED_DELIM); for (int i = 0; i < properties.length; i++) { if (properties[i].contains(INDEXED_DELIM)) { bean = getIndexedProperty(bean, properties[i]); continue; }//from w w w .j av a 2 s . c o m if (properties[i].contains(MAPPED_DELIM)) { bean = getMappedProperty(bean, properties[i]); continue; } bean = getSimpleProperty(bean, properties[i]); } return bean; } public static Object getIndexedProperty(Object bean, String name) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { int delim = name.indexOf(INDEXED_DELIM); int delim2 = name.indexOf(INDEXED_DELIM2); if ((delim < 0) || (delim2 <= delim)) throw new IllegalArgumentException("Invalid indexed property '" + name + "'"); int index = -1; try { String subscript = name.substring(delim + 1, delim2); index = Integer.parseInt(subscript); } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid indexed property '" + name + "'"); } name = name.substring(0, delim); return getIndexedProperty(bean, name, index); } public static Object getIndexedProperty(Object bean, String name, int index) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean, name); if (propertyDescriptor == null) throw new IllegalArgumentException("No property:" + name); if (propertyDescriptor instanceof IndexedPropertyDescriptor) { Method readMethod = ((IndexedPropertyDescriptor) propertyDescriptor) .getIndexedReadMethod(); if (readMethod == null) throw new IllegalArgumentException( "No readMethod for property:" + name); Object value = readMethod.invoke(bean, new Object[] { new Integer(index) }); return value; } Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod == null) throw new IllegalArgumentException( "No readMethod for property:" + name); Object array = readMethod.invoke(bean, new Object[0]); if (array.getClass().isArray()) { return Array.get(array, index); } if (!(array instanceof java.util.List)) throw new IllegalArgumentException("Property '" + name + "' is not indexed"); return ((java.util.List<?>) array).get(index); } public static Object getMappedProperty(Object bean, String name) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { int delim = name.indexOf(MAPPED_DELIM); int delim2 = name.indexOf(MAPPED_DELIM2); if ((delim < 0) || (delim2 <= delim)) throw new IllegalArgumentException("Invalid mapped property '" + name + "'"); String subscript = name.substring(delim + 1, delim2); name = name.substring(0, delim); return getMappedProperty(bean, name, subscript); } public static Object getMappedProperty(Object bean, String name, String key) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean, name); if (propertyDescriptor == null) throw new IllegalArgumentException("No property:" + name); Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod == null) throw new IllegalArgumentException( "No readMethod for property:" + name); Object map = readMethod.invoke(bean, new Object[0]); if (!(map instanceof java.util.Map)) throw new IllegalArgumentException("Property '" + name + "' is not mapped"); return ((java.util.Map<?, ?>) map).get(key); } public static Object getSimpleProperty(Object bean, String name) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Method readMethod = getSimplePropertyReadMethod(bean, name); if (readMethod == null) throw new IllegalArgumentException( "No readMethod for property:" + name); readMethod.setAccessible(true); Object value = readMethod.invoke(bean, new Object[0]); return value; } public static PropertyDescriptor getPropertyDescriptor(Object bean, String name) { PropertyDescriptor[] descriptors = getPropertyDescriptors(bean); for (int i = 0; i < descriptors.length; i++) { if (name.equals(descriptors[i].getName())) return descriptors[i]; } return null; } public static Method getSimplePropertyReadMethod(Object bean, String name) { PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean, name); if (propertyDescriptor == null) throw new IllegalArgumentException("No property:" + name); return propertyDescriptor.getReadMethod(); } public static PropertyDescriptor[] getPropertyDescriptors(Object bean) { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(bean.getClass()); } catch (IntrospectionException e) { return (new PropertyDescriptor[0]); } PropertyDescriptor[] descriptors = beanInfo .getPropertyDescriptors(); return descriptors; } }