
ars.util.Beans Maven / Gradle / Ivy
package ars.util;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.net.URLDecoder;
import java.net.JarURLConnection;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Date;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Comparator;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.text.DecimalFormat;
import java.lang.reflect.Type;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.InvocationTargetException;
/**
* 对象工具类
*
* @author wuyongqiang
*/
public final class Beans {
/**
* 空对象数组
*/
public static final Object[] EMPTY_ARRAY = new Object[0];
/**
* 默认数字格式化对象
*/
public static final DecimalFormat DEFAULT_DECIMAL_FORMAT = new DecimalFormat("0.##");
/**
* 对象单例映射表
*/
private static final Map, Object> singles = new HashMap, Object>(0);
private Beans() {
}
/**
* 获取对象类型
*
* @param objects 对象数组
* @return 类型数组
*/
public static Class>[] getTypes(Object... objects) {
if (objects == null) {
throw new IllegalArgumentException("Objects must not be null");
}
Class>[] types = new Class>[objects.length];
for (int i = 0; i < objects.length; i++) {
Object object = objects[i];
types[i] = object == null ? null : object.getClass();
}
return types;
}
/**
* 判断类型是否是基本数据类型
*
* @param cls 数据类型
* @return true/false
*/
public static boolean isBasicClass(Class> cls) {
if (cls == null) {
return false;
} else if (Number.class.isAssignableFrom(cls)) {
return true;
}
try {
return ((Class>) cls.getField("TYPE").get(null)).isPrimitive();
} catch (NoSuchFieldException e) {
return cls.isPrimitive();
} catch (IllegalAccessException e) {
return cls.isPrimitive();
}
}
/**
* 判断对象类型是否是元类型
*
* 元类型包括基本数据类型、字符串类型、枚举类型、日期类型、类对象类型
*
* @param cls 对象类型
* @return true/false
*/
public static boolean isMetaClass(Class> cls) {
return cls != null && (isBasicClass(cls) || cls == String.class || cls == Class.class || cls == Object.class
|| Enum.class.isAssignableFrom(cls) || Date.class.isAssignableFrom(cls));
}
/**
* 判断类型是否是基本数据包装类型
*
* @param cls 数据类型
* @return true/false
*/
public static boolean isBasicWrapClass(Class> cls) {
return cls != null && (cls == Byte.class || cls == Character.class || cls == Integer.class || cls == Short.class
|| cls == Long.class || cls == Float.class || cls == Double.class || cls == Boolean.class);
}
/**
* 判断类型是否是基本数据数字类型
*
* @param cls 数据类型
* @return true/false
*/
public static boolean isBasicNumberClass(Class> cls) {
return cls != null && (cls == byte.class || cls == char.class || cls == short.class || cls == int.class
|| cls == double.class || cls == long.class);
}
/**
* 判断类型是否是基本数据数字包装类型
*
* @param cls 数据类型
* @return true/false
*/
public static boolean isBasicNumberWrapClass(Class> cls) {
return cls != null && (cls == Byte.class || cls == Character.class || cls == Short.class || cls == Integer.class
|| cls == Double.class || cls == Long.class);
}
/**
* 判断数据类型是否是数字类型
*
* @param cls 数据类型
* @return true/false
*/
public static boolean isNumberClass(Class> cls) {
return isBasicNumberClass(cls) || isBasicNumberWrapClass(cls);
}
/**
* 获取基本数据包装类型
*
* @param cls 基本数据类型
* @return 基本数据包装类型
*/
public static Class> getBasicWrapClass(Class> cls) {
if (cls == byte.class) {
return Byte.class;
} else if (cls == char.class) {
return Character.class;
} else if (cls == int.class) {
return Integer.class;
} else if (cls == short.class) {
return Short.class;
} else if (cls == long.class) {
return Long.class;
} else if (cls == float.class) {
return Float.class;
} else if (cls == double.class) {
return Double.class;
} else if (cls == boolean.class) {
return Boolean.class;
}
return cls;
}
/**
* 获取树型键/值对象比较器
*
* @param 键类型
* @param 值类型
* @param map 树型键/值对象
* @return 比较器对象
*/
@SuppressWarnings("unchecked")
public static Comparator getTreeMapComparator(TreeMap map) {
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
Field field = null;
try {
field = map.getClass().getDeclaredField("comparator");
field.setAccessible(true);
return (Comparator) field.get(map);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} finally {
if (field != null) {
field.setAccessible(false);
}
}
}
/**
* 获取类对象的泛型
*
* @param cls 类对象对象
* @return 泛型类型数组
*/
public static Class>[] getGenericTypes(Class> cls) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null");
}
Type genericSuperclass = cls.getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
Type[] types = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
List> classes = new ArrayList>(types.length);
for (Type type : types) {
if (type instanceof Class) {
classes.add((Class>) type);
}
}
return classes.toArray(new Class>[0]);
}
Class> parent = cls.getSuperclass();
return parent == null ? new Class>[0] : getGenericTypes(parent);
}
/**
* 获取字段的泛型
*
* @param field 字段对象
* @return 泛型类型数组
*/
public static Class>[] getGenericTypes(Field field) {
if (field == null) {
throw new IllegalArgumentException("Field must not be null");
}
Type genericSuperclass = field.getGenericType();
if (genericSuperclass instanceof ParameterizedType) {
Type[] types = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
List> classes = new ArrayList>(types.length);
for (Type type : types) {
if (type instanceof Class) {
classes.add((Class>) type);
}
}
return classes.toArray(new Class>[0]);
}
return new Class>[0];
}
/**
* 判断对象是否为空
*
* @param object 对象
* @return true/false
*/
public static boolean isEmpty(Object object) {
if (object == null || (object instanceof CharSequence && ((CharSequence) object).length() == 0)
|| (object instanceof Map && ((Map, ?>) object).isEmpty())
|| (object instanceof Collection && ((Collection>) object).isEmpty())
|| (object instanceof Iterable && !((Iterable>) object).iterator().hasNext())) {
return true;
}
Class> type = object.getClass();
if (type.isArray()) {
Class> component = type.getComponentType();
if (component == byte.class) {
return ((byte[]) object).length == 0;
} else if (component == char.class) {
return ((char[]) object).length == 0;
} else if (component == int.class) {
return ((int[]) object).length == 0;
} else if (component == short.class) {
return ((short[]) object).length == 0;
} else if (component == long.class) {
return ((long[]) object).length == 0;
} else if (component == float.class) {
return ((float[]) object).length == 0;
} else if (component == double.class) {
return ((double[]) object).length == 0;
} else if (component == boolean.class) {
return ((boolean[]) object).length == 0;
}
return ((Object[]) object).length == 0;
}
return false;
}
/**
* 判断数组中是否存在制定对象
*
* @param 数据类型
* @param array 数组
* @param object 对象
* @return true/false
*/
public static boolean isExist(T[] array, T object) {
if (array == null || array.length == 0 || object == null) {
return false;
}
for (T t : array) {
if (isEqual(t, object)) {
return true;
}
}
return false;
}
/**
* 判断数组中是否存在制定对象
*
* @param 数据类型
* @param array 数组
* @param object 对象
* @param comparator 比较器
* @return true/false
*/
public static boolean isExist(T[] array, T object, Comparator comparator) {
if (comparator == null) {
throw new IllegalArgumentException("Comparator must not be null");
}
if (array == null || array.length == 0 || object == null) {
return false;
}
for (T t : array) {
if (comparator.compare(t, object) == 0) {
return true;
}
}
return false;
}
/**
* 判断数组中是否存在制定对象
*
* @param 数据类型
* @param array 数组
* @param objects 对象数组
* @return true/false
*/
public static boolean isExist(T[] array, T[] objects) {
if (array == null || array.length == 0 || objects == null || objects.length == 0) {
return false;
}
outer:
for (T o : objects) {
for (T t : array) {
if (isEqual(t, o)) {
continue outer;
}
}
return false;
}
return true;
}
/**
* 判断数组中是否存在制定对象
*
* @param 数据类型
* @param array 数组
* @param objects 对象数组
* @param comparator 比较器
* @return true/false
*/
public static boolean isExist(T[] array, T[] objects, Comparator comparator) {
if (comparator == null) {
throw new IllegalArgumentException("Comparator must not be null");
}
if (array == null || array.length == 0 || objects == null || objects.length == 0) {
return false;
}
outer:
for (T o : objects) {
for (T t : array) {
if (comparator.compare(t, o) == 0) {
continue outer;
}
}
return false;
}
return true;
}
/**
* 判断集合中是否存在制定对象
*
* @param 数据类型
* @param collection 集合
* @param object 对象
* @return true/false
*/
public static boolean isExist(Collection collection, T object) {
if (collection == null || collection.isEmpty() || object == null) {
return false;
}
for (T t : collection) {
if (isEqual(t, object)) {
return true;
}
}
return false;
}
/**
* 判断集合中是否存在制定对象
*
* @param 数据类型
* @param collection 集合
* @param object 对象
* @param comparator 对象比较器
* @return true/false
*/
public static boolean isExist(Collection collection, T object, Comparator comparator) {
if (comparator == null) {
throw new IllegalArgumentException("Comparator must not be null");
}
if (collection == null || collection.isEmpty() || object == null) {
return false;
}
for (T t : collection) {
if (comparator.compare(t, object) == 0) {
return true;
}
}
return false;
}
/**
* 判断集合中是否存在制定对象
*
* @param 数据类型
* @param collection 集合
* @param objects 对象数组
* @return true/false
*/
public static boolean isExist(Collection collection, T[] objects) {
if (collection == null || collection.isEmpty() || objects == null || objects.length == 0) {
return false;
}
outer:
for (T o : objects) {
for (T t : collection) {
if (isEqual(t, o)) {
continue outer;
}
}
return false;
}
return true;
}
/**
* 判断集合中是否存在制定对象
*
* @param 数据类型
* @param collection 集合
* @param objects 对象集合
* @return true/false
*/
public static boolean isExist(Collection collection, Collection objects) {
if (collection == null || collection.isEmpty() || objects == null || objects.isEmpty()) {
return false;
}
outer:
for (T o : objects) {
for (T t : collection) {
if (isEqual(t, o)) {
continue outer;
}
}
return false;
}
return true;
}
/**
* 判断集合中是否存在制定对象
*
* @param 数据类型
* @param collection 集合
* @param objects 对象数组
* @param comparator 对象比较器
* @return true/false
*/
public static boolean isExist(Collection collection, T[] objects, Comparator comparator) {
if (collection != null && collection.size() > 0 && objects != null && objects.length > 0) {
for (T object : objects) {
if (!isExist(collection, object, comparator)) {
return false;
}
}
return true;
}
return false;
}
/**
* 判断集合中是否存在制定对象
*
* @param 数据类型
* @param collection 集合
* @param objects 对象集合
* @param comparator 对象比较器
* @return true/false
*/
public static boolean isExist(Collection collection, Collection objects, Comparator comparator) {
if (collection != null && collection.size() > 0 && objects != null && !objects.isEmpty()) {
for (T object : objects) {
if (!isExist(collection, object, comparator)) {
return false;
}
}
return true;
}
return false;
}
/**
* 判断两个对象是否相同
*
* @param object 对象
* @param other 对象
* @return true/false
*/
public static boolean isEqual(Object object, Object other) {
return object == other || (object != null && object.equals(other)) || (other != null && other.equals(object));
}
/**
* 判断两个数组中元素是否相同
*
* @param 数据类型
* @param array1 数组1
* @param array2 数组2
* @return true/false
*/
public static boolean isEqual(T[] array1, T[] array2) {
return isEqual(array1, array2, new Comparator() {
@Override
public int compare(T o1, T o2) {
return o1.equals(o2) ? 0 : -1;
}
});
}
/**
* 判断两个数组中元素是否相同
*
* @param 数据类型
* @param array1 数组1
* @param array2 数组2
* @param comparator 比较器
* @return true/false
*/
public static boolean isEqual(T[] array1, T[] array2, Comparator comparator) {
return array1 == array2 || (array1 != null && array2 != null && array1.length == 0 && array2.length == 0)
|| isEqual(Arrays.asList(array1), Arrays.asList(array2), comparator);
}
/**
* 判断两个字典元素是否相同
*
* @param 键类型
* @param 值类型
* @param map1 字典1
* @param map2 字典2
* @return true/false
*/
public static boolean isEqual(Map map1, Map map2) {
if (map1 == map2 || (map1 != null && map2 != null && map1.isEmpty() && map2.isEmpty())) {
return true;
} else if (map1.size() != map2.size()) {
return false;
}
for (Entry entry : map1.entrySet()) {
if (!isEqual(entry.getValue(), map2.get(entry.getKey()))) {
return false;
}
}
for (Entry entry : map2.entrySet()) {
if (!isEqual(entry.getValue(), map1.get(entry.getKey()))) {
return false;
}
}
return true;
}
/**
* 判断两个集合中元素是否相同
*
* @param 数据类型
* @param collection1 集合1
* @param collection2 集合2
* @return true/false
*/
public static boolean isEqual(Collection collection1, Collection collection2) {
if (collection1 == collection2
|| (collection1 != null && collection2 != null && collection1.isEmpty() && collection2.isEmpty())) {
return true;
} else if (collection1.size() != collection2.size()) {
return false;
}
boolean equals = true;
List list2 = collection2 instanceof List ? (List) collection2 : new ArrayList(collection2);
Map exists = new HashMap();
for (T o1 : collection1) {
boolean found = false;
for (int i = 0; i < list2.size(); i++) {
if (list2.get(i).equals(o1)) {
found = true;
exists.put(i, null);
break;
}
}
if (!found) {
equals = false;
break;
}
}
if (equals) {
for (int i = 0; i < list2.size(); i++) {
if (!exists.containsKey(i) && !collection1.contains(list2.get(i))) {
return false;
}
}
}
return equals;
}
/**
* 判断两个集合中元素是否相同
*
* @param 数据类型
* @param collection1 集合1
* @param collection2 集合2
* @param comparator 比较器
* @return true/false
*/
public static boolean isEqual(Collection collection1, Collection collection2, Comparator comparator) {
if (collection1 == collection2
|| (collection1 != null && collection2 != null && collection1.isEmpty() && collection2.isEmpty())) {
return true;
} else if (collection1.size() != collection2.size()) {
return false;
}
boolean equals = true;
List list2 = collection2 instanceof List ? (List) collection2 : new ArrayList(collection2);
Map exists = new HashMap();
for (T o1 : collection1) {
boolean found = false;
for (int i = 0; i < list2.size(); i++) {
if (comparator.compare(o1, list2.get(i)) == 0) {
found = true;
exists.put(i, null);
break;
}
}
if (!found) {
equals = false;
break;
}
}
if (equals) {
for (int i = 0; i < list2.size(); i++) {
if (exists.containsKey(i)) {
continue;
}
boolean found = false;
for (T o1 : collection1) {
if (comparator.compare(o1, list2.get(i)) == 0) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
}
return equals;
}
/**
* 判断两个对象属性值是否相同
*
* @param 数据类型
* @param object 对象
* @param other 对象
* @param fields 属性字段数组
* @return true/false
*/
public static boolean isEqual(T object, T other, Field... fields) {
if (object == other) {
return true;
} else if (object == null || other == null) {
return false;
}
for (Field field : fields) {
boolean accessible = field.isAccessible();
if (!accessible) {
field.setAccessible(true);
}
try {
if (!isEqual(field.get(object), field.get(other))) {
return false;
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} finally {
if (!accessible) {
field.setAccessible(false);
}
}
}
return true;
}
/**
* 判断两个对象属性值是否相同
*
* @param 数据类型
* @param object 对象
* @param other 对象
* @param properties 属性名称数组
* @return true/false
*/
public static boolean isEqual(T object, T other, String... properties) {
if (object == other) {
return true;
} else if (object == null || other == null) {
return false;
}
for (String property : properties) {
if (!isEqual(getValue(object, property), getValue(other, property))) {
return false;
}
}
return true;
}
/**
* 判断对象是否为数组
*
* @param object 对象
* @return true/false
*/
public static boolean isArray(Object object) {
return object != null && object.getClass().isArray();
}
/**
* 判断对象是否为数字
*
* @param object 对象
* @return true/false
*/
public static boolean isNumber(Object object) {
return object != null && isNumberClass(object.getClass());
}
/**
* 获取对象实例
*
* @param 数据类型
* @param type 对象类型
* @return 对象实例
*/
public static T getInstance(Class type) {
return getInstance(type, false);
}
/**
* 获取对象实例
*
* @param 数据类型
* @param type 对象类型
* @param single 是否单例
* @return 对象实例
*/
@SuppressWarnings("unchecked")
public static T getInstance(Class type, boolean single) {
if (type == null) {
throw new IllegalArgumentException("Type must not be null");
}
try {
if (single) {
T instance = (T) singles.get(type);
if (instance == null) {
synchronized (type) {
if ((instance = (T) singles.get(type)) == null) {
instance = type.newInstance();
singles.put(type, instance);
}
}
}
return instance;
}
return type.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* 获取对象实例
*
* @param 数据类型
* @param type 对象类型
* @param arguments 实例化参数
* @return 对象实例
*/
public static T getInstance(Class type, Object... arguments) {
return getInstance(type, false, arguments);
}
/**
* 获取对象实例
*
* @param 数据类型
* @param type 对象类型
* @param single 是否单例
* @param arguments 实例化参数
* @return 对象实例
*/
@SuppressWarnings("unchecked")
public static T getInstance(Class type, boolean single, Object... arguments) {
if (type == null) {
throw new IllegalArgumentException("Type must not be null");
}
if (arguments == null) {
throw new IllegalArgumentException("Arguments must not be null");
}
try {
if (single) {
T instance = (T) singles.get(type);
if (instance == null) {
synchronized (type) {
if ((instance = (T) singles.get(type)) == null) {
instance = type.getConstructor(getTypes(arguments)).newInstance(arguments);
singles.put(type, instance);
}
}
}
return instance;
}
return type.getConstructor(getTypes(arguments)).newInstance(arguments);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
/**
* 判断对象是否可以实例化
*
* @param type 对象类型
* @return true/false
*/
public static boolean isInstantiable(Class> type) {
if (type == null) {
throw new IllegalArgumentException("Type must not be null");
}
int mod = type.getModifiers();
if (!(Modifier.isAbstract(mod) || Modifier.isInterface(mod) || Enum.class.isAssignableFrom(type))) {
Constructor>[] constructors = type.getConstructors();
if (constructors.length == 0) {
return true;
}
for (Constructor> constructor : constructors) {
if (constructor.getParameterTypes().length == 0) {
return true;
}
}
}
return false;
}
/**
* 根据字段名称获取字段对象
*
* @param cls 类对象
* @param name 字段名称
* @return 字段对象
*/
public static Field getField(Class> cls, String name) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null");
}
if (name == null) {
throw new IllegalArgumentException("Name must not be null");
}
try {
return cls.getDeclaredField(name);
} catch (NoSuchFieldException e) {
Class> parent = cls.getSuperclass();
if (parent == null) {
throw new RuntimeException(e);
}
return getField(parent, name);
}
}
/**
* 根据字段名称获取字段对象
*
* @param cls 类对象
* @param names 字段名称数组
* @return 字段对象数组
*/
public static Field[] getFields(Class> cls, String... names) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null");
}
if (names == null || names.length == 0) {
List fields = new LinkedList();
while (cls != Object.class) {
for (Field field : cls.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers()) && !field.getName().startsWith("this$")) {
fields.add(field);
}
}
cls = cls.getSuperclass();
}
return fields.toArray(new Field[0]);
}
Field[] fields = new Field[names.length];
Field[] _fields = cls.getDeclaredFields();
outer:
for (int i = 0; i < names.length; i++) {
String name = names[i];
for (Field field : _fields) {
if (field.getName().equals(name)) {
fields[i] = field;
continue outer;
}
}
Class> parent = cls.getSuperclass();
while (parent != null) {
try {
fields[i] = parent.getDeclaredField(name);
continue outer;
} catch (NoSuchFieldException e) {
parent = parent.getSuperclass();
}
}
throw new RuntimeException("No such field:" + name);
}
return fields;
}
/**
* 获取对象属性名称
*
* @param cls 对象类型
* @return 字段名称数组
*/
public static String[] getProperties(Class> cls) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null");
}
if (Enum.class.isAssignableFrom(cls)) {
Method method = null;
try {
method = cls.getMethod("values");
method.setAccessible(true);
Object[] values = (Object[]) method.invoke(cls);
String[] properties = new String[values.length];
for (int i = 0; i < values.length; i++) {
properties[i] = values[i].toString();
}
return properties;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} finally {
if (method != null) {
method.setAccessible(false);
}
}
}
List properties = new LinkedList();
while (cls != Object.class) {
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers())) {
properties.add(field.getName());
}
}
cls = cls.getSuperclass();
}
return properties.toArray(Strings.EMPTY_ARRAY);
}
/**
* 获取对象方法
*
* @param cls 类对象
* @param name 方法名称
* @param parameterTypes 方法参数类型数组
* @return 方法对象
*/
public static Method getMethod(Class> cls, String name, Class>... parameterTypes) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null");
}
if (name == null) {
throw new IllegalArgumentException("Name must not be null");
}
return getMethod(cls, cls, name, parameterTypes);
}
/**
* 获取对象方法
*
* @param original 原始类对象
* @param cls 类对象
* @param name 方法名称
* @param parameterTypes 方法参数类型数组
* @return 方法对象
*/
private static Method getMethod(Class> original, Class> cls, String name, Class>... parameterTypes) {
methods:
for (Method method : cls.getMethods()) {
if (method.getName().equals(name)) {
Class>[] _parameterTypes = method.getParameterTypes();
if (parameterTypes.length == _parameterTypes.length) {
for (int i = 0; i < parameterTypes.length; i++) {
if (!_parameterTypes[i].isAssignableFrom(parameterTypes[i])) {
continue methods;
}
}
return method;
}
}
}
Class> parent = cls.getSuperclass();
if (parent == null) {
StringBuilder buffer = new StringBuilder().append('(');
for (int i = 0; i < parameterTypes.length; i++) {
if (i > 0) {
buffer.append(", ");
}
buffer.append(parameterTypes[i].getName());
}
buffer.append(')');
throw new RuntimeException("No such method:" + original.getName() + '.' + name + buffer.toString());
}
return getMethod(original, parent, name, parameterTypes);
}
/**
* 获取字段get方法
*
* @param cls 类对象
* @param field 字段对象
* @return 方法对象
*/
public static Method getGetMethod(Class> cls, Field field) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null");
}
if (field == null) {
throw new IllegalArgumentException("Field must not be null");
}
String property = field.getName();
StringBuilder buffer = new StringBuilder().append(Character.toUpperCase(property.charAt(0)));
if (property.length() > 1) {
buffer.append(property.substring(1));
}
try {
return cls.getMethod(buffer.insert(0, "get").toString());
} catch (NoSuchMethodException e) {
if (field.getType() == boolean.class || field.getType() == Boolean.class) {
try {
return cls.getMethod(buffer.insert(0, "is").toString());
} catch (NoSuchMethodException e1) {
throw new RuntimeException(e);
}
}
throw new RuntimeException(e);
}
}
/**
* 获取字段set方法
*
* @param cls 类对象
* @param field 字段对象
* @return 方法对象
*/
public static Method getSetMethod(Class> cls, Field field) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null");
}
if (field == null) {
throw new IllegalArgumentException("Field must not be null");
}
String property = field.getName();
StringBuilder buffer = new StringBuilder("set").append(Character.toUpperCase(property.charAt(0)));
if (property.length() > 1) {
buffer.append(property.substring(1));
}
try {
return cls.getMethod(buffer.toString(), field.getType());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
/**
* 获取对象指定字段的值,对象属性必须支持get方法,如果属性类型为boolean,则可以是is方法
*
* @param object 对象实例
* @param property 属性名称
* @return 字段值
*/
public static Object getValue(Object object, String property) {
if (property == null) {
throw new IllegalArgumentException("Property must not be null");
}
if (object == null) {
return null;
}
String suffix = null;
int index = property.indexOf('.');
if (index > 0) {
suffix = property.substring(index + 1);
property = property.substring(0, index);
}
Class> meta = object instanceof Class ? (Class>) object : object.getClass();
Object value = getValue(object, getField(meta, property));
if (value == null || suffix == null) {
return value;
}
return getValue(value, suffix);
}
/**
* 获取对象指定字段的值,对象属性必须支持get方法,如果属性类型为boolean,则可以是is方法
*
* @param object 对象实例
* @param field 字段对象
* @return 字段值
*/
public static Object getValue(Object object, Field field) {
if (field == null) {
throw new IllegalArgumentException("Field must not be null");
}
if (object == null) {
return null;
}
Class> meta = object instanceof Class ? (Class>) object : object.getClass();
Method method = getGetMethod(meta, field);
method.setAccessible(true);
try {
return Modifier.isPublic(method.getModifiers()) ? method.invoke(object) : null;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} finally {
method.setAccessible(false);
}
}
/**
* 获取对象实例指定属性,对象属性必须支持get方法,如果属性类型为boolean,则可以是is方法
*
* @param object 对象实例
* @param properties 属性名称数组(如果为空则获取所有属性值)
* @return 键/值对象
*/
public static Map getValues(Object object, String... properties) {
if (object == null) {
return new HashMap(0);
}
if (properties.length == 0) {
Map values = new HashMap();
Class> meta = object instanceof Class ? (Class>) object : object.getClass();
while (meta != Object.class) {
Field[] fields = meta.getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers())) {
values.put(field.getName(), getValue(object, field));
}
}
meta = meta.getSuperclass();
}
return values;
}
Map values = new HashMap(properties.length);
for (String property : properties) {
values.put(property, getValue(object, property));
}
return values;
}
/**
* 获取对象方法调用结果(级联属性之间使用“.”号隔开)
*
* @param object 对象实例
* @param method 属性方法名称
* @return 属性方法值
*/
public static Object getMethodValue(Object object, String method) {
if (method == null) {
throw new IllegalArgumentException("Method must not be null");
}
if (object == null) {
return null;
}
Class> meta = object instanceof Class ? (Class>) object : object.getClass();
int sign = method.lastIndexOf('.');
if (sign <= 0) {
try {
return meta.getMethod(method).invoke(object);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
return getMethodValue(getValue(object, method.substring(0, sign)), method.substring(sign + 1));
}
/**
* 获取集合中对象方法值
*
* 如果对象实例为Collection或数组,则返回所有集合中所有对象指定方法名称的返回值; 否则返回实例指定属性值
*
* @param object 对象实例
* @param method 方法名称
* @return 值
*/
public static Object getAssembleMethodValue(Object object, String method) {
if (method == null) {
throw new IllegalArgumentException("Method must not be null");
}
if (isEmpty(object)) {
return object;
}
if (object instanceof Collection) {
Collection> collection = (Collection>) object;
List