package com.kiselev.reflection.ui.bytecode.holder;

import com.kiselev.reflection.ui.bytecode.assembly.AgentAssembler;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Vadim Kiselev on 6/13/2017.
 */
public class ByteCodeHolder {

    private static Map<String, byte[]> byteCodeMap = new HashMap<>();

    public static void uploadByteCodeForClass(String className, byte[] byteCode) {
        byteCodeMap.put(className, byteCode);
    }

    public static List<String> getClassesNamesForByteCode() {
        return new ArrayList<>(byteCodeMap.keySet());
    }

    public static String getDecompilledByteCode(Class<?> clazz) {
        uploadByteCodeForClass(clazz.getName(), null);
        loadByteCode();
        byte[] byteCode = byteCodeMap.get(clazz.getName());

        String classFileName = getTypeName(clazz) + ".class";
        writeByteCodeToFile(classFileName, byteCode);

        return "Bytecode was saved to file with name " + classFileName;
    }

    private static void loadByteCode() {
        if (!AgentAssembler.isAssembled()) {
            AgentAssembler.assembly();
        }
    }

    private static String getTypeName(Class<?> clazz) {
        String typeName = clazz.getSimpleName();

        if ("".equals(typeName)) {
            typeName = clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
        }

        return typeName;
    }

    private static void writeByteCodeToFile(String fileName, byte[] byteCode) {
        if (fileName != null && byteCode != null) {
            try (FileOutputStream stream = new FileOutputStream(fileName)) {
                stream.write(byteCode);
            } catch (IOException exception) {
                throw new RuntimeException(exception);
            }
        } else {
            throw new RuntimeException("Empty file name or byte code");
        }
    }
}

@@ -195,66 +195,15 @@
 til.
-ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List
+HashMap
 ;%0Aim
@@ -533,379 +533,271 @@
 tic 
-List%3C
 String
-%3E
  get
-ClassesNamesForByteCode() %7B%0A        return new ArrayList%3C%3E(byteCodeMap.keySet());
+DecompilledByteCode(Class%3C?%3E clazz) %7B%0A        if (!AgentAssembler.isAssembled()) %7B
 %0A    
-%7D%0A%0A
     
-public static String getDecompilledByteCode(Class%3C?%3E clazz) %7B%0A        uploadByteCodeForClass(clazz.getName(), null);%0A        loadByteCode();%0A        byte%5B%5D byteCode = byteCodeMap.get(clazz.getName());%0A%0A        String classFileName = getTypeName(clazz) + %22.class%22
+    AgentAssembler.assembly();%0A        %7D%0A%0A        String classFileName = getTypeName(clazz) + %22.class%22;%0A        byte%5B%5D byteCode = byteCodeMap.get(clazz.getName())
 ;%0A  
@@ -848,17 +848,16 @@
 eCode);%0A
-%0A
         
@@ -931,150 +931,8 @@
  %7D%0A%0A
-    private static void loadByteCode() %7B%0A        if (!AgentAssembler.isAssembled()) %7B%0A            AgentAssembler.assembly();%0A        %7D%0A    %7D%0A%0A
     

Revert unused API from ByteCodeHolder