博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
反射知识点
阅读量:7024 次
发布时间:2019-06-28

本文共 4582 字,大约阅读时间需要 15 分钟。

hot3.png

  • s.getClass().getInterfaces() : 表示获取该对象的所有实现的接口数组
public class ReflectUtil {    /**     * 反射执行方法     *     * @param classLoader     * @param className     * @param methodName     * @param receiver     * @param classes     * @param params     * @return     * @throws Throwable     */    public static Object invoke(ClassLoader classLoader, String className, String methodName, Object receiver, Class[] classes, Object[] params) throws Throwable {        return classLoader.loadClass(className).getMethod(methodName, classes).invoke(receiver, params);    }    /**     * 获取泛型类型     *     * @param o     * @return     */    public static Class getTClass(Object o) {        try {            Type genType = o.getClass().getGenericSuperclass();            Type[] params = ((ParameterizedType) genType).getActualTypeArguments();            Class entityClass = (Class) params[0];            return entityClass;        } catch (Exception e) {        }        try {            Type[] genType = o.getClass().getGenericInterfaces();            Type[] params = ((ParameterizedType) genType[0]).getActualTypeArguments();            Class entityClass = (Class) params[0];            return entityClass;        } catch (Exception e) {        }        return null;    }    public static Type[] getTClasses(Object o) {        Type genType = o.getClass().getGenericSuperclass();        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();        return params;    }    /**     * 复制属性     *     * @param toObj     * @param fromObj     */    public static void copyFields(Object toObj, Object fromObj) {        Class toClass = toObj.getClass();        Class fromClass = fromObj.getClass();        HashSet
fields = new HashSet<>(); for (Field field : toClass.getFields()) { fields.add(field); } for (Field field : toClass.getDeclaredFields()) { fields.add(field); } for (Field toField : fields) { toField.setAccessible(true); try { Field fromField = fromClass.getField(toField.getName()); if (fromField.getType() == toField.getType()) { fromField.setAccessible(true); toField.set(toObj, fromField.get(fromObj)); } } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } public static void copyFieldsByAnnotation(Object toObj, Object fromObj) { Class toClass = toObj.getClass(); Class fromClass = fromObj.getClass(); HashSet
fields = new HashSet<>(); for (Field field : toClass.getFields()) { fields.add(field); } for (Field field : toClass.getDeclaredFields()) { fields.add(field); } for (Field toField : fields) { toField.setAccessible(true); for (Annotation annotation : toField.getAnnotations()) { if (annotation instanceof FromOld) { FromOld fromOld = (FromOld) annotation; try { String oldName = fromOld.oldName(); if (TextUtils.isEmpty(oldName)) { oldName = toField.getName(); } Field fromField = fromClass.getField(oldName); if (fromField.getType() == toField.getType()) { fromField.setAccessible(true); toField.set(toObj, fromField.get(fromObj)); } } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } } public static Object getField(Object obj, Class
cl, String field) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field localField = cl.getDeclaredField(field); localField.setAccessible(true); return localField.get(obj); } public static void setField(Object obj, Class
cl, String field, Object value) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field localField = cl.getDeclaredField(field); localField.setAccessible(true); localField.set(obj, value); }}

转载于:https://my.oschina.net/u/3246345/blog/1114068

你可能感兴趣的文章
am335x uboot, kernel 编译
查看>>
把mysql脚本或其他数据库脚本导入Powerdesigner
查看>>
phalcon 连接多个数据库 phalcon multi-database
查看>>
React Native(十一)——按钮重复点击事件的处理
查看>>
zepto jquery和zepto的区别?
查看>>
机器学习笔记(4):多类逻辑回归-使用gluton
查看>>
26.angularJS $routeProvider
查看>>
内存映射函数remap_pfn_range学习——示例分析(2)
查看>>
年轻的工程师如何月入伍万XD
查看>>
NAT64与DNS64基本原理概述
查看>>
Java-Shiro(四):Shiro
查看>>
Oracle 备份、恢复单表或多表数据步骤
查看>>
Windows安装Linux子系统--安装GUI界面
查看>>
raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not...
查看>>
Easy Web Development Framework for Java
查看>>
python多线程学习 - Rollen Holt - 博客园
查看>>
Redis命令总结
查看>>
字符串分割(C++)
查看>>
window.parent与window.openner区别
查看>>
oracle - 函数
查看>>