0

【笔记】Java手动向内存中加载类

 1 year ago
source link: https://loli.fj.cn/2023/01/26/Java%E6%89%8B%E5%8A%A8%E5%90%91%E5%86%85%E5%AD%98%E4%B8%AD%E5%8A%A0%E8%BD%BD%E7%B1%BB/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【笔记】Java手动向内存中加载类

2023-01-26

Java手动向内存中加载类

手动向内存中加载类

Opcodes.V1_8:JDK版本
Opcodes.ACC_PUBLIC:作用域
"Class1":类名
"com":包名
java/lang/Object:父类
null:接口

public class Demo extends ClassLoader {
public static void main(String[] args) {
// 定义一个类
ClassWriter classWriter = new ClassWriter(0);
byte[] code = classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Class1", "com", "java/lang/Object", null).toByteArray();
// 加载一个类
test.defineClass("Class1", code, 0, code.length);
}
}

哔哩哔哩——黑马程序员


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK