通过反射调用private方法

来源:互联网 发布:idea修改js不重启 编辑:程序博客网 时间:2024/06/09 21:29
package com.supermap.testdemo;import android.util.Log;/** * Created by Administrator on 2017/6/12 0012. */public class Test {    private void log() {        Log.w("bruce", "hello world");    }    private void log(String content) {        Log.w("bruce", content);    }}

try {                    Method method = Test.class.getDeclaredMethod("log");                    method.setAccessible(true);                    method.invoke(new Test());                } catch (NoSuchMethodException e) {                    e.printStackTrace();                } catch (InvocationTargetException e) {                    e.printStackTrace();                } catch (IllegalAccessException e) {                    e.printStackTrace();                }
  Test test = new Test();                try {                    Method method = test.getClass().getDeclaredMethod("log", String.class);                    method.setAccessible(true);                    method.invoke(test, "hello superMap");                } catch (NoSuchMethodException e) {                    e.printStackTrace();                } catch (InvocationTargetException e) {                    e.printStackTrace();                } catch (IllegalAccessException e) {                    e.printStackTrace();                }


原创粉丝点击