Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SuspiciousActivity committed Mar 11, 2021
1 parent 5a5cb64 commit 71437ea
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 158 deletions.
1 change: 0 additions & 1 deletion src/me/ByteEdit/boxes/SearchBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public void replaceFind() {
public void replace() {
if (!Main.decompiler.isEditable())
return;
String txt = Main.txtByteEditView.getText();
int startPos = Main.txtByteEditView.getSelectionStart();
Main.txtByteEditView.replaceSelection(txtReplace.getText());
Main.txtByteEditView.select(startPos, startPos + txtReplace.getText().length());
Expand Down
42 changes: 3 additions & 39 deletions src/me/ByteEdit/decompiler/CFRDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.benf.cfr.reader.PluginRunner;
import org.benf.cfr.reader.api.ClassFileSource;
import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair;
import org.benf.cfr.reader.entities.ClassFile;
import org.benf.cfr.reader.entities.Method;
import org.benf.cfr.reader.entities.constantpool.ConstantPool;
import org.benf.cfr.reader.state.ClassFileSourceImpl;
import org.benf.cfr.reader.state.DCCommonState;
import org.benf.cfr.reader.util.bytestream.BaseByteData;
import org.benf.cfr.reader.util.getopt.OptionsImpl;
import org.benf.cfr.reader.util.output.ToStringDumper;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

public class CFRDecompiler implements IDecompiler {

Expand Down Expand Up @@ -83,10 +72,10 @@ public class CFRDecompiler implements IDecompiler {

@Override
public String decompile(ClassNode cn) {
return doDecompilation(cn, getBytes(cn), null);
return doDecompilation(cn, getBytes(cn));
}

private static String doDecompilation(ClassNode cn, byte[] b, MethodNode mn) {
private static String doDecompilation(ClassNode cn, byte[] b) {
try {
HashMap<String, String> ops = options;
ClassFileSource cfs = new ClassFileSource() {
Expand Down Expand Up @@ -114,24 +103,7 @@ public Collection<String> addJar(String arg0) {
throw new RuntimeException();
}
};
PluginRunner runner = new PluginRunner(ops, cfs);
if (mn != null) {
BaseByteData data = new BaseByteData(b);
ClassFile cf = new ClassFile(data, "", initDCState(ops, cfs));
Field cpf = Method.class.getDeclaredField("cp");
Field descI = Method.class.getDeclaredField("descriptorIndex");
descI.setAccessible(true);
cpf.setAccessible(true);
for (Method m : cf.getMethodByName(mn.name)) {
ConstantPool cp = (ConstantPool) cpf.get(m);
if (cp.getUTF8Entry(descI.getInt(m)).getValue().equals(mn.desc)) {
ToStringDumper tsd = new ToStringDumper();
m.dump(tsd, true);
return tsd.toString();
}
}
}
String decompilation = runner.getDecompilationFor(cn.name);
String decompilation = new PluginRunner(ops, cfs).getDecompilationFor(cn.name);
System.gc(); // cfr has a performance bug
return decompilation;
} catch (Exception e) {
Expand All @@ -142,12 +114,4 @@ public Collection<String> addJar(String arg0) {
return sw.toString();
}
}

private static DCCommonState initDCState(Map<String, String> optionsMap, ClassFileSource classFileSource) {
OptionsImpl options = new OptionsImpl(optionsMap);
if (classFileSource == null)
classFileSource = new ClassFileSourceImpl(options);
DCCommonState dcCommonState = new DCCommonState(options, classFileSource);
return dcCommonState;
}
}
5 changes: 2 additions & 3 deletions src/me/ByteEdit/decompiler/FernflowerDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.jetbrains.java.decompiler.struct.StructContext;
import org.jetbrains.java.decompiler.struct.lazy.LazyLoader;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

public class FernflowerDecompiler implements IBytecodeProvider, IResultSaver, IDecompiler {

Expand Down Expand Up @@ -53,10 +52,10 @@ public class FernflowerDecompiler implements IBytecodeProvider, IResultSaver, ID

@Override
public String decompile(ClassNode cn) {
return doDecompilation(cn, getBytes(cn), null);
return doDecompilation(cn, getBytes(cn));
}

public String doDecompilation(ClassNode cn, byte[] b, MethodNode mn) {
public String doDecompilation(ClassNode cn, byte[] b) {
try {
// TODO decompile method only
this.bytes = b;
Expand Down
8 changes: 3 additions & 5 deletions src/me/ByteEdit/decompiler/ProcyonDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.lang.reflect.Field;

import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

import com.strobel.assembler.InputTypeLoader;
import com.strobel.assembler.metadata.Buffer;
Expand All @@ -21,10 +20,10 @@ public class ProcyonDecompiler implements IDecompiler {

@Override
public String decompile(ClassNode cn) {
return doDecompilation(cn, getBytes(cn), null);
return doDecompilation(cn, getBytes(cn));
}

private static String doDecompilation(ClassNode cn, byte[] b, MethodNode mn) {
private static String doDecompilation(ClassNode cn, byte[] b) {
try {
// TODO decompile method only
DecompilerSettings settings = new DecompilerSettings();
Expand All @@ -48,9 +47,8 @@ public boolean tryLoadType(String s, Buffer buffer) {
buffer.putByteArray(b, 0, b.length);
buffer.position(0);
return true;
} else {
return backLoader.tryLoadType(s, buffer);
}
return backLoader.tryLoadType(s, buffer);
}
});
TypeReference type = metadataSystem.lookupType(cn.name);
Expand Down
64 changes: 37 additions & 27 deletions src/me/ByteEdit/edit/Assembler.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ public static ClassNode assemble(String input) {
tryCatchBlocksToParse.clear();
stage = 0;
} else if (s.startsWith("// #Signature: ")) {
if (node == null)
throw new IllegalStateException("node is null");
node.signature = UnicodeUtils.unescape(hsr, s.substring(15));
} else if (s.equals("// #TryCatch:")) {
stage = 1;
Expand All @@ -352,6 +354,8 @@ public static ClassNode assemble(String input) {
localVarsToParse.add(s);
}
} else if (s.equals("}")) {
if (node == null)
throw new IllegalStateException("node is null");
if (!annotationsForNext.isEmpty()) {
if (node.visibleAnnotations == null) {
node.visibleAnnotations = new ArrayList<>();
Expand Down Expand Up @@ -404,6 +408,8 @@ public static ClassNode assemble(String input) {
}
clazz.methods.add(node);
} else if (!s.startsWith("\t")) {
if (node == null)
throw new IllegalStateException("node is null");
s = s.substring(0, s.length() - 2);
if (s.contains(" throws ")) {
String exceptions = s.substring(s.lastIndexOf(" throws ") + 8);
Expand Down Expand Up @@ -477,6 +483,8 @@ public static ClassNode assemble(String input) {
s = s.substring(1);
if (!temp.isEmpty()) {
if (s.equals("]")) {
if (node == null)
throw new IllegalStateException("node is null");
temp += s;
node.instructions.add(getNode(hsr, temp, methodLabelMap));
temp = "";
Expand All @@ -487,6 +495,8 @@ public static ClassNode assemble(String input) {
if (s.endsWith("[")) {
temp += s + "\n";
} else {
if (node == null)
throw new IllegalStateException("node is null");
node.instructions.add(getNode(hsr, s, methodLabelMap));
}
}
Expand All @@ -508,7 +518,8 @@ public static ClassNode assemble(String input) {
return null;
} finally {
try {
read.close();
if (read != null)
read.close();
} catch (Exception e) {
}
}
Expand Down Expand Up @@ -1559,38 +1570,37 @@ public LabelNode apply(SwitchIntContainer t, LabelNode u) {
private static Object[] parseFrameList(HugeStringsRev hsr, String str, HashMap<LabelNode, Integer> labels) {
ArrayList<Object> list = new ArrayList<>();
String l = str.substring(1, str.length() - 1);
if (l.isEmpty()) {
if (l.isEmpty())
return EMPTY_OBJECT_ARRAY;
} else {
for (String asd : l.split(", ")) {
if (!asd.startsWith("(")) {
int frameType = ClassUtil.getFrameTypeByName(asd);
if (frameType != -1)
list.add(Integer.valueOf(frameType));
else
list.add(UnicodeUtils.unescape(hsr, asd.substring(1, asd.length() - 1)));
} else {
if (asd.startsWith("(label) ")) {
int labelNr = Integer.parseInt(asd.split(" ")[1]);
boolean found = false;
for (Map.Entry<LabelNode, Integer> entry : labels.entrySet()) {
if (entry.getValue() == labelNr) {
list.add(entry.getKey());
found = true;
}
}
if (!found) {
LabelNode ln = new LabelNode(new Label());
labels.put(ln, labelNr);
list.add(ln);

for (String asd : l.split(", ")) {
if (!asd.startsWith("(")) {
int frameType = ClassUtil.getFrameTypeByName(asd);
if (frameType != -1)
list.add(Integer.valueOf(frameType));
else
list.add(UnicodeUtils.unescape(hsr, asd.substring(1, asd.length() - 1)));
} else {
if (asd.startsWith("(label) ")) {
int labelNr = Integer.parseInt(asd.split(" ")[1]);
boolean found = false;
for (Map.Entry<LabelNode, Integer> entry : labels.entrySet()) {
if (entry.getValue() == labelNr) {
list.add(entry.getKey());
found = true;
}
} else {
list.add(ClassUtil.getCastedValue(asd.split(" ")[1], asd.split("\\) ")[0].substring(1)));
}
if (!found) {
LabelNode ln = new LabelNode(new Label());
labels.put(ln, labelNr);
list.add(ln);
}
} else {
list.add(ClassUtil.getCastedValue(asd.split(" ")[1], asd.split("\\) ")[0].substring(1)));
}
}
return list.toArray(new Object[list.size()]);
}
return list.toArray(new Object[list.size()]);
}

}
Loading

0 comments on commit 71437ea

Please sign in to comment.