反射概述
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
要想解剖一个类,必须先要获取到该类的字节码文件对象。而解剖使用的就是Class类中的方法.所以先要获取到每一个字节码文件对应的Class类型的对象.
什么是反射 元数据(metadata): 描述数据的描述数据 反射:得到类的元数据的过程 在运行时期,动态地去获取某一个类中的成员信息(构造器,方法,字段,内部类,接口,父类等等) 并且把类中的每一种成员,都描述程一个新类。 Class:表示所有的类 Constructor:表示所有的构造器 Method:表示所有的方法 Field:表示所有的字段
获取 Class 实例的四种方式
/**
* 1. getCLass 和 .class 出现的时期不同:Class.forName()和getClass()是在运行时加载;
* Class.class是在编译器加载,即.class是静态加载,.getClass()是动态加载
*
* 2. 除内部类外的其他类的应用上.class功能完全等于.getClass()!只是一个是用类直接获得的,
* 一个是用实例获得的
*
* java.lang.Class : 是反射的源头
* 我们创建了一个类, 通过编译(javac.exe), 生成了对应的 .class文件, 之后我们使用 java.exe
* 加载(JVM 的类加载器完成)此 .class 文件, 此 .class 文件加载到内存后, 就是一个运行时类,存
* 在缓冲区,那么这个运行时类本身就是一个 Class 实例.
* 1. 一个运行时类只被加载一次
* 2. 有了 Class 的实例以后, 我们才可以进行如下的操作,
* 1). 创建对应的运行时类的对象
* 2). 获取对应运行时类的完整结构(属性,方法,构造器,内部类,父类,异常,所在包,注解...)
* 3). 调用对应的运行时类的指定结构(属性,方法,构造器)
* 4). 反射的应用 ---> 动态代理
*/
/**
* 如何获取运行时类的实例(4种)
* @throws ClassNotFoundException
*/
@Test
public void test2()
throws ClassNotFoundException {
Class<Person> clazz1 = Person.class;
System.out.println(clazz1.getName());
Person p =
new Person();
Class clazz2 = p.getClass();
System.out.println(clazz2);
String className =
"com.anqi.refection.test.Person";
Class clazz3 = Class.forName(className);
System.out.println(clazz3);
ClassLoader classLoader =
this.getClass().getClassLoader();
Class clazz4 = classLoader.loadClass(className);
System.out.println(clazz4.getName());
}
ClassLoader
虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验、转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是Java虚拟机的类加载机制。
【三个类加载器】
1. BootStrap ClassLoader:称为启动类加载器,是Java类加载层次中最顶层的类加载器,负责加载JDK中的核心类库,如:rt.jar、resources.jar、charsets.jar等,可通过如下程序获得该类加载器从哪些地方加载了相关的jar或class文件: 2. Extension ClassLoader:称为扩展类加载器,负责加载Java的扩展类库,默认加载JAVA_HOME/jre/lib/ext/目下的所有jar。 3. App ClassLoader:称为系统类加载器,负责加载应用程序classpath目录下的所有jar和class文件。
/**
* ClassLoader 共有三层加载器
* Bootstrap classLoader
* ExtClassLoader:扩展的class loader
* AppClassLoader:系统class loader 获取不到, JVM 用来加载核心类库 获取的时候显示为 null
* @throws ClassNotFoundException
* @throws IOException
*/
@Test
public void test3()
throws ClassNotFoundException, IOException {
ClassLoader loader1 = ClassLoader.getSystemClassLoader();
System.out.println(loader1);
ClassLoader loader2 = loader1.getParent();
System.out.println(loader2);
ClassLoader loader3 = loader2.getParent();
System.out.println(loader3);
Class clazz = Person.class;
ClassLoader loader4 = clazz.getClassLoader();
System.out.println(loader4);
String className =
"java.lang.Object";
Class clazz2 = Class.forName(className);
ClassLoader loader5 = clazz2.getClassLoader();
System.out.println(loader5);
FileInputStream in =
new FileInputStream(
new File(
"db.properties"));
Properties pro =
new Properties();
pro.load(in);
String user = pro.getProperty(
"user");
String password = pro.getProperty(
"password");
System.out.println(
"user-"+user+
"password-"+password);
}
Field全解
/**
* Field -getgetDeclaredField(String name)
* 获取指定 name 值的字段
* -getField(String name)
* 获取指定 name 值的非 private 修饰的字段值
* -getFields()
* 获取所有的字段,必须所有字段都不能被 private 修饰
* -getDeclaredFields()
* 获取所有的字段
* -对 private 修饰的字段修改时,需要设置
* f1.setAccessible(true);压制修饰检查
*/
@Test
public void test1() {
Class clazz = Person.class;
Field[] fields = clazz.getFields();
for (Field field : fields) {
System.out.println(field.getName());
}
Field[] fields2 = clazz.getDeclaredFields();
for (Field field : fields2) {
System.out.println(field.getName());
}
}
@Test
public void test2() {
Class clazz = Person.class;
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
int i = field.getModifiers();
String str1 = Modifier.toString(i);
System.out.println(str1);
Class type = field.getType();
System.out.println(type.getTypeName());
System.out.println(field.getName());
}
}
@Test
public void test3()
throws Exception {
Class<Person> clazz = Person.class;
Field name = clazz.getField(
"name");
Person p = (Person)clazz.newInstance();
name.set(p,
"Angel");
System.out.println(p);
Field age = clazz.getDeclaredField(
"age");
age.setAccessible(
true);
age.set(p,
21);
System.out.println(p);
}
Method全解
@Test
public void test1() {
Class clazz = Person.class;
Method[] methods = clazz.getMethods();
for (Method method : methods) {
}
Method[] methods2 = clazz.getDeclaredMethods();
for (Method method : methods2) {
System.out.println(method);
}
}
@Test
public void test2() {
Class clazz = Person.class;
Method[] m = clazz.getDeclaredMethods();
for (Method method : m) {
Annotation[] ann = method.getAnnotations();
for (Annotation annotation : ann) {
System.out.println(annotation);
}
String str = Modifier.toString(method.getModifiers());
System.out.print(str+
" ");
Class returnType = method.getReturnType();
System.out.print(returnType.getName()+
" ");
System.out.print(method.getName());
Class[] para = method.getParameterTypes();
for (Class p : para) {
System.out.print(
"(");
System.out.print(p);
System.out.print(
")");
}
Class[] exps = method.getExceptionTypes();
if(exps.length !=
0)
System.out.println(
"throws ");
for (Class exp : exps) {
System.out.println(exp.getName());
}
}
}
@Test
public void test3()
throws Exception {
Class<Person> clazz = Person.class;
Method m1 = clazz.getMethod(
"show");
Person p = clazz.newInstance();
Object returnVal = m1.invoke(p);
System.out.println(returnVal);
Method m2 = clazz.getMethod(
"toString");
Object returnVal1 = m2.invoke(p);
System.out.println(returnVal1);
Method m3 = clazz.getMethod(
"info");
m3.invoke(Person.class);
Method m4 = clazz.getDeclaredMethod(
"display",String.class);
m4.setAccessible(
true);
m4.invoke(p,
"CHN");
}
Constructor 构造器全解
@Test
public void test1()
throws Exception {
Class<Person> clazz = (Class<Person>)
Class.forName(
"com.anqi.refection.test.Person");
Person p = clazz.newInstance();
System.out.println(p);
}
@Test
public void test2()
throws ClassNotFoundException {
String className =
"com.anqi.refection.test.Person";
Class<Person> clazz = (Class<Person>) Class.forName(className);
Constructor[] cons = clazz.getConstructors();
for (Constructor constructor : cons) {
System.out.println(constructor);
}
}
@Test
public void test3()
throws Exception{
Class<Person> clazz = Person.class;
Constructor<Person> con = clazz.getDeclaredConstructor(String.class,
int.class);
con.setAccessible(
true);
Person p = con.newInstance(
"LiChen",
18);
System.out.println(p);
}
通过反射获取父类、泛型、接口、注解、包等
/**
* 获取运行时类的父类
*/
@Test
public void test() {
Class<Person> clazz = Person.class;
Class superClass = clazz.getSuperclass();
System.out.println(superClass);
}
/**
* 获取带泛型的父类
*/
@Test
public void test2() {
Class<Person> clazz = Person.class;
Type type = clazz.getGenericSuperclass();
System.out.println(type);
}
/**
* 获取父类的泛型
*/
@Test
public void test3() {
Class<Person> clazz = Person.class;
Type type = clazz.getGenericSuperclass();
ParameterizedType t = (ParameterizedType) type;
Type[] types = t.getActualTypeArguments();
System.out.println(types[
0]);
}
/**
* 获取实现的接口
*/
@Test
public void test4() {
Class<Person> clazz = Person.class;
Class[] interfaces = clazz.getInterfaces();
for (Class class1 : interfaces) {
System.out.println(class1.getName());
}
}
/**
* 获取所在的包
*/
@Test
public void test5() {
Class<Person> clazz = Person.class;
Package p = clazz.getPackage();
System.out.println(p);
}
/**
* 获取注解
*/
@Test
public void test6() {
Class<Person> clazz = Person.class;
Annotation[] ann = clazz.getAnnotations();
for (Annotation annotation : ann) {
System.out.println(annotation);
}
}