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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| public class ReflectUtils { public static Object setStringEmptyValue(Object o, Class<?> c) { Field[] fields = FPAReflectUtils.getAllFields(c); try { for (Field field : fields) { field.setAccessible(true); PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c); Method getMethod = pd.getReadMethod(); Method setMethod = pd.getWriteMethod(); Type type = getMethod.getGenericReturnType(); Object value = getMethod.invoke(o); if (value == null && type.getTypeName().equals("java.lang.String")) { setMethod.invoke(o, StringUtils.EMPTY); } } } catch (SecurityException | IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); return null; } return o; }
public static Field[] getAllFields(Class<?> clazz) { List<Field> fieldList = new ArrayList<>(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields()))); clazz = clazz.getSuperclass(); fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields()))); Field[] fields = new Field[fieldList.size()]; return fieldList.toArray(fields); }
public static Object getValueByName(Object object, Class<?> clazz, String fieldName) throws NoSuchFieldException, IntrospectionException, InvocationTargetException, IllegalAccessException { Field[] fields = clazz.getDeclaredFields(); if ("ID".equalsIgnoreCase(fieldName)) { PropertyDescriptor pd = new PropertyDescriptor("id", clazz); Method getMethod = pd.getReadMethod(); return getMethod.invoke(object); } else { for (Field field : fields) { if (field.getName().equals(fieldName)) { field.setAccessible(true); PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); Method getMethod = pd.getReadMethod(); return getMethod.invoke(object); } } } throw new NoSuchFieldException(fieldName); }
public static Map<String, String> getAnnotationFields(Object o) { Map<String, String> map = new HashMap<>(); Class<?> clazz = o.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { boolean annotationPresent = field.isAnnotationPresent(TableField.class); if (annotationPresent) { String name = field.getAnnotation(TableField.class).value(); map.put(field.getName(), name); } } return map; }
public static String getFieldNameByTableName(Class<?> clazz, String tableName) throws NoSuchFieldException { if ("ID".equalsIgnoreCase(tableName)) return "ID"; Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { boolean annotationPresent = field.isAnnotationPresent(TableField.class); if (annotationPresent) { String fieldName = field.getAnnotation(TableField.class).value().replace("`", ""); if (fieldName.equals(tableName)) { return field.getName(); } } } throw new NoSuchFieldException(); }
public static String getTableNameByFieldName(Class<?> clazz, String fieldName) throws NoSuchFieldException { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { boolean annotationPresent = field.isAnnotationPresent(TableField.class); if (annotationPresent && field.getName().equals(fieldName)) { return field.getAnnotation(TableField.class).value().replace("`", ""); } } throw new NoSuchFieldException(); }
public static <T> T mapToObj(Map map, Class<T> target) throws Exception { Field[] fields = target.getDeclaredFields(); T o = target.newInstance(); for (Field field : fields) { Object val; if ((val = map.get(field.getName())) != null) { field.setAccessible(true); field.set(o, val); } } return o; }
private static <T> T newInstance(Object source, Class<T> target, List<Field> sourceFields, List<Field> targetFields) throws Exception { T t = target.newInstance(); if (targetFields.isEmpty()) { return t; } for (Field field : sourceFields) { field.setAccessible(true); Object o = field.get(source); Field sameField = getSameField(field, targetFields); if (Objects.nonNull(sameField)) { sameField.setAccessible(true); sameField.set(t, o); } } return t; }
private static Field getSameField(Field field, List<Field> fields) { String name = field.getName(); String type = field.getType().getName(); for (Field f : fields) { if (name.equals(f.getName()) && type.equals(f.getType().getName())) { return f; } } return null; }
private static List<Field> getFields(Class<?> c) { List<Field> fieldList = new ArrayList<>(); Field[] fields = c.getDeclaredFields(); if (fields.length > 0) { fieldList.addAll(Arrays.asList(fields)); } return getSuperClassFields(c, fieldList); }
private static List<Field> getSuperClassFields(Class<?> o, List<Field> allFields) { Class<?> superclass = o.getSuperclass(); if (Objects.isNull(superclass) || Object.class.getName().equals(superclass.getName())) { return allFields; } Field[] fields = superclass.getDeclaredFields(); if (fields.length == 0) { return allFields; } allFields.addAll(Arrays.asList(fields)); return getSuperClassFields(superclass, allFields); } }
|
This is copyright.