Java安全-01反射

知識(shí)基礎(chǔ)
類
對(duì)象
構(gòu)造方法
重載
單例模式
動(dòng)態(tài)加載
JVM在執(zhí)行的時(shí)候,并不是一次性把所有的class加載到內(nèi)存中的,而是用到誰加載誰。
反射的概述
https://www.cnblogs.com/tech-bird/p/3525336.html
Reflection(反射)是Java被視為動(dòng)態(tài)語言的關(guān)鍵,反射機(jī)制允許程序在執(zhí)行期借助于Reflection API取得任何類的內(nèi)部信息,并能直接操作任意對(duì)象的內(nèi)部屬性及方法
獲取class對(duì)象的三種方式
class.forName("全類名")
類名.class
對(duì)象.getClass(),如果上下文存在某個(gè)實(shí)例對(duì)象,可以通過getClass獲取他的類
public class re {public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {Class cls1 = Class.forName("demo.Person");Class cls2 = Person.class;Person person = new Person();Class cls3 = person.getClass();System.out.println(cls1);System.out.println(cls2);System.out.println(cls3);System.out.println(cls1==cls2);System.out.println(cls3==cls2);}

類“初始化”執(zhí)行順序是什么
package demo;public class test {public static void main(String[] args) {Ref ref = new Ref();}}class Ref{static {System.out.println("最先執(zhí)行\(zhòng)r\n");}{System.out.println("第二執(zhí)行\(zhòng)r\n");}public Ref(){System.out.println("最后執(zhí)行\(zhòng)r\n");}}

demo1
package demo;public class test {public static void main(String[] args) throws ClassNotFoundException {Class.forName("demo.CalcDemo");}}class CalcDemo {static {try {Runtime rt = Runtime.getRuntime();Process pc = rt.exec("calc");pc.waitFor();} catch (Exception e) {}}}


Class方法
獲取變量
Field getField(name):根據(jù)字段名獲取某個(gè)public的field(包括父類)
Field getDeclaredField(name):根據(jù)字段名獲取當(dāng)前類的某個(gè)field(不包括父類)
Field[] getFields():獲取所有public的field(包括父類)
Field[] getDeclaredFields():獲取當(dāng)前類的所有field(不包括父類)
cls3.getField("aaa");//指定名稱的public修飾的cls3.getFields();//獲取所有public修飾的成員變量cls3.getDeclaredField("aaaa");//獲取所有cls3.getDeclaredFields();
獲取構(gòu)造方法
cls3.getConstructors();cls3.getConstructor("aaa");cls3.getDeclaredConstructor("bbb");cls3.getDeclaredConstructors();
package demo;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;public class test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {// Ref ref = new Ref();Class cls = Class.forName("demo.Person");Constructor constructor = cls.getConstructor();Constructor constructor1 = cls.getConstructor(String.class);constructor.newInstance();constructor1.newInstance("jl");}}

demo2
package demo;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Arrays;import java.util.List;public class test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {Class cls = Class.forName("java.lang.ProcessBuilder");Method methodStart = cls.getMethod("start");Constructor constructor = cls.getConstructor(List.class);Object obj = constructor.newInstance(Arrays.asList("calc.exe"));methodStart.invoke(obj);}}

demo3-可變長參數(shù)
package demo;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Arrays;import java.util.List;public class test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {Class cls = Class.forName("java.lang.ProcessBuilder");Method methodStart = cls.getMethod("start");Constructor constructor = cls.getConstructor(String[].class);Object obj = constructor.newInstance(new String[][]{{"calc.exe"}});methodStart.invoke(obj);}}

獲取成員方法
cls3.getConstructors();cls3.getConstructor("aaa");cls3.getDeclaredConstructor("bbb");cls3.getDeclaredConstructors();
demo4
package demo;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;public class test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {// Ref ref = new Ref();Class cls = Class.forName("demo.Person");Constructor constructor = cls.getConstructor();Constructor constructor1 = cls.getConstructor(String.class);constructor.newInstance();constructor1.newInstance("jl");}}


demo5
package demo;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {Class cls = Class.forName("java.lang.Runtime");Method method = cls.getMethod("exec", String.class);Method method1 = cls.getMethod("getRuntime");method.invoke(method1.invoke(cls),"calc.exe");}}
Runtime類就是單例模式,我們只能通過 Runtime.getRuntime() 來獲取到 Runtime 對(duì) 象。

往期回顧
小程序測(cè)試流
ModSecurity?搭建web安全防火墻和流量檢測(cè)
評(píng)論
圖片
表情
