Skip to content

Commit 0d221db

Browse files
Add ASMifier support
This adds support (both GUI and CLI) for generating Java code from ASMifier to generate the bytecode of a class with ASM
1 parent 56b3a72 commit 0d221db

File tree

8 files changed

+88
-26
lines changed

8 files changed

+88
-26
lines changed

src/main/java/the/bytecode/club/bytecodeviewer/CommandLineInput.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public static int parseCommandLine(String[] args) {
100100
System.out.println("Krakatau-Bytecode");
101101
System.out.println("JD-GUI");
102102
System.out.println("Smali");
103+
System.out.println("ASMifier");
103104
return STOP;
104105
} else if (cmd.hasOption("clean")) {
105106
new File(Constants.getBCVDirectory()).delete();
@@ -163,7 +164,8 @@ public static int parseCommandLine(String[] args) {
163164
!decompiler.equalsIgnoreCase("krakatau") &&
164165
!decompiler.equalsIgnoreCase("krakatau-bytecode") &&
165166
!decompiler.equalsIgnoreCase("jd-gui") &&
166-
!decompiler.equalsIgnoreCase("smali")
167+
!decompiler.equalsIgnoreCase("smali") &&
168+
!decompiler.equalsIgnoreCase("asmifier")
167169
) {
168170
System.out.println("Error, no decompiler called '" + decompiler + "' found. Type -list"
169171
+ " for the list");
@@ -356,6 +358,26 @@ public static void executeCommandLine(String[] args) {
356358
}
357359
}
358360
}
361+
else if (decompiler.equalsIgnoreCase("asmifier")) {
362+
System.out.println("Generating ASM code for " + input.getAbsolutePath() + " with ASMifier");
363+
BytecodeViewer.openFiles(new File[]{input}, false);
364+
365+
Thread.sleep(5 * 1000);
366+
367+
if (target.equalsIgnoreCase("all")) {
368+
System.out.println("Coming soon.");
369+
//Decompiler.smali.decompileToZip(output.getAbsolutePath());
370+
} else {
371+
try {
372+
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
373+
final ClassWriter cw = accept(cn);
374+
String contents = Decompiler.ASMIFIER_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
375+
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
376+
} catch (Exception e) {
377+
BytecodeViewer.handleException(e);
378+
}
379+
}
380+
}
359381

360382
System.out.println("Finished.");
361383
System.out.println("Bytecode Viewer " + VERSION + " [CLI] - Created by @Konloch - https://bytecodeviewer.com");

src/main/java/the/bytecode/club/bytecodeviewer/decompilers/Decompiler.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
package the.bytecode.club.bytecodeviewer.decompilers;
22

3-
import the.bytecode.club.bytecodeviewer.decompilers.impl.ASMTextifierDisassembler;
4-
import the.bytecode.club.bytecodeviewer.decompilers.impl.BytecodeDisassembler;
5-
import the.bytecode.club.bytecodeviewer.decompilers.impl.CFRDecompiler;
6-
import the.bytecode.club.bytecodeviewer.decompilers.impl.FernFlowerDecompiler;
7-
import the.bytecode.club.bytecodeviewer.decompilers.impl.JADXDecompiler;
8-
import the.bytecode.club.bytecodeviewer.decompilers.impl.JDGUIDecompiler;
9-
import the.bytecode.club.bytecodeviewer.decompilers.impl.JavapDisassembler;
10-
import the.bytecode.club.bytecodeviewer.decompilers.impl.KrakatauDecompiler;
11-
import the.bytecode.club.bytecodeviewer.decompilers.impl.KrakatauDisassembler;
12-
import the.bytecode.club.bytecodeviewer.decompilers.impl.ProcyonDecompiler;
13-
import the.bytecode.club.bytecodeviewer.decompilers.impl.SmaliDisassembler;
3+
import the.bytecode.club.bytecodeviewer.decompilers.impl.*;
144

155
/***************************************************************************
166
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@@ -51,6 +41,7 @@ public enum Decompiler
5141
JD_DECOMPILER("JD-GUI Decompiler", "jdgui", new JDGUIDecompiler()),
5242
JADX_DECOMPILER("JADX Decompiler", "jadx", new JADXDecompiler()),
5343
ASM_TEXTIFY_DISASSEMBLER("ASM Disassembler", "asm", new ASMTextifierDisassembler()),
44+
ASMIFIER_DECOMPILER("ASMifier Generator", "asmifier", new ASMifierGenerator()),
5445
JAVAP_DISASSEMBLER("Javap Disassembler", "javap", new JavapDisassembler()),
5546
;
5647

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package the.bytecode.club.bytecodeviewer.decompilers.impl;
2+
3+
import org.objectweb.asm.tree.ClassNode;
4+
import org.objectweb.asm.util.ASMifier;
5+
import org.objectweb.asm.util.Textifier;
6+
import org.objectweb.asm.util.TraceClassVisitor;
7+
import the.bytecode.club.bytecodeviewer.decompilers.InternalDecompiler;
8+
9+
import java.io.PrintWriter;
10+
import java.io.StringWriter;
11+
12+
/***************************************************************************
13+
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
14+
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
15+
* *
16+
* This program is free software: you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation, either version 3 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
* This program is distributed in the hope that it will be useful, *
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
24+
* GNU General Public License for more details. *
25+
* *
26+
* You should have received a copy of the GNU General Public License *
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
28+
***************************************************************************/
29+
30+
/**
31+
* Objectweb ASMifier output
32+
*
33+
* @author Nick Botticelli
34+
*/
35+
public class ASMifierGenerator extends InternalDecompiler
36+
{
37+
@Override
38+
public String decompileClassNode(ClassNode cn, byte[] b) {
39+
StringWriter writer = new StringWriter();
40+
cn.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(writer)));
41+
return writer.toString();
42+
}
43+
44+
@Override
45+
public void decompileToZip(String sourceJar, String zipName) {
46+
}
47+
}

src/main/java/the/bytecode/club/bytecodeviewer/gui/components/DecompilerViewComponent.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJCheckBoxMenuItem;
1212
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJRadioButtonMenuItem;
1313

14-
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.BYTECODE;
15-
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.BYTECODE_NON_EDITABLE;
16-
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.JAVA;
17-
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.JAVA_AND_BYTECODE;
14+
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.*;
1815

1916
/***************************************************************************
2017
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@@ -63,12 +60,12 @@ public DecompilerViewComponent(String name, DecompilerComponentType type, Decomp
6360

6461
private void createMenu()
6562
{
66-
if(type == JAVA || type == JAVA_AND_BYTECODE)
63+
if(type == JAVA || type == JAVA_NON_EDITABLE || type == JAVA_AND_BYTECODE)
6764
menu.add(java);
6865
if(type == BYTECODE || type == JAVA_AND_BYTECODE || type == BYTECODE_NON_EDITABLE)
6966
menu.add(bytecode);
7067

71-
if(type != BYTECODE_NON_EDITABLE)
68+
if(type != JAVA_NON_EDITABLE && type != BYTECODE_NON_EDITABLE)
7269
{
7370
menu.add(new JSeparator());
7471
menu.add(editable);
@@ -79,7 +76,7 @@ private void createMenu()
7976

8077
public void addToGroup(ButtonGroup group)
8178
{
82-
if(type == JAVA || type == JAVA_AND_BYTECODE)
79+
if(type == JAVA || type == JAVA_NON_EDITABLE || type == JAVA_AND_BYTECODE)
8380
group.add(java);
8481
if(type == BYTECODE || type == JAVA_AND_BYTECODE || type == BYTECODE_NON_EDITABLE)
8582
group.add(bytecode);
@@ -118,6 +115,7 @@ public Decompiler[] getDecompilers()
118115
public enum DecompilerComponentType
119116
{
120117
JAVA,
118+
JAVA_NON_EDITABLE,
121119
BYTECODE,
122120
BYTECODE_NON_EDITABLE,
123121
JAVA_AND_BYTECODE

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/DecompilerSelectionPane.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJMenu;
1919
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJRadioButtonMenuItem;
2020

21-
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.BYTECODE;
22-
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.BYTECODE_NON_EDITABLE;
23-
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.JAVA;
24-
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.JAVA_AND_BYTECODE;
21+
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentType.*;
2522

2623
/***************************************************************************
2724
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@@ -61,12 +58,13 @@ public class DecompilerSelectionPane
6158
private final DecompilerViewComponent smali = new DecompilerViewComponent("Smali", BYTECODE, Decompiler.SMALI_DISASSEMBLER);
6259
private final DecompilerViewComponent bytecode = new DecompilerViewComponent("Bytecode", BYTECODE_NON_EDITABLE, Decompiler.BYTECODE_DISASSEMBLER);
6360
private final DecompilerViewComponent asmTextify = new DecompilerViewComponent("ASM Textify", BYTECODE_NON_EDITABLE, Decompiler.ASM_TEXTIFY_DISASSEMBLER);
61+
private final DecompilerViewComponent asmifier = new DecompilerViewComponent("ASMifier", JAVA_NON_EDITABLE, Decompiler.ASMIFIER_DECOMPILER);
6462
private final DecompilerViewComponent javap = new DecompilerViewComponent("Javap", BYTECODE_NON_EDITABLE, Decompiler.JAVAP_DISASSEMBLER);
6563

6664
//TODO when adding new decompilers insert the DecompilerViewComponent object into here
6765
// also in the group, then finally the build menu
6866
public List<DecompilerViewComponent> components = new ArrayList<>(Arrays.asList(
69-
procyon, CFR, JADX, JD, fern, krakatau, smali, bytecode, asmTextify, javap));
67+
procyon, CFR, JADX, JD, fern, krakatau, smali, bytecode, asmTextify, asmifier, javap));
7068

7169
public DecompilerSelectionPane(int paneID)
7270
{
@@ -159,13 +157,16 @@ public void buildMenu()
159157
menu.add(bytecode.getMenu());
160158
menu.add(javap.getMenu());
161159
menu.add(asmTextify.getMenu());
160+
menu.add(asmifier.getMenu());
162161
menu.add(new JSeparator());
163162
menu.add(hexcode);
164163
}
165164

166165
public Decompiler getSelectedDecompiler()
167166
{
168-
return Decompiler.valueOf(group.getSelection().getActionCommand());
167+
javax.swing.ButtonModel selection = group.getSelection();
168+
String actionCommand = selection.getActionCommand();
169+
return Decompiler.valueOf(actionCommand);
169170
}
170171

171172
public void setSelectedDecompiler(Decompiler decompiler)

src/main/java/the/bytecode/club/bytecodeviewer/translation/TranslatedComponents.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public enum TranslatedComponents
9494
BYTECODE,
9595
HEXCODE,
9696
ASM_TEXTIFY,
97-
97+
ASMIFIER,
98+
9899
SETTINGS,
99100
COMPILE_ON_SAVE,
100101
COMPILE_ON_REFRESH,

src/main/java/the/bytecode/club/bytecodeviewer/translation/TranslatedStrings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public enum TranslatedStrings
5454
HEXCODE,
5555
BYTECODE,
5656
ASM_TEXTIFY,
57+
ASMIFIER,
5758
ERROR,
5859
DISASSEMBLER,
5960
RESULTS,

src/main/resources/translations/english.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"HEXCODE": "Hexcode",
9797
"BYTECODE": "Bytecode",
9898
"ASM_TEXTIFY": "ASM Textify",
99+
"ASMIFIER": "ASMifier",
99100

100101
"BYTECODE_DECOMPILER": "Bytecode Decompiler",
101102
"DEBUG_HELPERS": "Debug Helpers",

0 commit comments

Comments
 (0)