package net.ranides.assira.asm;

import net.ranides.assira.text.IOStrings;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.util.ASMifier;
import org.objectweb.asm.util.TraceClassVisitor;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MyAsmifier {

    public static void main(String[] args2) throws IOException {
        dumpType(SimpleObservableModel.class, "SimpleObservableModelDump");
    }

    private static void dumpType(Class<?> type, String name) throws IOException {
        ASMifier asmifier = new ASMifier();

        Path path = Paths.get("assira.drafts/src/test/java/net/ranides/assira/asm/"+name+".java");

        StringWriter writer = new StringWriter();

        TraceClassVisitor traceClassVisitor = new TraceClassVisitor(null, asmifier, new PrintWriter(writer));
        new ClassReader(type.getName())
            .accept(traceClassVisitor, 0);

        String text = writer.toString().replace("package asm.", "package ");

        IOStrings.write(path, text, StandardCharsets.UTF_8);
    }

}
