反射就是加载类,然后获取类的属性、方法和构造函数等。
为了方便我用自己创建的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()
//以下为方法介绍
getConstructor
public Constructor<T> getConstructor(Class<?>... parameterTypes)throws NoSuchMethodException,SecurityExceptiongetDeclaredConstructor
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)throws NoSuchMethodException,SecurityException
public void demoConstructorTest () throws Exception {Class demoClass = Class.forName("com.demo.Demo");//无参构造方法Constructor demoNullConstructor = Constructor(null);Demo demoNullDemo = (Demo) wInstance(null);//输出//This is Demo's no parameter constructors method//有参构造方法Constructor demoStringConstructor = Constructor(String.class);Demo demoStringDemo = (Demo) wInstance("demoString");//输出//This is Demo's String constructors method//通过getDeclaredConstructor()进行private构造方法Constructor demoPrivateConstructor = DeclaredConstructor(String.class, int.class);demoPrivateConstructor.setAccessible(true);Demo demoPrivateDemo = (Demo) wInstance("demo1", 1);//输出//This is Demo's String and int constructors method//特殊方法创建对象Demo demoSpecialDemo = (Demo) wInstance();//使用此方法时必须保证有无参构造方法//输出//This is Demo's no parameter constructors method}
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) wInstance();//无参run方法Method demoRunMethod = Method("run", null);demoRunMethod.invoke(objectSpecialDemo, null);//输出//run//有参run方法Method demoRunFastMethod = Method("run", String.class);demoRunFastMethod.invoke(objectSpecialDemo, "fast");//输出//runfast//Private方法Method demoPrivateTimeMethod = DeclaredMethod("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 = Method("main", String[].class);//以下两种方法皆可demoMainMethod.invoke(objectSpecialDemo, (Object)new String[]{"1", "2"});demoMainMethod.invoke(objectSpecialDemo, new Object[]{new String[]{"1", "2"}});//输出//main}
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 flect.Constructor;
import flect.Method;
import flect.Field;
SimpleDateFormat;public class Reflex {@Testpublic 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;}@Testpublic void demoConstructorTest () throws Exception {Class demoClass = Class.forName("com.demo.Demo");//无参构造方法Constructor demoNullConstructor = Constructor(null);Demo objectNullDemo = (Demo) wInstance(null);//输出//This is Demo's no parameter constructors method//有参构造方法Constructor demoStringConstructor = Constructor(String.class);Demo objectStringDemo = (Demo) wInstance("demoString");//输出//This is Demo's String constructors method//通过getDeclaredConstructor()进行private构造方法Constructor demoPrivateConstructor = DeclaredConstructor(String.class, int.class);demoPrivateConstructor.setAccessible(true);Demo objectPrivateDemo = (Demo) wInstance("demo1", 1);//输出//This is Demo's String and int constructors method//特殊方法创建对象Demo objectSpecialDemo = (Demo) wInstance();//使用此方法时必须保证有无参构造方法//输出//This is Demo's no parameter constructors method}@Testpublic void demoMethodTest () throws Exception {Class demoClass = new Demo().getClass();Demo objectSpecialDemo = (Demo) wInstance();//无参run方法Method demoRunMethod = Method("run", null);demoRunMethod.invoke(objectSpecialDemo, null);//输出//run//有参run方法Method demoRunFastMethod = Method("run", String.class);demoRunFastMethod.invoke(objectSpecialDemo, "fast");//输出//runfast//Private方法Method demoPrivateTimeMethod = DeclaredMethod("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 = Method("main", String[].class);//以下两种方法皆可demoMainMethod.invoke(objectSpecialDemo, (Object)new String[]{"1", "2"});demoMainMethod.invoke(objectSpecialDemo, new Object[]{new String[]{"1", "2"}});//输出//main}@Testpublic void demoFieldTest () throws Exception {Class demoClass = Demo.class;Demo objectSpecialDemo = (Demo) wInstance();//time成员变量Field demoTimeField = Field("time");long timeLong = (long) (objectSpecialDemo);System.out.println(timeLong);//输出//0//publicString变量Field demoPublicStringField = Field("publicString");String publicString = (String) (objectSpecialDemo);System.out.println(publicString);//输出//null//设置变量的值demoPublicStringField.set(objectSpecialDemo,"publicString");//注:可以用下函数获取变量的类型Class demoTypeClass = Type();//再通过以下函数判断if (demoTypeClass.equals(String.class)) {publicString = (String) (objectSpecialDemo);System.out.println(publicString);}//private变量Field demoPrivateStringField = DeclaredField("by");demoPrivateStringField.setAccessible(true);String privateString = (String) (objectSpecialDemo);System.out.println(privateString);//输出//Hermit_Yoshino}
}
源文件已上传csdn,当然也上传了gitee
需要的可以访问
或
本文发布于:2024-02-05 02:50:26,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170722280662332.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |