原文链接:小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(反射)基础知识讲解的更多相关文章

  1. Java Reflection 反射基础

    反射基础: package reflection; /** * Created by : Infaraway * DATE : 2017/3/2 * Time : 23:06 * Funtion : ...

  2. Java的反射基础技术

    今天本人给大家讲解一下Java的反射基础技术,如有不对的或者讲的不好的可以多多提出,我会进行相应的更改,先提前感谢提出意见的各位了!!! 什么是反射? 反射它是根据字节码文件可以反射出类的信息.字段. ...

  3. 第76节:Java中的基础知识

    第76节:Java中的基础知识 设置环境,安装操作系统,安装备份,就是镜像,jdk配置环境,eclipse下载解压即可使用,下载tomcat 折佣动态代理解决网站的字符集编码问题 使用request. ...

  4. Java并发(基础知识)—— Executor框架及线程池

    在Java并发(基础知识)—— 创建.运行以及停止一个线程中讲解了两种创建线程的方式:直接继承Thread类以及实现Runnable接口并赋给Thread,这两种创建线程的方式在线程比较少的时候是没有 ...

  5. Html基础知识讲解

    Html基础知识讲解 <title>淄博汉企</title> </head> <body bgcolor="#66FFCC" topmar ...

  6. 【Java面试】基础知识篇

    [Java面试]基础知识篇 Java基础知识总结,主要包括数据类型,string类,集合,线程,时间,正则,流,jdk5--8各个版本的新特性,等等.不足的地方,欢迎大家补充.源码分享见个人公告.Ja ...

  7. python基础知识讲解——@classmethod和@staticmethod的作用

    python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...

  8. Java面试题-基础知识

    参考文章:Java面试题-基础知识 基础能力 什么是值传递和引用传递 线程状态有哪些,它们之间是如何转换的 进程与线程的区别,进程间如何通讯,线程间如何通讯? HashMap的数据结构是什么?如何实现 ...

  9. JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)

    一:工具类 –不存储数据,而是在数据容器上,实现高效操作 • 排序 • 搜索 –Arrays类 –Collection类 二:Arrays类(处理数组) (一)基本方法 –排序:对数组排序, sort ...

随机推荐

  1. GPU与CPU

    GPU与CPU CPU CPU,也就是中央处理器,结构主要包括控制器(指挥各部分工作).运算器(实现数据加工).寄存器.高缓以及数据/控制/状态总线.计算机的性能很大程度上依赖于CPU,CPU的功能包 ...

  2. 小程序--->小程序图片上传阿里OSS使用方法

    小程序图片上传阿里OSS使用方法 首先看下参考文档 ( http://blog.csdn.net/qq_38125123/article/details/73870667) 这里只将一些运用过程中遇到 ...

  3. 漏洞利用:验证绕过,XSS利用,Cookic盗用,文件上传

    1.      文件上传 低级别 写好上传的内容 选择好上传的文件 上传成功. 测试:访问文件,执行代码 中级别 修改文件后缀为png 上传该文件 抓包修改文件后缀为php,然后允许数据包通过. 上传 ...

  4. 一些可以查询IP地理位置、身份证所在地、手机归属地的接口

    查询IP http://ip.dnsexit.com/ 新浪的IP查询接口: 新浪的:http://counter.sina.com.cn/ip?ip=IP地址 返回Js数据,感觉不是很精确,可以把问 ...

  5. 解决dotnet错误 System.InvalidOperationException Message=Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.

    开始=>设置=>manage user certificats  (管理用户证书),里面所有的.net core的全部删除 然后控制台执行: dotnet dev-certs https ...

  6. sock.listen()

    (转载) 函数原型: int listen(int sockfd, int backlog); 当服务器编程时,经常需要限制客户端的连接个数,下面为问题分析以及解决办法: 下面只讨论TCP  UDP不 ...

  7. WeChall_Training: Crypto - Caesar I (Crypto, Training)

    As on most challenge sites, there are some beginner cryptos, and often you get started with the good ...

  8. 基于 Google-S2 的地理相册服务实现及应用

    马蜂窝技术原创内容,更多干货请关注公众号:mfwtech 随着智能手机存储容量的增大,以及相册备份技术的普及,我们可以随时随地用手机影像记录生活,在手机中存储几千张甚至上万张照片已经是很常见的事情.但 ...

  9. Linux中查看日志文件的正确姿势,求你别tail走天下了!

    作为一个后端开发工程师,在Linux中查看查看文件内容是基本操作了.尤其是通常要分析日志文件排查问题,那么我们应该如何正确打开日志文件呢?对于笔者这种小菜鸡来说,第一反应就是 cat,tail,vi( ...

  10. golang函数 和 条件语句

    /* if : if 语句 由一个布尔表达式后紧跟一个或多个语句组成 is else : if 语句 后可以使用可选的 else 语句, else 语句中的表达式在布尔表达式为 false 时执行 s ...