一、反射概念
反射就是加载类,然后获取类的属性、方法和构造函数等。
为了方便我用自己创建的Demo类来举例
package com.demo;
public class Demo {
private String name = "demo";
private int number = 001;
public long time = 0;
private String by = "Hermit_Yoshino";
public String publicString;
public Demo () {
System.out.println("This is Demo's no parameter constructors method");
}
public Demo (String name) {
this.name = name;
System.out.println("This is Demo's String constructors method");
}
private Demo (String name, int number) {
this.name = name;
this.number = number;
System.out.println("This is Demo's String and int constructors method");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void run () {
System.out.println("run");
}
public void run (String fast) {
System.out.println("run" + fast);
}
private long time () {
this.time = System.currentTimeMillis();
return time;
}
public static void main(String[] args) {
System.out.println("main");
}
}
二、加载类到内存
一共有三种加载类的方法
public class Reflex {
public void demoTest () throws ClassNotFoundException {
Class demoAClass = Class.forName("com.demo.Demo");
//这里的name “com.demo.Demo” 在eclipse中可通过右击Demo类的 Copy Quailfied Name 选项获取
Class demoBClass = new Demo().getClass();
Class demoCClass = Demo.class;
}
}
三、解剖反射类
通过使用getConstructor()、getMethod()、getField(),可以反射public成员,
如果需要反射private成员,需要使用getDeclaredConstructor()、getDeclaredMethod()、getDeclaredField()
1.反射构造方法
//以下为方法介绍
getConstructor
public Constructor<T> getConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
getDeclaredConstructor
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
public void demoConstructorTest () throws Exception {
Class demoClass = Class.forName("com.demo.Demo");
//无参构造方法
Constructor demoNullConstructor = demoClass.getConstructor(null);
Demo demoNullDemo = (Demo) demoNullConstructor.newInstance(null);
//输出
//This is Demo's no parameter constructors method
//有参构造方法
Constructor demoStringConstructor = demoClass.getConstructor(String.class);
Demo demoStringDemo = (Demo) demoStringConstructor.newInstance("demoString");
//输出
//This is Demo's String constructors method
//通过getDeclaredConstructor()进行private构造方法
Constructor demoPrivateConstructor = demoClass.getDeclaredConstructor(String.class, int.class);
demoPrivateConstructor.setAccessible(true);
Demo demoPrivateDemo = (Demo) demoPrivateConstructor.newInstance("demo1", 1);
//输出
//This is Demo's String and int constructors method
//特殊方法创建对象
Demo demoSpecialDemo = (Demo) demoClass.newInstance();
//使用此方法时必须保证有无参构造方法
//输出
//This is Demo's no parameter constructors method
}
2.反射方法
getMethod
public Method getMethod(String name,
Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
getDeclaredMethod
public Method getDeclaredMethod(String name,
Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
public void demoMethodTest () throws Exception {
Class demoClass = new Demo().getClass();
Demo objectSpecialDemo = (Demo) demoClass.newInstance();
//无参run方法
Method demoRunMethod = demoClass.getMethod("run", null);
demoRunMethod.invoke(objectSpecialDemo, null);
//输出
//run
//有参run方法
Method demoRunFastMethod = demoClass.getMethod("run", String.class);
demoRunFastMethod.invoke(objectSpecialDemo, "fast");
//输出
//runfast
//Private方法
Method demoPrivateTimeMethod = demoClass.getDeclaredMethod("time", null);
demoPrivateTimeMethod.setAccessible(true);
demoPrivateTimeMethod.invoke(objectSpecialDemo, null);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(objectSpecialDemo.time));
//输出
//2019-03-31 15:50:33
//main 方法
Method demoMainMethod = demoClass.getMethod("main", String[].class);
//以下两种方法皆可
demoMainMethod.invoke(objectSpecialDemo, (Object)new String[]{"1", "2"});
demoMainMethod.invoke(objectSpecialDemo, new Object[]{new String[]{"1", "2"}});
//输出
//main
}
3.反射字段
getField
public Field getField(String name)
throws NoSuchFieldException,
SecurityException
getDeclaredField
public Field getDeclaredField(String name)
throws NoSuchFieldException,
SecurityException
package com.demo;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
public class Reflex {
@Test
public void demoClassTest () throws Exception {
Class demoAClass = Class.forName("com.demo.Demo");
//这里的name “com.demo.Demo” 在eclipse中可通过右击Demo类的 Copy Quailfied Name 选项获取
Class demoBClass = new Demo().getClass();
Class demoCClass = Demo.class;
}
@Test
public void demoConstructorTest () throws Exception {
Class demoClass = Class.forName("com.demo.Demo");
//无参构造方法
Constructor demoNullConstructor = demoClass.getConstructor(null);
Demo objectNullDemo = (Demo) demoNullConstructor.newInstance(null);
//输出
//This is Demo's no parameter constructors method
//有参构造方法
Constructor demoStringConstructor = demoClass.getConstructor(String.class);
Demo objectStringDemo = (Demo) demoStringConstructor.newInstance("demoString");
//输出
//This is Demo's String constructors method
//通过getDeclaredConstructor()进行private构造方法
Constructor demoPrivateConstructor = demoClass.getDeclaredConstructor(String.class, int.class);
demoPrivateConstructor.setAccessible(true);
Demo objectPrivateDemo = (Demo) demoPrivateConstructor.newInstance("demo1", 1);
//输出
//This is Demo's String and int constructors method
//特殊方法创建对象
Demo objectSpecialDemo = (Demo) demoClass.newInstance();
//使用此方法时必须保证有无参构造方法
//输出
//This is Demo's no parameter constructors method
}
@Test
public void demoMethodTest () throws Exception {
Class demoClass = new Demo().getClass();
Demo objectSpecialDemo = (Demo) demoClass.newInstance();
//无参run方法
Method demoRunMethod = demoClass.getMethod("run", null);
demoRunMethod.invoke(objectSpecialDemo, null);
//输出
//run
//有参run方法
Method demoRunFastMethod = demoClass.getMethod("run", String.class);
demoRunFastMethod.invoke(objectSpecialDemo, "fast");
//输出
//runfast
//Private方法
Method demoPrivateTimeMethod = demoClass.getDeclaredMethod("time", null);
demoPrivateTimeMethod.setAccessible(true);
demoPrivateTimeMethod.invoke(objectSpecialDemo, null);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(objectSpecialDemo.time));
//输出
//2019-03-31 15:50:33
//main 方法
Method demoMainMethod = demoClass.getMethod("main", String[].class);
//以下两种方法皆可
demoMainMethod.invoke(objectSpecialDemo, (Object)new String[]{"1", "2"});
demoMainMethod.invoke(objectSpecialDemo, new Object[]{new String[]{"1", "2"}});
//输出
//main
}
@Test
public void demoFieldTest () throws Exception {
Class demoClass = Demo.class;
Demo objectSpecialDemo = (Demo) demoClass.newInstance();
//time成员变量
Field demoTimeField = demoClass.getField("time");
long timeLong = (long) demoTimeField.get(objectSpecialDemo);
System.out.println(timeLong);
//输出
//0
//publicString变量
Field demoPublicStringField = demoClass.getField("publicString");
String publicString = (String) demoPublicStringField.get(objectSpecialDemo);
System.out.println(publicString);
//输出
//null
//设置变量的值
demoPublicStringField.set(objectSpecialDemo,"publicString");
//注:可以用下函数获取变量的类型
Class demoTypeClass = demoPublicStringField.getType();
//再通过以下函数判断
if (demoTypeClass.equals(String.class)) {
publicString = (String) demoPublicStringField.get(objectSpecialDemo);
System.out.println(publicString);
}
//private变量
Field demoPrivateStringField = demoClass.getDeclaredField("by");
demoPrivateStringField.setAccessible(true);
String privateString = (String) demoPrivateStringField.get(objectSpecialDemo);
System.out.println(privateString);
//输出
//Hermit_Yoshino
}
}
四、最后
源文件已上传csdn,当然也上传了gitee
需要的可以访问
https://gitee.com/rorikontesu/javaweb_reflex_demo/tree/master
或 https://download.csdn.net/download/hermit_yoshino/11077079