类加载器是用来把类(class)装载进内存的。JVM规范定义了如下类型的类的加载器。
练习:
package com.duo.reflection;
public class Test5 {
public static void main(String[] args) throws ClassNotFoundException {
//获取系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);
//获取系统类加载器的父类加载器 --> 扩展类加载器
ClassLoader parent = systemClassLoader.getParent();
System.out.println(parent);
//获取扩展类加载器的父类加载器 --> 根加载器(由C/C++编写)
ClassLoader parent1 = parent.getParent();
System.out.println(parent1);
//测试当前类是由哪个加载器加载的
ClassLoader classLoader = Class.forName("com.duo.reflection.Test5").getClassLoader();
System.out.println(classLoader);
//测试JDK内置的类是由谁加载的
classLoader = Class.forName("java.lang.Object").getClassLoader();
System.out.println(classLoader);
//如何获得系统类加载器可加载的路径?
System.out.println(System.getProperty("java.class.path"));
/*
D:\Environment\java\jdk1.8\jre\lib\charsets.jar;
D:\Environment\java\jdk1.8\jre\lib\deploy.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\dnsns.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\jaccess.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\localedata.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\nashorn.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\sunec.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;
D:\Environment\java\jdk1.8\jre\lib\ext\zipfs.jar;
D:\Environment\java\jdk1.8\jre\lib\javaws.jar;
D:\Environment\java\jdk1.8\jre\lib\jce.jar;
D:\Environment\java\jdk1.8\jre\lib\jfr.jar;
D:\Environment\java\jdk1.8\jre\lib\jfxswt.jar;
D:\Environment\java\jdk1.8\jre\lib\jsse.jar;
D:\Environment\java\jdk1.8\jre\lib\management-agent.jar;
D:\Environment\java\jdk1.8\jre\lib\plugin.jar;
D:\Environment\java\jdk1.8\jre\lib\resources.jar;
D:\Environment\java\jdk1.8\jre\lib\rt.jar;
D:\java_code\Annotation\out\production\Annotation;
D:\Program Files\IntelliJ IDEA Community Edition 2023.2.3\lib\idea_rt.jar
*/
//双亲委派机制
}
}
运行结果:
通过反射获取运行时类的完整结构
Field、Method、Constructor、Superclass、Interface、Annotation
练习通过反射获取运行时类的完整结构:
package com.duo.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
//获得类的信息
public class Test6 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class<?> c1 = Class.forName("com.duo.reflection.Person");
Person person = new Person();
c1 = person.getClass(); //通过对象来获得包名、类名
//1.获得类的名字
System.out.println(c1.getName()); //a.包名 + 类名
System.out.println(c1.getSimpleName()); //b.类名
//2.获得类的属性
System.out.println("=====================");
Field[] fields = c1.getDeclaredFields(); //a.获得全部属性
for (Field field : fields) {
System.out.println(field);
}
Field age = c1.getDeclaredField("age"); //b.获得指定属性
System.out.println(age);
//3.获得类的方法
System.out.println("=====================");
Method[] methods = c1.getMethods(); //a.获得本类及其父类的所有public方法
for (Method method : methods) {
System.out.println("getMethods: " + method);
}
methods = c1.getDeclaredMethods(); //b.获得本类的所有方法
for (Method method : methods) {
System.out.println("getDeclaredMethods: " + method);
}
Method setAge = c1.getMethod("setAge", int.class); //c.获得指定方法
System.out.println(setAge);
//3.获得构造器
System.out.println("=====================");
Constructor<?>[] constructors = c1.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
constructors = c1.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
}
}
运行结果:
【小结】
创建类的对象:调用Class对象的newInstance()方法
倘若类中没有无参构造器,只须在操作时明确调用类中的构造器,并将参数传递进去,就可以继续实例化操作。步骤如下:
调用指定的方法:通过反射,调用类中的方法,通过Method类完成
练习:
package com.duo.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//通过反射,动态地创建对象
public class Test7 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//先获得Class对象
Class<?> c1 = Class.forName("com.duo.reflection.Person");
//构造一个对象
Person person = (Person) c1.newInstance(); //本质是调用了类的无参构造器
System.out.println(person);
//通过构造器创建对象
System.out.println("=======================");
Constructor<?> declaredConstructor = c1.getDeclaredConstructor(String.class, int.class);
Person person1 = (Person) declaredConstructor.newInstance("Li", 23);
System.out.println(person1);
//通过反射调用普通方法
System.out.println("=======================");
Person person3 = (Person) c1.newInstance();
Method setAge = c1.getDeclaredMethod("setAge", int.class);
setAge.invoke(person3, 22);
System.out.println(person3.getAge());
//通过反射操作属性
System.out.println("=======================");
Person person4 = (Person) c1.newInstance();
Field name = c1.getDeclaredField("name");
name.setAccessible(true); //关闭程序的安全检测,但也会降低效率
name.set(person4, "Wang"); //name属性修饰符为private则无法直接对其进行操作,需要.setAccessible()方法设置为true
System.out.println(person4.getName());
}
}
运行结果:
Object invoke(Object obj, Object … args)
setAccessible