java Reflection(反射)基础知识讲解
原文链接:小ben马的java Reflection(反射)基础知识讲解
1.获取Class对象的方式
1.1)使用 "Class#forName"
public static Class<?> forName(String className) throws ClassNotFoundException;
如果没有获取到Class对象,则抛出异常 ClassNotFoundException
;
eg:
Class<?> customerClazz = Class.forName("cn.xiaobenma.demo.core.reflection.VipCustomer");
1.2)使用某个类的 ".class",eg:
Class<?> customerClazz = VipCustomer.class;
1.3)某个对象的 "#getClass()",eg:
VipCustomer customer = new VipCustomer("001", "小ben马", "10086", VipCustomer.VIP_ADVANCED);
Class<?> customerClazz = customer.getClass();
2.判断是否为某个类的实例
我们通常使用 "instanceof" 来判断对象是否为某个类的实例。同样,可以使用 "Class#isInstance()" 来判断
public native boolean isInstance(Object obj);
例子:
boolean isCustomer = customer instanceof VipCustomer;
isCustomer = VipCustomer.class.isInstance(customer);
如果person为null, 上述例子都返回false。
3. 创建实例
3.1)使用 "Class#newInstance()": 要保证能访问类的无参构造方法
public T newInstance() throws InstantiationException, IllegalAccessException;
3.2)通过 "Constructor#newInstance(Object ... initargs)"
public T newInstance(Object ... initargs)
throws InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException
eg:
Constructor<VipCustomer> constructor = getVipCustomerClass().getConstructor(String.class, String.class, String.class, int.class);
VipCustomer customer = constructor.newInstance("001", "小ben马", "10086", VipCustomer.VIP_ADVANCED);
4. 获取方法(类方法、成员方法)
在Class中定义的方法如下:
//a. 获取当前类及其父类,所有`public`类方法和成员方法
public Method[] getMethods() throws SecurityException;
//b. 通过方法名、参数类型,获取单个`public`类方法或者成员方法(当前类或父类定义的)
public Method getMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException;
//c. 获取当前类(不包括超类)定义的所有类方法和成员方法
public Method[] getDeclaredMethods() throws SecurityException;
//d. 通过方法名、参数类型,获取单个当前类(不包括超类)定义的类方法或成员方法
public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException;
5. 获取构造方法
在Class中定义的方法如下:
//a. 获取当前类所有`public`构造方法
public Constructor<?>[] getConstructors() throws SecurityException;
//b. 根据参数类型,获取当前类单个`public`构造方法
public Constructor<T> getConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException;
//c. 获取当前类所有构造方法
public Constructor<?>[] getDeclaredConstructors() throws SecurityException;
//d. 根据参数类型,获取当前类单个构造方法
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException;
6. 获取变量(类变量、成员变量)
在Class中定义的方法如下:
//a. 获取当前类及其超类,所有`public`类变量和成员变量
public Field[] getFields() throws SecurityException;
//b. 通过名称,获取当前类或超类,单个`public`类变量或者成员变量
public Field getField(String name)
throws NoSuchFieldException, SecurityException;
//c. 获取当前类(不包括超类),所有类变量和成员变量
public Field[] getDeclaredFields() throws SecurityException;
//d. 通过名称,获取当前类(不包括超类),单个类变量或成员变量
public Field getDeclaredField(String name)
throws NoSuchFieldException, SecurityException;
7. 通过inovke,调用对象的方法
eg:
Method setRank = getVipCustomerClass().getDeclaredMethod("setRank", int.class);
setRank.invoke(customer, VipCustomer.VIP_NORMAL);
8.获取和修改类变量或者成员变量的值
8.1) Field#set(Object obj, Object value): 通过对象和变量值,设置变量,eg:
Field field = getVipCustomerClass().getDeclaredField("rank");
field.set(customer, VipCustomer.VIP_ADVANCED);
8.2) Field#get(Object obj): 通过对象获取变量值,eg:
Field field = getVipCustomerClass().getDeclaredField("rank");
int rank = (int) field.get(customer);
9.动态改变方法和变量的可访问性
在使用反射获取被调用类的构造方法、方法或变量,可能对于调用类是不可访问的,如被调用类的"private"构造方法,"private" 方法, "private" 变量,会抛出 IllegalAccessException。
java可以通过使用 "AccessibleObject#setAccessible(boolean flag)" 改变可访问性。
"Constructor"、"Method" 和 "Field" 都是 "AccessibleObject" 的子类。
eg:
//在VipCustomer中定义类静态常量PRI_NO=100
Field field = getVipCustomerClass().getDeclaredField("PRI_NO");
field.setAccessible(true);
Assert.assertEquals(100, field.get(null));
//私有构造方法
Constructor<VipCustomer> constructor = getVipCustomerClass().getDeclaredConstructor();
constructor.setAccessible(true);
VipCustomer customer = constructor.newInstance();
Assert.assertNull(customer.getCustomerNo());
Assert.assertNull(customer.getName());
Assert.assertNull(customer.getMobilePhone());
//调用私有类方法
Method method = getVipCustomerClass().getDeclaredMethod("doNothingByVipCustomer");
method.setAccessible(true);
method.invoke(null);
10.利用反射创建数组
数组是比较特殊的类型。
10.1) Array#newInstance(Class<?> componentType, int length),创建一维数组,eg:
//一维数组
Object array = Array.newInstance(String.class, 2);
Array.set(array, 0, "小ben马");
Array.set(array, 1, "xiaobenma");
Assert.assertEquals("小ben马", Array.get(array, 0));
Assert.assertEquals("xiaobenma", Array.get(array, 1));
10.2) Array#newInstance(Class<?> componentType, int... dimensions),创建多维数组,eg:
//多维数组
//String[2][1]
Object arrays = Array.newInstance(String.class, 2, 1);
Object array0 = Array.newInstance(String.class, 1);
Array.set(array0, 0, "小ben马");
Array.set(arrays, 0, array0);
Object array1 = Array.newInstance(String.class, 1);
Array.set(array1, 0, "xiaobenma");
Array.set(arrays, 1, array1);
Assert.assertEquals("小ben马", Array.get(Array.get(arrays, 0), 0));
Assert.assertEquals("xiaobenma", Array.get(Array.get(arrays, 1), 0));
10.3)Array#get(Object array, int index)
根据数组和相关索引获取对应的值
10.4)Array#set(Object array, int index, Object value)
根据索引和相关索引,设置对应的值
11.Modifier说明
通常在类中
- 定义构造方法格式: [修饰符列表] 类名([参数列表])。
- 定义变量的格式为: [修饰符列表] 返回类型 变量名称。
- 定义方法的格式为: [修饰符列表] 返回类型 方法名([参数列表])。
"Member#getModifiers()" 返回一个int类型,通过解释int的值,能获取定义的修饰符列表。"Constructor"、"Method" 和 "Field" 都是 "Member" 的实现类。
修饰符返回的int值,需要通过 "Modifier" 解析。
其中,
- Modifier#toString(int mod): 打印定义的所有修饰符
- Modifier#isXXX(int mod): 判断修饰符的类型
如果你看过Modifier
的源码,你会发现一个有趣的事情,修饰符是按bit
位定义的,如:
/**
* The {@code int} value representing the {@code public}
* modifier.
*/
public static final int PUBLIC = 0x00000001;
/**
* The {@code int} value representing the {@code private}
* modifier.
*/
public static final int PRIVATE = 0x00000002;
/**
* The {@code int} value representing the {@code protected}
* modifier.
*/
public static final int PROTECTED = 0x00000004;
ps说明
- 反射相关包
java.lang.reflect
Proxy
是反射中比较重要的应用,在后续博客单独更新。- 相关demo代码 core-java-learning
java Reflection(反射)基础知识讲解的更多相关文章
- Java Reflection 反射基础
反射基础: package reflection; /** * Created by : Infaraway * DATE : 2017/3/2 * Time : 23:06 * Funtion : ...
- Java的反射基础技术
今天本人给大家讲解一下Java的反射基础技术,如有不对的或者讲的不好的可以多多提出,我会进行相应的更改,先提前感谢提出意见的各位了!!! 什么是反射? 反射它是根据字节码文件可以反射出类的信息.字段. ...
- 第76节:Java中的基础知识
第76节:Java中的基础知识 设置环境,安装操作系统,安装备份,就是镜像,jdk配置环境,eclipse下载解压即可使用,下载tomcat 折佣动态代理解决网站的字符集编码问题 使用request. ...
- Java并发(基础知识)—— Executor框架及线程池
在Java并发(基础知识)—— 创建.运行以及停止一个线程中讲解了两种创建线程的方式:直接继承Thread类以及实现Runnable接口并赋给Thread,这两种创建线程的方式在线程比较少的时候是没有 ...
- Html基础知识讲解
Html基础知识讲解 <title>淄博汉企</title> </head> <body bgcolor="#66FFCC" topmar ...
- 【Java面试】基础知识篇
[Java面试]基础知识篇 Java基础知识总结,主要包括数据类型,string类,集合,线程,时间,正则,流,jdk5--8各个版本的新特性,等等.不足的地方,欢迎大家补充.源码分享见个人公告.Ja ...
- python基础知识讲解——@classmethod和@staticmethod的作用
python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...
- Java面试题-基础知识
参考文章:Java面试题-基础知识 基础能力 什么是值传递和引用传递 线程状态有哪些,它们之间是如何转换的 进程与线程的区别,进程间如何通讯,线程间如何通讯? HashMap的数据结构是什么?如何实现 ...
- JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)
一:工具类 –不存储数据,而是在数据容器上,实现高效操作 • 排序 • 搜索 –Arrays类 –Collection类 二:Arrays类(处理数组) (一)基本方法 –排序:对数组排序, sort ...
随机推荐
- Ogre 的Node 位移、旋转
位移旋转有三种方式TS_LOCAL,TS_PARENT,TS_WORLD. TS_LOCAL是指自身坐标系,TS_PARENT是父节点坐标系,TS_WORLD是世界坐标系 比如 translate( ...
- qsort 函数笔记
函数声明 void qsort(void *base, size_t nitems, size_t size, int (*compare)(const void *, const void*)); ...
- robotframework,移动端(小程序)自动化,获取元素属性值的方法
如下图,获取商品价格 属性值显示在content-desc内 传统的get text指定是无法获得到这个元素指定属性的值的 只有通过使用AppiumLibrary.get element attrib ...
- Struts(六)
JSON(JavaScript Object Notation) 1.一种轻量级的数据交换格式 2.通常用于在客户端和服务器之间传递数据 3.jQuery的所有参数都是以JSON格式 ...
- 05讲基础篇:某个应用的CPU使用率居然达到100%,我该怎么办
小结 CPU 使用率是最直观和最常用的系统性能指标,更是我们在排查性能问题时,通常会关注的第一个指标.所以我们更要熟悉它的含义,尤其要弄清楚用户(%user).Nice(%nice).系统(%syst ...
- CSS绝对定位absolute详解
转:https://www.jianshu.com/p/a3da5e27d22b 之前介绍过CSS浮动float详解,本篇介绍的绝对定位absolute和浮动float有部分相似性.如果能理解 ...
- Android Studio MainActivity中的R为红色
csdn解决链接 https://blog.csdn.net/M283592338/article/details/79880413
- Go语言学习之goroutine
协程Coroutine 特点 轻量级的"线程" 非抢占式多任务处理,由协程主动交出控制权 编译器/解释器/虚拟机层面的多任务,非操作系统 多个协程可以在一个或多个线程上执行 go关 ...
- asp.net core系列 WebAPI 作者:懒懒的程序员一枚
asp.net core系列 36 WebAPI 搭建详细示例一.概述1.1 创建web项目1.2 添加模型类1.3 添加数据库上下文1.4 注册上下文1.5 添加控制器1.6 添加Get方法1.7 ...
- Qt中字符串和数值之间的转换
来自<Qt5.9 C++开发指南> 普通数值和字符串之间的转换 一.从字符串转换为数值 QString类从字符串转换为整数的函数有: int QString::toInt(bool *ok ...