<!--动态生成class文件--><dependency><groupId>asm</groupId><artifactId>asm</artifactId><version>3.3.1</version></dependency>
工具类
package com.gblfy.chapter2;import java.util.ArrayList;
import java.util.List;import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;/**https://blog.csdn.net/bolg_hero/article/details/78189621* 继承ClassLoader是为了方便调用defineClass方法,因为该方法的定义为protected* */
public class Metaspace extends ClassLoader {public static List<Class<?>> createClasses() {// 类持有List<Class<?>> classes = new ArrayList<Class<?>>();// 循环1000w次生成1000w个不同的类。for (int i = 0; i < 10000000; ++i) {ClassWriter cw = new ClassWriter(0);// 定义一个类名称为Class{i},它的访问域为public,父类为java.lang.Object,不实现任何接口cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, "Class" + i, null,"java/lang/Object", null);// 定义构造函数<init>方法MethodVisitor mw = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>","()V", null, null);// 第一个指令为加载thismw.visitVarInsn(Opcodes.ALOAD, 0);// 第二个指令为调用父类Object的构造函数mw.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object","<init>", "()V");// 第三条指令为returnmw.visitInsn(Opcodes.RETURN);mw.visitMaxs(1, 1);mw.visitEnd();Metaspace test = new Metaspace();byte[] code = cw.toByteArray();// 定义类Class<?> exampleClass = test.defineClass("Class" + i, code, 0, code.length);classes.add(exampleClass);}return classes;}
}