原文链接:小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. RainbowPlan团队项目-总结

    博客介绍 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ 这个作业要求在哪里 https:// ...

  2. selenium等待方式之显示等待

    有时候,页面元素并未及时加载出来导致后面的步骤无法执行 这里就需要在加载前添加等待时间,让目标元素有足够的时间加载出来 第一种方法:使用time.sleep() 这种方法过于强制,无论元素是否加载出来 ...

  3. [C语言学习笔记二] extern 函数的用法

    extern 用来定义一个或多个变量.其后跟数据类型名和初始值.例如: extern int a =10 它与 int,long long int,double,char的本质区别,在于 extern ...

  4. SpringBoot安全篇Ⅵ --- 整合Spring Security

    知识储备: 关于SpringSecurity的详细学习可以查看SpringSecurity的官方文档. Spring Security概览 应用程序的两个主要区域是"认证"和&qu ...

  5. flask插件全家桶集成学习---持续更新ing

    不得不说flask的设计要比django要小巧精妙的多了,没有那么臃肿,只保留核心功能,其他的都需要自己引入,即各种各样的插件来满足我们的需求,我这里记录一下自己学习项目中用的插件使用方法和一些技巧总 ...

  6. CUDA 计算pi (π)

    通过简单的程序设计熟练CUDA的使用步骤 下面是cuda代码及相关注释 #include <stdio.h> #include <iostream> #include < ...

  7. 暑假第五周总结(学习HBASE相关知识)

    本周主要对HBASE的相关知识进行了学习,主要是通过视频的讲解了解到了HBASE的存储机制,HBASE的机制与普通的关系型数据库完全不同,HBASE以列进行存储,其主要执行的就是增删查操作,其更改主要 ...

  8. tf识别非固定长度图片ocr(数字+字母 n位长度可变)- CNN+RNN+CTC

    先安装必须的库 tensorflow_gpu==1.15.0 numpy opencv_python github: https://github.com/bai-shang/crnn_ctc_ocr ...

  9. Go语言实现:【剑指offer】矩阵中的路径

    该题目来源于牛客网<剑指offer>专题. 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向 ...

  10. python学习(4)循环语句

    循环语句主要有两个,一个是 while :一个是for in range() 以案例来说明: 写一个猜数字的游戏,正确的数字等于38.如果数字等于38,则提示正确,然后结束:如果数字大于38则提示大了 ...