Java examples for Reflection:Java Bean
get all the properties from a Bean Class;
/**/*from www . j a va 2s.c o m*/ *all rights reserved,@copyright 2003 */ import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; public class Main{ public static void main(String[] argv) throws Exception{ Class clz = String.class; System.out.println(java.util.Arrays.toString(getProperties(clz))); } public static final String PROPERTY_SET_METHOD_PREFIX = "set"; public static final String PROPERTY_GET_METHOD_PREFIX = "get"; public static final String PROPERTY_IS_METHOD_PREFIX = "is"; /** * get all the properties from a Bean Class; * * @param clz * @return */ public static BeanProperty[] getProperties(Class clz) { HashMap p = new HashMap(); Method[] methods = clz.getMethods(); int count = methods.length; for (int i = 0; i < count; i++) { String methodName = methods[i].getName(); if (methodName.startsWith(PROPERTY_SET_METHOD_PREFIX)) { Class[] paramTypes = methods[i].getParameterTypes(); if (paramTypes != null && paramTypes.length == 1 && PrimitiveType.isPrimitive(paramTypes[0])) { String propName = Character.toLowerCase(methodName .charAt(PROPERTY_SET_METHOD_PREFIX.length())) + methodName .substring(PROPERTY_SET_METHOD_PREFIX .length() + 1); BeanProperty bp = (BeanProperty) p.get(propName); if (bp == null) { bp = new BeanProperty(propName, PrimitiveType.getTypeName(paramTypes[0])); bp.setMethodName = methodName; p.put(propName, bp); } else { if (bp.getType().equalsIgnoreCase( PrimitiveType.getTypeName(paramTypes[0]))) { bp.setMethodName = methodName; } } } } else if (methodName.startsWith(PROPERTY_GET_METHOD_PREFIX)) { Class[] paramTypes = methods[i].getParameterTypes(); Class returnType = methods[i].getReturnType(); if ((paramTypes == null || paramTypes.length == 0) && PrimitiveType.isPrimitive(returnType)) { String propName = Character.toLowerCase(methodName .charAt(PROPERTY_SET_METHOD_PREFIX.length())) + methodName .substring(PROPERTY_SET_METHOD_PREFIX .length() + 1); BeanProperty bp = (BeanProperty) p.get(propName); if (bp == null) { bp = new BeanProperty(propName, PrimitiveType.getTypeName(returnType)); bp.getMethodName = methodName; p.put(propName, bp); } else { if (bp.getType().equalsIgnoreCase( PrimitiveType.getTypeName(returnType))) { bp.getMethodName = methodName; } } } } else if (methodName.startsWith(PROPERTY_IS_METHOD_PREFIX)) { Class[] paramTypes = methods[i].getParameterTypes(); Class returnType = methods[i].getReturnType(); if ((paramTypes == null || paramTypes.length == 0) && PrimitiveType.isPrimitive(returnType)) { String propName = Character.toLowerCase(methodName .charAt(PROPERTY_SET_METHOD_PREFIX.length())) + methodName .substring(PROPERTY_SET_METHOD_PREFIX .length() + 1); BeanProperty bp = (BeanProperty) p.get(propName); if (bp == null) { bp = new BeanProperty(propName, PrimitiveType.getTypeName(returnType)); bp.getMethodName = methodName; p.put(propName, bp); } else { if (bp.getType().equalsIgnoreCase( PrimitiveType.getTypeName(returnType))) { bp.getMethodName = methodName; } } } } } return (BeanProperty[]) p.values().toArray( new BeanProperty[p.size()]); } }