通过暴力反射得到私有成员

    技术2022-05-20  62

    先上Demo

     

    public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { // TODO Auto-generated method stub Test test = new Test(4,5); Field field1 = test.getClass().getDeclaredField("x"); field1.setAccessible(true); System.out.println(field1.get(test)); }

     

    Test类

    */ public class Test { private int x; public int y; public Test(int x, int y) { super(); this.x = x; this.y = y; } }

     

    先获得Test类的字节码getClass(),然后再获得类的声明变量x,将其属性的访问性设为TRUE,变暴力反射出私有成员。

     

     


    最新回复(0)