反射操作对象以及爆破

Cat示范类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.zss.reflact;


/**
* @author zss
*/
public class Cat {
public String name="asac";
private int he=10;
public int age=0;
int tall=15;
public void cry(String name){
System.out.println("HI"+name);
}
public void hello(){
System.out.println("NIAO");
}
@Override
public String toString() {
String sex = "娜娜";
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}

public Cat(String name, int age, int tall) {
this.name = name;
this.age = age;
this.tall = tall;
}
private Cat(String name){
this.name=name;
}

public Cat() {
}
}

反射创建实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.zss.reflact;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class<?> cla= Class.forName("com.zss.reflact.Cat");
//通过无参构造器
Object o=cla.newInstance();
System.out.println(o);
//有参构造器
Constructor<?> constructor=cla.getConstructor(String.class,int.class,int.class);
constructor.newInstance("java",12,56);
System.out.println(constructor);

//通过私有构造器
Constructor<?> constructor1=cla.getDeclaredConstructor(String.class);
//进行爆破,造成私有可以调用,使用反射可以访问私有的构造器
constructor1.setAccessible(true);
constructor1.newInstance("HAHA");
System.out.println(constructor1);


}
}

image-20220321214920927

反射访问类的成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.zss.reflact;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class<?> cla= Class.forName("com.zss.reflact.Cat");
Object o=cla.newInstance();
Field name=cla.getField("name");
//如果name为静态static,则下面的o也可以是null,静态也是对象的
name.set(o,"lili");
//通过反射得到真实内容
System.out.println(name.get(o));


Field he=cla.getDeclaredField("he");
//爆破
he.setAccessible(true);
System.out.println(he.get(o));




}
}

image-20220321220059624

反射访问方法

当然私有方法也可以爆破调用,其余用法与成员相同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.zss.reflact;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author zss
*/
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class<?> cla= Class.forName("com.zss.reflact.Cat");
Object o=cla.newInstance();
Method method=cla.getMethod("cry",String.class);
method.invoke(o,"你好啊啊");

}
}

image-20220321220535697