通过反射调用private方法

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();
                }


你可能感兴趣的:(通过反射调用private方法)