Skip to content

Commit 2b7a19a

Browse files
committed
BCV API Expansion
1 parent accae07 commit 2b7a19a

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

  • src/main/java/the/bytecode/club/bytecodeviewer/api

src/main/java/the/bytecode/club/bytecodeviewer/api/BCV.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
import java.util.ArrayList;
77
import java.util.Enumeration;
88
import java.util.List;
9+
import java.util.Objects;
910
import java.util.jar.JarEntry;
1011
import java.util.jar.JarFile;
1112
import javax.swing.JFrame;
13+
14+
import org.objectweb.asm.ClassWriter;
1215
import org.objectweb.asm.tree.ClassNode;
16+
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
1317
import the.bytecode.club.bytecodeviewer.compilers.Compiler;
1418
import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler;
1519
import the.bytecode.club.bytecodeviewer.decompilers.Decompiler;
@@ -176,6 +180,41 @@ public static void openFiles(File[] files, boolean recentFiles) {
176180
public static ClassNode getCurrentlyOpenedClassNode() {
177181
return the.bytecode.club.bytecodeviewer.BytecodeViewer.getCurrentlyOpenedClassNode();
178182
}
183+
184+
/**
185+
* Returns the currently opened class nodes ClassFile bytes
186+
*
187+
* @return The ClassFile bytes for the actively opened resource
188+
*/
189+
public static byte[] getCurrentlyOpenedClassNodeBytes()
190+
{
191+
final ClassNode cn = BytecodeViewer.getCurrentlyOpenedClassNode();
192+
final ClassWriter cw = new ClassWriter(0);
193+
194+
try {
195+
Objects.requireNonNull(cn).accept(cw);
196+
} catch (Exception e) {
197+
e.printStackTrace();
198+
try {
199+
Thread.sleep(200);
200+
Objects.requireNonNull(cn).accept(cw);
201+
} catch (InterruptedException ignored) {
202+
}
203+
}
204+
205+
return cw.toByteArray();
206+
}
207+
208+
/**
209+
* This decompiles the actively opened ClassFile inside of BCV.
210+
*
211+
* @param decompiler The decompiler you would like to use
212+
* @return The Ascii/text representation of the class node from the decompiler provided
213+
*/
214+
public static String decompileCurrentlyOpenedClassNode(Decompiler decompiler)
215+
{
216+
return decompiler.getDecompiler().decompileClassNode(BCV.getCurrentlyOpenedClassNode(), BCV.getCurrentlyOpenedClassNodeBytes());
217+
}
179218

180219
/**
181220
* Used to load a ClassNode.

0 commit comments

Comments
 (0)