本文和大家聊聊Java反射。
什么是Java反射(Reflection)?
在程序運(yùn)行的過(guò)程中,能夠動(dòng)態(tài)的檢索類相關(guān)的數(shù)據(jù)結(jié)構(gòu)信息(包含構(gòu)造器、字段、方法),并能對(duì)其進(jìn)行操控的技術(shù)。
Java反射能做什么?
- 獲取類的構(gòu)造器
- 獲取類的屬性(成員變量)
- 獲取類的方法
- 調(diào)用類的構(gòu)造器
- 調(diào)用類的方法
- 讀取或修改類的屬性值
Java反射有哪些應(yīng)用場(chǎng)景?
咱們最常見(jiàn)的,開(kāi)發(fā)人員在IDE(IDEA)中想調(diào)用類中某個(gè)方法或?qū)傩詴r(shí),在類對(duì)象后面一按“.”,IDE就會(huì)自動(dòng)列出它的屬性或方法,這里就用到了反射。
一些通用框架中也使用反射,以下以Spring為例:
package com.hys;
/**
* JavaBean
*/
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- Beans.xml JavaBean 的配置文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.hys.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
package com.hys;
/**
* 主應(yīng)用
*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
執(zhí)行結(jié)果:
Your Message : Hello World!
上面的例子可以看出,Spring框架在Beans.xml中檢索到id是“helloWorld”的<bean>,通過(guò)<bean>的class屬性加載com.hys.HelloWorld類,然后通過(guò)反射將<property>的value的內(nèi)容賦值給com.hys.HelloWorld類的message屬性。
下面咱們通過(guò)一個(gè)一個(gè)的小例子,來(lái)掌握J(rèn)ava反射如何使用
顯示類所有的方法(包含方法的所信息)
package com.hys;
import java.lang.reflect.Method;
public class Main {
public String method1(String s){
return s;
}
private int method2(Object p, int x) throws NullPointerException {
if (p == null)
throw new NullPointerException();
return x;
}
public static void main(String[] args) {
try {
Class cls = Class.forName("com.hys.Main");//
Method methlist[] = cls.getDeclaredMethods();//獲得所有方法(包含private)
for (int i = 0; i < methlist.length; i++) {
Method m = methlist[i];
System.out.println("name = " + m.getName());//獲取方法名
System.out.println("decl class = " + m.getDeclaringClass());// 獲取聲明方法的類
Class pvec[] = m.getParameterTypes();//獲取方法的參數(shù)類型數(shù)組
for (int j = 0; j < pvec.length; j++) {
System.out.println("param #" + j + " " + pvec[j]);
}
Class evec[] = m.getExceptionTypes();//獲取方法拋出的異常類型數(shù)組
for (int j = 0; j < evec.length; j++) {
System.out.println("exc #" + j + " " + evec[j]);
}
System.out.println("return type = " + m.getReturnType());//獲取方法返回值類型
}
}
catch (Throwable e) {
System.err.println(e);
}
}
}
執(zhí)行結(jié)果:
name = main
decl class = class com.hys.Main
param #0 class [Ljava.lang.String;
return type = void
name = method2
decl class = class com.hys.Main
param #0 class java.lang.Object
param #1 int
exc #0 class java.lang.NullPointerException
return type = int
name = method1
decl class = class com.hys.Main
param #0 class java.lang.String
return type = class java.lang.String
顯示類的所有構(gòu)建器信息
package com.hys;
import java.lang.reflect.Constructor;
public class Main {
public Main() {
}
protected Main(int i, double d) {
}
public static void main(String[] args) {
try {
Class cls = Class.forName("com.hys.Main");
Constructor ctorlist[] = cls.getDeclaredConstructors();//獲取所有構(gòu)造器(包含private)
for (int i = 0; i < ctorlist.length; i++) {
Constructor ct = ctorlist[i];
System.out.println("name = " + ct.getName()); //獲取構(gòu)造器名稱
System.out.println("decl class = " + ct.getDeclaringClass());//獲取聲明構(gòu)造器的類
Class pvec[] = ct.getParameterTypes();//獲取構(gòu)造器參數(shù)的類型列表
for (int j = 0; j < pvec.length; j++) {
System.out.println("param #" + j + " " + pvec[j]);
}
}
}
catch (Throwable e) {
System.err.println(e);
}
}
}
執(zhí)行結(jié)果:
name = com.hys.Main
decl class = class com.hys.Main
name = com.hys.Main
decl class = class com.hys.Main
param #0 int
param #1 double
顯示類的所有字段信息
package com.hys;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Main {
private double d;
public static final int i = 37;
String s = "testing";
public static void main(String[] args) {
try {
Class cls = Class.forName("com.hys.Main");
Field fieldlist[] = cls.getDeclaredFields();//獲取類的所有字段(包含private)
for (int i= 0; i < fieldlist.length; i++) {
Field fld = fieldlist[i];
System.out.println("name = " + fld.getName());//獲取屬性名
System.out.println("type = " + fld.getType());//獲取屬性類型
int mod = fld.getModifiers();//獲取屬性修飾符
System.out.println("modifiers = " + Modifier.toString(mod));
}
}
catch (Throwable e) {
System.err.println(e);
}
}
}
執(zhí)行結(jié)果:
name = d
type = double
modifiers = private
name = i
type = int
modifiers = public static final
name = s
type = class java.lang.String
modifiers =
調(diào)用類的方法
package com.hys;
import java.lang.reflect.Method;
public class Main {
public int add(int a, int b)
{
return a + b;
}
public static void main(String[] args) {
try {
Class cls = Class.forName("com.hys.Main");
Object instanceObj = cls.newInstance();//創(chuàng)建類對(duì)象實(shí)例
Class partypes[] = new Class[2];//為方法創(chuàng)建參數(shù)類型數(shù)組(add(int,int))
partypes[0] = Integer.TYPE;
partypes[1] = Integer.TYPE;
Method addMethod = cls.getMethod("add", partypes);// 通過(guò)方法名稱,獲取方法對(duì)象
Object arglist[] = new Object[2];//為方法創(chuàng)建參數(shù)值數(shù)組
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retObj = addMethod.invoke(instanceObj, arglist);// 調(diào)用add方法
Integer retVal = (Integer)retObj;
System.out.println(retVal.intValue());
}
catch (Throwable e) {
System.err.println(e);
}
}
}
執(zhí)行結(jié)果:
84
修改類的屬性值
package com.hys;
import java.lang.reflect.Field;
public class Main {
public double d;
public static void main(String[] args) {
try {
Class cls = Class.forName("com.hys.Main");
Object instanceObj = cls.newInstance();
Field fieldDouble = cls.getField("d");//獲得類的屬性對(duì)象
System.out.println("d = " + ((Main)instanceObj).d);
fieldDouble.setDouble(instanceObj, 12.34);//修改類的屬性值
System.out.println("d = " + ((Main)instanceObj).d);
}
catch (Throwable e) {
System.err.println(e);
}
}
}
執(zhí)行結(jié)果:
d = 0.0
d = 12.34
我是青嵐之峰,如果讀完后感覺(jué)有所收獲,歡迎點(diǎn)贊加關(guān)注