All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.ipokerface.weixin.utils.ReflectionUtils Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package cn.ipokerface.weixin.utils;

import java.lang.reflect.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by       PokerFace
 * Create Date      2019-12-28.
 * Email:           [email protected]
 * Version          1.0.0
 * 

* Description: */ public class ReflectionUtils { /** * 获取包包名 * * @param obj * @return */ public static String getPackageName(Object obj) { return obj.getClass().getPackage().getName(); } /** * 获取字段的泛型参数类型 * * @param obj * @param fieldName * @return */ public static Class getFieldGenericType(Object obj, String fieldName) { Field field = getAccessibleField(obj, fieldName); Type type = field.getGenericType(); if (type instanceof ParameterizedType) { return (Class) ((ParameterizedType) type) .getActualTypeArguments()[0]; } return null; } /** * 调用方法 * * @param object * 对象 * * @param propertyName * 属性名称 */ public static Object invokeMethod(Object object, String propertyName) { try { Method getterMethod = object.getClass().getMethod(propertyName); return getterMethod.invoke(object); } catch (Exception e) { e.printStackTrace(); return null; } } public static Object invokeMethod(Object object, String propertyName, Object... args) { try { Method getterMethod = object.getClass().getMethod(propertyName); return getterMethod.invoke(object, args); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 调用Getter方法 * * @param object * 对象 * * @param propertyName * 属性名称 * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static Object invokeGetterMethod(Object object, String propertyName) throws Exception { String getterMethodName = null; Method getterMethod = null; String propertyNa = null; if (propertyName.contains(".")) { propertyNa = StringUtil.substringBefore(propertyName, "."); getterMethodName = "get" + StringUtil.capitalize(propertyNa); getterMethod = object.getClass().getMethod(getterMethodName); return invokeGetterMethod(getterMethod.invoke(object), StringUtil.substringAfter(propertyName, ".")); } else { getterMethodName = "get" + StringUtil.capitalize(propertyName); getterMethod = object.getClass().getMethod(getterMethodName); return getterMethod.invoke(object); } } /** * 调用Setter方法 * * @param object * 对象 * * @param propertyName * 属性名称 * * @param propertyValue * 属性值 */ public static void invokeSetterMethod(Object object, String propertyName, Object propertyValue) { Class setterMethodClass = propertyValue.getClass(); invokeSetterMethod(object, propertyName, propertyValue, setterMethodClass); } /** * 调用Setter方法 * * @param object * 对象 * * @param propertyName * 属性名称 * * @param propertyValue * 属性值 * * @param setterMethodClass * 参数类型 */ public static void invokeSetterMethod(Object object, String propertyName, Object propertyValue, Class setterMethodClass) { String setterMethodName = "set" + StringUtil.capitalize(propertyName); try { Method setterMethod = object.getClass().getMethod(setterMethodName, setterMethodClass); setterMethod.invoke(object, propertyValue); } catch (Exception e) { e.printStackTrace(); } } /** * 获取对象属性值,无视private/protected/getter * * @param object * 对象 * * @param fieldName * 属性名称 */ public static Object getFieldValue(Object object, String fieldName) { Field field = getAccessibleField(object, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field " + fieldName); } Object result = null; try { result = field.get(object); } catch (IllegalAccessException e) { } return result; } /** * 设置对象属性值,无视private/protected/setter * * @param object * 对象 * * @param fieldName * 属性名称 */ public static void setFieldValue(Object object, String fieldName, Object value) { Field field = getAccessibleField(object, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field " + fieldName); } try { field.set(object, value); } catch (IllegalAccessException e) { } } // 获取字段的类型 public static String getFieldType(Object object, String fieldName) { Field field = getAccessibleField(object, fieldName); return field.getType().getSimpleName(); } public static Field getAccessibleField(final Object object, final String fieldName) { for (Class superClass = object.getClass(); superClass != Object.class;) { try { Field field = superClass.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch (NoSuchFieldException e) { return null; } } return null; } public static Set getAllField(Class clazz) { Set fieldSet = new HashSet(); while (clazz != Object.class) { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { int modifier = field.getModifiers(); if (Modifier.isFinal(modifier) || Modifier.isStatic(modifier)) { continue; } fieldSet.add(field); } clazz = clazz.getSuperclass(); } return fieldSet; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy