1、什么是反射技术?

  动态获取指定类以及类中的内容(成员),并运行其内容。

  应用程序已经运行,无法在其中进行new对象的建立,就无法使用对象。这时可以根据配置文件的类全名去找对应的字节码文件,并加载进内存,并创建该类对象实例。这就需要使用反射技术完成。反射技术最重要的就是Class字节码对象。其次有Constructor、Method、Field等类。

  其实,反射机制的非常重要的一个类就是Class字节码对象,获取方式有三种:

class Parent {
protected String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
} class Children extends Parent {
private static final Integer age = new Integer(25); public Children(String name) {
this.name = name;
} public static Integer getAge() {
return age;
}
}

测试获取Class对象的三种方式:

    public static void testClazz() throws ClassNotFoundException {
Children children = new Children("zhangsan");
Class<? extends Children> class1 = children.getClass();// 获取class对象的第一种方式
System.out.println(class1);
Class class2 = Children.class;// 获取class对象的第二种方式
System.out.println(class2);
Class class3 = Class.forName("cn.xm.exam.test.Children");// 获取class对象的第三种方式(要写全路径)
System.out.println(class3);
}

结果:

class cn.xm.exam.test.Children
class cn.xm.exam.test.Children
class cn.xm.exam.test.Children

总结:获取Class对象的三种方式:

  Class.forName("类的路径")

  类名.Class

  实例.getClass()

2. Class字节码对象的作用一: newInstance和获取Constructor对象

修改Children类:

class Children extends Parent {
private static final Integer age = new Integer(25); public Children() {
System.out.println("无参构造方法");
} private Children(String name) {
System.out.println("有参构造方法");
this.name = name;
} public static Integer getAge() {
return age;
}
}

2.1  clazz.newInstance();// 其内部是调用无参的Constructor对象进行创建对象

        Class clazz = Children.class;// 获取class对象的第二种方式
Object newInstance = clazz.newInstance();// 其内部是调用无参构造方法进行创建
System.out.println(newInstance);

结果:

无参构造方法
cn.xm.exam.test.Children@4614ac54

如果没有无参构造方法或者无参构造方法的修饰符是private,调用此方法会报错java.lang.IllegalAccessException

2.2 获取Constructor对象

    public static void test2() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException {
// 1.获取所有的构造
Class clazz = Children.class;// 获取class对象的第二种方式
Constructor[] constructors = clazz.getConstructors();// 获取所有public声明的构造方法
blConstructor(constructors);
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();// 获取所有public\private\protected\default声明的构造方法
blConstructor(declaredConstructors); // 2.根据参数类型获取构造方法
Constructor constructor = clazz.getDeclaredConstructor(String.class);// 获取参数类型为string修饰符为private的构造方法(因为是private修饰,所以只能用此方法获取)
blConstructor(new Constructor[] { constructor });
Constructor declaredConstructor = clazz.getConstructor(null);// 获取无参构造方法,修饰符为只能为public
blConstructor(new Constructor[] { declaredConstructor });
} public static void blConstructor(Constructor[] constructors) {
System.out.println("=============");
for (Constructor constructor : constructors) {
System.out.println("name ->" + constructor.getName());
Class[] parameterTypes = constructor.getParameterTypes();// 获取构造方法的参数类型数组
for (Class clazz11 : parameterTypes) {
System.out.println("clazz11->" + clazz11);
}
System.out.println("isAccessible ->" + constructor.isAccessible());// isAccessible返回的是是否是private声明的
}
}

结果:

=============
name ->cn.xm.exam.test.Children
isAccessible ->false
=============
name ->cn.xm.exam.test.Children
clazz11->class java.lang.String
isAccessible ->false
name ->cn.xm.exam.test.Children
isAccessible ->false
=============
name ->cn.xm.exam.test.Children
clazz11->class java.lang.String
isAccessible ->false
=============
name ->cn.xm.exam.test.Children
isAccessible ->false

总结:    getConstructors()返回所有声明为public的构造方法

    getDeclaredConstructors()获取所有的方法,不管修饰符

    getDeclaredConstructor(String.class);//获取单参数,且类型为String的构造方法,修饰符可以为public\private\protected\default

    getConstructor(null);// 获取无参构造方法,修饰符为只能为public

      带Declared的方法可以获取到private修饰的构造方法,isAccessible方法返回的是此对象的可访问标志的值(默认返回的是false)。

2.3 Constructor对象创建实例

  Constructor可以创建对象,也可以获取构造方法的参数类型等。

    public static void test2() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class clazz = Children.class;// 获取class对象的第二种方式
// 2.根据参数类型获取构造方法
Constructor constructor = clazz.getDeclaredConstructor(String.class);// 获取参数类型为string修饰符为private的构造方法(因为是private修饰,所以只能用此方法获取)
Constructor declaredConstructor = clazz.getConstructor(null);// 获取无参构造方法,修饰符为只能为public System.out.println("=====================");
Object newInstance1 = declaredConstructor.newInstance(); System.out.println("=====================");
Object newInstance = constructor.newInstance("111");
}

结果:(由于带参数的构造是private修饰的,所以不能直接new)

=====================
无参构造方法
=====================
Exception in thread "main" java.lang.IllegalAccessException: Class cn.xm.exam.test.Test can not access a member of class cn.xm.exam.test.Children with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:110)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:262)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:254)
at java.lang.reflect.Constructor.newInstance(Constructor.java:517)
at cn.xm.exam.test.Test.test2(Test.java:31)
at cn.xm.exam.test.Test.main(Test.java:8)

  

解决办法:(调用创建对象之前设置可见性为true)

    public static void test2() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class clazz = Children.class;// 获取class对象的第二种方式
// 2.根据参数类型获取构造方法
Constructor constructor = clazz.getDeclaredConstructor(String.class);// 获取参数类型为string修饰符为private的构造方法(因为是private修饰,所以只能用此方法获取)
Constructor declaredConstructor = clazz.getConstructor(null);// 获取无参构造方法,修饰符为只能为public System.out.println("=====================");
Object newInstance1 = declaredConstructor.newInstance(); System.out.println("=====================");
constructor.setAccessible(true);// 暴力可见
Object newInstance = constructor.newInstance("111");
}

结果:

=====================
无参构造方法
=====================
有参构造方法

3. Method对象

修改Children类:

class Children extends Parent {
private static final Integer age = new Integer(25); public Children() {
System.out.println("无参构造方法");
} private Children(String name) {
System.out.println("有参构造方法");
this.name = name;
} public static Integer getAge() {
return age;
} private void method1() {
System.out.println("method1......");
} private void method1(String arg) {
System.out.println("method1......" + arg);
} public void method2() {
System.out.println("method1......");
}
}

3.1 Method对象的获取方式

  此对象也是有四种获取方式,带Declared的可以获取任意修饰符的。

        Class clazz = Children.class;// 获取class对象的第二种方式
// 2.根据参数类型获取构造方法
Constructor constructor = clazz.getDeclaredConstructor(String.class);// 获取参数类型为string修饰符为private的构造方法(因为是private修饰,所以只能用此方法获取)
constructor.setAccessible(true);// 暴力可见
Object newInstance = constructor.newInstance("111");
Method[] methods = clazz.getMethods();
Method[] declaredMethods = clazz.getDeclaredMethods();
Method method = clazz.getMethod("method2", null);
Method declaredMethod = clazz.getDeclaredMethod("method1", String.class);

3.1 Method对象的作用

  Method对象可以获取方法的名字、返回类型、参数类型、异常类型、注解等信息,另外可以通过此实例直接调用方法。调用静态方法传入第一个参数传入null即可。

    public static void test2() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class clazz = Children.class;// 获取class对象的第二种方式
Constructor constructor = clazz.getDeclaredConstructor(String.class);// 获取参数类型为string修饰符为private的构造方法(因为是private修饰,所以只能用此方法获取)
constructor.setAccessible(true);// 暴力可见
Object newInstance = constructor.newInstance("111");
// 2.根据参数类型获取方法
Method method = clazz.getMethod("getAge", null);
Method declaredMethod = clazz.getDeclaredMethod("method1", String.class);
// 2.1获取方法名字、返回类型、参数类型
System.out.println(
method.getName() + "\t" + method.getReturnType() + "\t" + Arrays.toString(method.getParameterTypes()));
// 2.2 .2执行private修饰的方法(第一个参数是对象,第二个是参数)
declaredMethod.setAccessible(true);// 设置暴力可见
declaredMethod.invoke(newInstance, "111");
// 2.2 .2静态方法的执行
System.out.println(method.invoke(null));
}

结果:

有参构造方法
getAge class java.lang.Integer []
method1......111
25

4. Field对象

  Field相当于成员属性、字段。

修改Children类:

class Children extends Parent {
private static final Integer age = new Integer(25);
private String sex;
public int score; public Children() {
System.out.println("无参构造方法");
} private Children(String name) {
System.out.println("有参构造方法");
this.name = name;
} public static Integer getAge() {
return age;
}
}

4.1 Field的获取方式

        Class clazz = Children.class;// 获取class对象的第二种方式
Constructor constructor = clazz.getDeclaredConstructor(String.class);// 获取参数类型为string修饰符为private的构造方法(因为是private修饰,所以只能用此方法获取)
constructor.setAccessible(true);// 暴力可见
Object newInstance = constructor.newInstance("111");
// 获取Field对象
Field[] fields = clazz.getFields();
System.out.println(Arrays.toString(fields));
Field[] declaredFields = clazz.getDeclaredFields();
System.out.println(Arrays.toString(declaredFields));
Field field = clazz.getField("score");
System.out.println(field);
Field declaredField = clazz.getDeclaredField("age");
System.out.println(declaredField);

结果:

有参构造方法
[public int cn.xm.exam.test.Children.score]
[private static final java.lang.Integer cn.xm.exam.test.Children.age, private java.lang.String cn.xm.exam.test.Children.sex, public int cn.xm.exam.test.Children.score]
public int cn.xm.exam.test.Children.score
private static final java.lang.Integer cn.xm.exam.test.Children.age

4.2 Field的作用

  Field实例可以获取字段的类型、值,也可以修改字段的值。

        Class clazz = Children.class;// 获取class对象的第二种方式
Constructor constructor = clazz.getDeclaredConstructor(String.class);// 获取参数类型为string修饰符为private的构造方法(因为是private修饰,所以只能用此方法获取)
constructor.setAccessible(true);// 暴力可见
Object newInstance = constructor.newInstance("111");
// 获取Field对象
Field field = clazz.getField("score");
System.out.println(field.getType() + "\t" + field.get(newInstance));
// 修改成员属性的值
field.set(newInstance, 80);
System.out.println(field.getType() + "\t" + field.get(newInstance));
// 获取静态成员属性
Field declaredField = clazz.getDeclaredField("age");
declaredField.setAccessible(true);// 暴力可见
System.out.println(declaredField.getType() + "\t" + declaredField.get(null));

结果:

有参构造方法
int 0
int 80
class java.lang.Integer 25

field提供了获取字段基本类型的值与引用类型的值get方法,也提供了修改基本类型与引用类型值的set方法:(第一个参数是实例对象,如果是静态属性传入null就可以)

      

总结:

1.三种方法可以获取到Class类:

  Class.forName("类的路径")

  类名.Class

  实例.getClass()

2.五种创建对象的方法:

  通过new语句实例化一个对象

  通过反射机制创建对象,class.newInstance()

  通过反射机制创建对象,constructor.newInstance()

  通过clone()方法创建一个对象

  通过反序列化方式创建对象

3.获取构造方法对象Constructor、方法对象Method、字段对象Field都有四种方法。(两个返回数组、两个返回单个实例)

  直接调用getXXX方法例如  getConstructors 返回的是public修饰的方法,而且包括从父类继承的方法

  调用getDeclaredXXX方法获取的不管修饰符,但是不包括从父类继承的方法。

  也可以根据方法名称与类型或者字段名称来获取对应的单个实例。

反射的基本使用以及原理(Class获取方式)的更多相关文章

  1. 反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性、字段),而不去使用Invoke方法)

    反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性.字段),而不去使用Invoke方法)   创建Delegate (1).Delegate.CreateDelegate(Type, ...

  2. MFC中 SDI/MDI框架各部分指针获取方式

    VC MFC SDI/MDI框架各部分指针获取方式   整理总结一下,希望能帮助到别人.   获得CWinApp 获得CMainFrame 获得CChildFrame 获得CDocument 获得CV ...

  3. geohash 算法原理及实现方式

    转自:http://www.cnblogs.com/dengxinglin/archive/2012/12/14/2817761.html geohash 算法原理及实现方式 1.geohash 特点 ...

  4. 爬虫 xpath 获取方式

    回顾 bs4 实例化bs对象,将页面源码数据加载到该对象中 定位标签:find('name',class_='xxx') findall() select() 将标签中的文本内容获取 string t ...

  5. CPU使用率原理及计算方式

    本文转载自CPU使用率原理及计算方式 CPU:超线程和多核 超线程(Hyper-Threading ) 超线程是Intel最早提出一项技术,最早出现在2002年的Pentium4上.单个采用超线程的C ...

  6. [转]Android SHA1与Package获取方式

    转自高德地图LBS Android SHA1与Package获取方式 获取应用包名 打开Android 应用工程的 AndroidManifest.xml配置文件,package 属性所对应的内容为应 ...

  7. JQ关于浏览器宽高的获取方式

    JQ关于浏览器宽高的获取方式 alert($(window).height()); //浏览器时下窗口可视区域高度alert($(document).height()); //浏览器时下窗口文档的高度 ...

  8. Unity---资源管理中不同资源的路径获取方式

    1.首先需要先了解两个知识点: Unity内置的文件路径获取方式.windows的Directory.GetFiles文件获取方式:   1>Unity内置的文件路径获取方式,一下是官方解释:h ...

  9. RabbitMQ消费端消息的获取方式(.Net Core)

    1[短链接]:BasicGet(String queue, Boolean autoAck) 通过request的方式独自去获取消息,断开式,一次次获取,如果返回null,则说明队列中没有消息. 隐患 ...

随机推荐

  1. 二十一、MySQL NULL 值处理

    MySQL NULL 值处理 我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. ...

  2. 用python给图片添加半透明水印

    # coding:utf-8 from PIL import Image, ImageDraw, ImageFont def add_text_to_image(image, text): font ...

  3. Codeforces Round #459 (Div. 2)-A. Eleven

    A. Eleven time limit per test1 second memory limit per test256 megabytes Problem Description Eleven ...

  4. 笔记-python-lib-内置函数

    笔记-python-lib-内置函数 注:文档来源为Python3.6.4官方文档 1.      built-in functions abs(x) 返回绝对值 all(iterable)   re ...

  5. hibernate实体xml一对多关系映射

    单向一对多关系映射: 一个房间对应多个使用者,也就是Room實例知道User實例的存在,而User實例則沒有意識到Room實例. 用户表: package onlyfun.caterpillar; p ...

  6. Python虚拟机类机制之对象模型(一)

    Python对象模型 在Python2.2之前,Python中存在着一个巨大的裂缝,就是Python的内置类type,比如:int和dict,这些内置类与程序员在Python中自定义的类并不是同一级别 ...

  7. Python爬虫作业

    题目如下:   请分析作业页面(https://edu.cnblogs.com/campus/hbu/Python2018Fall/homework/2420),    爬取已提交作业信息,并生成已提 ...

  8. Leetcode 630.课程表III

    课程表III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d天时要完成,你将会从第 ...

  9. mojoportal中使用jquey的插件

    以前在mojo中使用jquery的插件,都是把插件的文件内容直接写到了相关的模块中,这样的问题是不整洁,一大串代码. 如果直接在layout.master中引入插件文件,或者在自定义模块中引入插件文件 ...

  10. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo-client'.

    cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ...