Author: tchemit Date: 2011-01-24 12:39:23 +0100 (Mon, 24 Jan 2011) New Revision: 2036 Url: http://nuiton.org/repositories/revision/nuiton-utils/2036 Log: Evolution #1228: Add more usefull methods in BeanUtil Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 10:55:04 UTC (rev 2035) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 11:39:23 UTC (rev 2036) @@ -25,9 +25,13 @@ package org.nuiton.util.beans; import org.apache.commons.beanutils.MethodUtils; +import org.apache.commons.beanutils.PropertyUtils; import java.beans.PropertyChangeListener; +import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; +import java.util.HashSet; +import java.util.Set; /** * Usefull methods around the {@link PropertyChangeListener}. @@ -91,4 +95,118 @@ new Class[]{PropertyChangeListener.class} ); } + + /** + * Obtains all readable properties from a given type. + * + * @param beanType the type to seek + * @return the set of all readable properties for the given type + * @since 2.0 + */ + public static Set<String> getReadableProperties(Class<?> beanType) { + Set<Class<?>> exploredTypes = new HashSet<Class<?>>(); + Set<String> result = new HashSet<String>(); + + // get properties for the class + getReadableProperties(beanType, result, exploredTypes); + + return result; + } + + /** + * Obtains all writeable properties from a given type. + * + * @param beanType the type to seek + * @return the set of all writeable properties for the given type + * @since 2.0 + */ + public static Set<String> getWriteableProperties(Class<?> beanType) { + Set<Class<?>> exploredTypes = new HashSet<Class<?>>(); + Set<String> result = new HashSet<String>(); + + // get properties for the class + getWriteableProperties(beanType, result, exploredTypes); + + return result; + } + + protected static void getReadableProperties(Class<?> beanType, + Set<String> result, + Set<Class<?>> exploredTypes) { + + if (exploredTypes.contains(beanType)) { + + // already explored + return; + } + exploredTypes.add(beanType); + + // get properties for the class + getReadableProperties(beanType, result); + + if (beanType.getSuperclass() != null) { + + // get properties fro super-class + getReadableProperties(beanType.getSuperclass(), result, exploredTypes); + } + Class<?>[] interfaces = beanType.getInterfaces(); + for (Class<?> anInterface : interfaces) { + + // get properties fro super-class + getReadableProperties(anInterface, result, exploredTypes); + } + } + + protected static void getReadableProperties(Class<?> beanType, + Set<String> result) { + + PropertyDescriptor[] descriptors = + PropertyUtils.getPropertyDescriptors(beanType); + for (PropertyDescriptor descriptor : descriptors) { + String name = descriptor.getName(); + if (descriptor.getReadMethod() != null) { + result.add(name); + } + } + } + + protected static void getWriteableProperties(Class<?> beanType, + Set<String> result, + Set<Class<?>> exploredTypes) { + + if (exploredTypes.contains(beanType)) { + + // already explored + return; + } + exploredTypes.add(beanType); + + // get properties for the class + getWriteableProperties(beanType, result); + + if (beanType.getSuperclass() != null) { + + // get properties fro super-class + getWriteableProperties(beanType.getSuperclass(), result, exploredTypes); + } + Class<?>[] interfaces = beanType.getInterfaces(); + for (Class<?> anInterface : interfaces) { + + // get properties fro super-class + getWriteableProperties(anInterface, result, exploredTypes); + } + } + + protected static void getWriteableProperties(Class<?> beanType, + Set<String> result) { + + PropertyDescriptor[] descriptors = + PropertyUtils.getPropertyDescriptors(beanType); + for (PropertyDescriptor descriptor : descriptors) { + String name = descriptor.getName(); + if (descriptor.getReadMethod() != null) { + result.add(name); + } + } + } }