|
| 1 | +package com.github.nickid2018.asmexecutor.gen; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import org.objectweb.asm.*; |
| 5 | +import com.github.nickid2018.asmexecutor.*; |
| 6 | + |
| 7 | +import static org.objectweb.asm.Opcodes.*; |
| 8 | + |
| 9 | +public class ClassGenerator { |
| 10 | + |
| 11 | + public String details; |
| 12 | + public String cname; |
| 13 | + public ASMClass clazz; |
| 14 | + |
| 15 | + public ClassGenerator(String details) { |
| 16 | + this.details = details; |
| 17 | + } |
| 18 | + |
| 19 | + public void make() { |
| 20 | + String[] splits = details.split("\n", 2); |
| 21 | + details = splits[1]; |
| 22 | + String[] splitss = splits[0].split(" ", 3); |
| 23 | + ClassWriter writer = new ClassWriter(0); |
| 24 | + writer.visit(V1_8, Integer.parseInt(splitss[0]), |
| 25 | + cname = "_ASM_gen_" + (int) (Integer.MAX_VALUE * Math.random()), null, splitss[1], |
| 26 | + splitss[2].trim().isEmpty() ? null : splitss[2].trim().split(" ")); |
| 27 | + details = details.replaceAll("[Tt][Hh][Ii][Ss]_[Cc][Ll][Aa][Ss][Ss]", cname); |
| 28 | + String[] arrdetail = details.split("\n"); |
| 29 | + for (int index = 0; index < arrdetail.length;) { |
| 30 | + String detail = arrdetail[index]; |
| 31 | + String type = detail.split(" ")[0]; |
| 32 | + switch (type.toUpperCase()) { |
| 33 | + case "METHOD": |
| 34 | + index += genMethod(writer, arrdetail, index); |
| 35 | + break; |
| 36 | + case "FIELD": |
| 37 | + index += genField(writer, arrdetail, index); |
| 38 | + break; |
| 39 | + case "DEFAULT_INIT": |
| 40 | + index += genDefaultInit(writer); |
| 41 | + break; |
| 42 | + default: |
| 43 | + throw new IllegalArgumentException("Uncompile Tag"); |
| 44 | + } |
| 45 | + } |
| 46 | + writer.visitEnd(); |
| 47 | + clazz = ASMClass.newClass(writer.toByteArray()); |
| 48 | + } |
| 49 | + |
| 50 | + public Class<?> getGeneratedClass() { |
| 51 | + return clazz.clazz; |
| 52 | + } |
| 53 | + |
| 54 | + public static int genMethod(ClassWriter writer, String[] details, int offset) { |
| 55 | + int counter = 1; |
| 56 | + String description = details[offset]; |
| 57 | + String[] aftSplit = description.split(" "); |
| 58 | + MethodVisitor mwriter = writer.visitMethod(Integer.parseInt(aftSplit[1]), aftSplit[2], aftSplit[3].trim(), null, |
| 59 | + null); |
| 60 | + Map<Integer, Label> labels = new HashMap<>(); |
| 61 | + for (int i = offset;;) { |
| 62 | + String now = details[++i]; |
| 63 | + counter++; |
| 64 | + if (!MethodGenerator.genNextLine(mwriter, now, labels)) |
| 65 | + break; |
| 66 | + } |
| 67 | + return counter; |
| 68 | + } |
| 69 | + |
| 70 | + public static int genField(ClassWriter writer, String[] details, int offset) { |
| 71 | + String[] aftSplit = details[offset].split(" ", 5); |
| 72 | + FieldVisitor fwriter; |
| 73 | + if (aftSplit.length == 4) { |
| 74 | + fwriter = writer.visitField(Integer.parseInt(aftSplit[1]), aftSplit[2], aftSplit[3].trim(), null, null); |
| 75 | + } else { |
| 76 | + try { |
| 77 | + fwriter = writer.visitField(Integer.parseInt(aftSplit[1]), aftSplit[2], aftSplit[3].trim(), null, |
| 78 | + MethodGenerator.parseObjects("{" + aftSplit[4].replaceFirst(" ", ",") + "}")); |
| 79 | + } catch (Exception e) { |
| 80 | + throw new RuntimeException(e); |
| 81 | + } |
| 82 | + } |
| 83 | + fwriter.visitEnd(); |
| 84 | + return 1; |
| 85 | + } |
| 86 | + |
| 87 | + public static int genDefaultInit(ClassWriter writer) { |
| 88 | + MethodVisitor mv = writer.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); |
| 89 | + mv.visitVarInsn(ALOAD, 0); |
| 90 | + mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); |
| 91 | + mv.visitInsn(RETURN); |
| 92 | + mv.visitMaxs(1, 1); |
| 93 | + mv.visitEnd(); |
| 94 | + return 1; |
| 95 | + } |
| 96 | +} |
0 commit comments