Calling Private methods
Package Com.java.test;public class Privatemethod{private string SayHello (string name) {return "Hello" +name;}}
Test:
Package Com.java.test;import Java.lang.reflect.method;public class Privatemethodtest{public static void Main (String[] args) throws Exception{privatemethod p = new Privatemethod (); class<?> ClassType = P.getclass (); method = Classtype.getdeclaredmethod ("SayHello", New Class[]{string.class}); method.setaccessible (true);// Check mechanism for suppressing Java String str = (string) Method.invoke (p, new object[]{"Lzyer"}); System.out.println (str);}}
To modify a private property:
Package Com.java.test;public class Privatefield{private string name= "Zhangsan";p ublic string GetName () {return THIS.name;}}
Test:
Package Com.java.test;import Java.lang.reflect.field;public class Privatefieldtest{public static void Main (string[] args) throws Exception{privatefield p = new Privatefield (); Class<?> clazz = P.getclass (); Field field = Clazz.getdeclaredfield ("name"); Field.setaccessible (true);//suppress Java Check mechanism field.set (p, "Lisi"); System.out.println (P.getname ());}}
Java reflection calls private methods and modifies private properties