Java反射
非常好的一篇文章,跟着下来理解了不少。(至少手不是那么生了)
感觉重要的是知道包名和类名然后forname获得这个类,然后想怎么调用就怎么调用了。
方法名,变量名,构造函数名,然后赋值,调用函数都可以。
甚至当自己没有写get和set函数的时候,也可以给私有函数赋值。简直是除了函数的具体内容其他的都知道了。
下面是给私有成员赋值
package com.company; import javax.xml.transform.Source; import java.awt.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.List; public class Main { public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException { Class mo=Class.forName("com.company.moting"); /* Constructor moConstructor = mo.getConstructor(); System.out.println(moConstructor); System.out.println("================================="); moConstructor=mo.getDeclaredConstructor(); System.out.println(moConstructor); System.out.println("================================="); moConstructor=mo.getConstructor(); System.out.println(moConstructor); System.out.println("================================="); Object o = moConstructor.newInstance(); System.out.println("================================="); System.out.println(o.getClass()); moConstructor=mo.getDeclaredConstructor(int.class); System.out.println(moConstructor); moConstructor.setAccessible(true); o=moConstructor.newInstance(11);*/ Field[] moFields = mo.getDeclaredFields(); for (Field field : moFields) { System.out.println(field); } Field field = mo.getField("name"); Object obj=mo.getConstructor().newInstance(); moting moting=(moting) obj; System.out.println("1===================="); field=mo.getDeclaredField("phoneNum"); System.out.println(field); field.setAccessible(true); System.out.println(moting.toString()); field.set(obj,"芜湖起飞"); System.out.println(moting.toString()); /* mo.getMethods(); Method[] methods = mo.getDeclaredMethods(); for (Method method : methods) { System.out.println(method); } System.out.println("================="); Method show1 = mo.getMethod("show1", String.class); Object o=mo.getConstructor().newInstance(); show1.invoke(o,"zhssss"); show1=mo.getDeclaredMethod("show4", int.class); show1.setAccessible(true); Object o1 = show1.invoke(o, 111); System.out.println("================="); System.out.println(o1);*/ } } class moting{ public static void print1(){ System.out.println("3483608762"); } public String name; protected int age; char sex; private String phoneNum; public void show1(String s){ System.out.println("调用了:公有的,String参数的show1(): s = " + s); } protected void show2(){ System.out.println("调用了:受保护的,无参的show2()"); } void show3(){ System.out.println("调用了:默认的,无参的show3()"); } private String show4(int age){ System.out.println("调用了,私有的,并且有返回值的,int参数的show4(): age = " + age); return "abcd"; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", phoneNum=" + phoneNum + "]"; } moting(String str){ System.out.println("默认的构造方法s="+str); } public moting(){ System.out.println("调用公有,无参构造方法执行了"); } public moting(String name,int age){ System.out.println("name"+name+age); } protected moting(boolean n){ System.out.println("受保护的构造方法"+n); } private moting(int age){ System.out.println("xxxx"+age); } }
为以后hook打基础。