/** * 获取类属性名 * * @param pclass * @return String[] * @throws FrameException */ public static String[] getClassDeclaredFieldNames(Class<?> pclass) throws FrameException { Field[] propertyField; String[] returnArray; int count = 0; if (pclass == null) { return null; } propertyField = pclass.getDeclaredFields(); if (propertyField != null && propertyField.length > 0) { count = propertyField.length; } returnArray = new String[count]; for (int i = 0; i < count; i++) { returnArray[i] = propertyField[i].getName(); } return returnArray; } /** * 获取JavaBean属性名 * * @param pclass * @return String[] * @throws FrameException */ public static ObjectFieldDescriptor[] getObjectFieldDescriptors(Object obj) throws FrameException { Field[] fields; Field field; int count = 0; ObjectFieldDescriptor[] objectFieldDescriptors; String name; Object value; if (obj == null) { return null; } fields = obj.getClass().getDeclaredFields(); if (fields != null && fields.length > 0) { count = fields.length; } objectFieldDescriptors = new ObjectFieldDescriptor[count]; for (int i = 0; i < count; i++) { field = fields[i]; name = field.getName(); value = getFieldValueIgnoreAccessible(obj, field); objectFieldDescriptors[i] = new ObjectFieldDescriptor(name, field.getType(), value); } return objectFieldDescriptors; }
/** * 获取对象的域值,忽略访问权限 * * @throws FrameException */ public static Object getFieldValueIgnoreAccessible(Object obj, Field field) throws FrameException { boolean accessible; Object value; if (obj == null) { throw new FrameException("传入参数[obj]为空!"); } if (field == null) { throw new FrameException("传入参数[field]为空!"); } try { accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } value = field.get(obj); field.setAccessible(accessible); return value; } catch (IllegalArgumentException e) { throw new FrameException(e); } catch (IllegalAccessException e) { throw new FrameException(e); } }