/** * 获得一个对象各个属性的字节流 */ @SuppressWarnings("rawtypes") public static void getProperty(Object entityName) throws Exception { Class c = entityName.getClass(); Field field[] = c.getDeclaredFields(); for (Field f : field) { Object v = invokeMethod(entityName, f.getName(), null); if (v != null) { System.out.println(f.getName() + "\t" + v + "\t" + v.toString().length()); } } } /** * 获得对象属性的值 */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception { Class ownerClass = owner.getClass(); methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1); Method method = null; try { method = ownerClass.getMethod("get" + methodName); } catch (SecurityException e) { } catch (NoSuchMethodException e) { return " can't find 'get" + methodName + "' method"; } return method.invoke(owner); }
java反射打印属性值_java反射打印类中的属性和值
最新推荐文章于 2023-04-14 11:00:36 发布