forked from 1diot9/MyJavaSecStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsafeTools.java
More file actions
29 lines (24 loc) · 1.23 KB
/
Copy pathUnsafeTools.java
File metadata and controls
29 lines (24 loc) · 1.23 KB
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.rctf.solution.tools;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class UnsafeTools {
// 绕过构造方法获取对象
public static Object getObjectByUnsafe(Class clazz) throws Exception{
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
return unsafe.allocateInstance(clazz);
}
// --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.xml/com.sun.org.apache.bcel.internal.classfile=ALL-UNNAMED --add-opens=java.xml/com.sun.org.apache.xalan.internal.xsltc.trax=ALL-UNNAMED
// public static void patchModule(Class current, Class target) throws Exception {
// Field field = Unsafe.class.getDeclaredField("theUnsafe");
// field.setAccessible(true);
// Unsafe unsafe = (Unsafe) field.get(null);
//
// // 所有Class的数据结构都是一样的,相同字段的偏移量也是一样的
// long l = unsafe.objectFieldOffset(Class.class.getDeclaredField("module"));
// Module targetModule = target.getModule();
// unsafe.putObject(current, l, targetModule);
// }
}