1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| public class BeanUtils { public static Field findField(Class<?> clazz, String name) { try { return clazz.getField(name); } catch (NoSuchFieldException ex) { return findDeclaredField(clazz, name); } }
public static Field findDeclaredField(Class<?> clazz, String name) { try { return clazz.getDeclaredField(name); } catch (NoSuchFieldException ex) { if (clazz.getSuperclass() != null) { return findDeclaredField(clazz.getSuperclass(), name); } return null; } }
public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { try { return clazz.getMethod(methodName, paramTypes); } catch (NoSuchMethodException ex) { return findDeclaredMethod(clazz, methodName, paramTypes); } }
public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { try { return clazz.getDeclaredMethod(methodName, paramTypes); } catch (NoSuchMethodException ex) { if (clazz.getSuperclass() != null) { return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes); } return null; } }
public static Object getProperty(Object obj, String name) throws NoSuchFieldException { Object value; Field field = findField(obj.getClass(), name); if (field == null) { throw new NoSuchFieldException("no such field [" + name + "]"); } boolean accessible = field.isAccessible(); field.setAccessible(true); try { value = field.get(obj); } catch (Exception e) { throw new RuntimeException(e); } field.setAccessible(accessible); return value; }
public static void setProperty(Object obj, String name, Object value) throws NoSuchFieldException { Field field = findField(obj.getClass(), name); if (field == null) { throw new NoSuchFieldException("no such field [" + name + "]"); } boolean accessible = field.isAccessible(); field.setAccessible(true); try { field.set(obj, value); } catch (Exception e) { throw new RuntimeException(e); } field.setAccessible(accessible); }
public static Map<String, Object> obj2Map(Object obj, Map<String, Object> map) { if (map == null) { map = new HashMap<>(); } if (obj != null) { try { Class<?> clazz = obj.getClass(); do { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isStatic(mod)) { continue; } boolean accessible = field.isAccessible(); field.setAccessible(true); map.put(field.getName(), field.get(obj)); field.setAccessible(accessible); } clazz = clazz.getSuperclass(); } while (clazz != null); } catch (Exception e) { throw new RuntimeException(e); } } return map; }
public static List<Class<?>> getSuperclassList(Class<?> clazz) { List<Class<?>> clazzes = new ArrayList<>(3); clazzes.add(clazz); clazz = clazz.getSuperclass(); while (clazz != null) { clazzes.add(clazz); clazz = clazz.getSuperclass(); } return Collections.unmodifiableList(clazzes); } }
|