新增部分工具
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package com.hxuanyu.monitor.config;
|
||||
|
||||
import com.hxuanyu.monitor.utils.BeanUtils;
|
||||
import com.hxuanyu.common.util.BeanUtils;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.SchedulingException;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
|
@@ -1,129 +0,0 @@
|
||||
package com.hxuanyu.monitor.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 反射工具,操作Bean对象的属性
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
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<>(0);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得父类集合,包含当前class
|
||||
*
|
||||
* @param clazz 类型泛型
|
||||
* @return 父类集合
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user