Skip to content

Commit

Permalink
fix: additionally show smali code of inner classes (PR #824)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpstotz authored and skylot committed Jan 5, 2020
1 parent 0221380 commit c97e504
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
16 changes: 15 additions & 1 deletion jadx-core/src/main/java/jadx/core/dex/nodes/ClassNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jadx.core.dex.nodes;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -533,11 +534,24 @@ public String getPackage() {

public String getSmali() {
if (smali == null) {
smali = SmaliUtils.getSmaliCode(dex, clsDefOffset);
StringWriter stringWriter = new StringWriter(4096);
getSmali(this, stringWriter);
stringWriter.append(System.lineSeparator());
for (ClassNode innerClass : innerClasses) {
getSmali(innerClass, stringWriter);
stringWriter.append(System.lineSeparator());
}
smali = stringWriter.toString();
}
return smali;
}

protected static boolean getSmali(ClassNode classNode, StringWriter stringWriter) {
stringWriter.append(String.format("###### Class %s (%s)", classNode.getFullName(), classNode.getRawName()));
stringWriter.append(System.lineSeparator());
return SmaliUtils.getSmaliCode(classNode.dex, classNode.clsDefOffset, stringWriter);
}

public ProcessState getState() {
return state;
}
Expand Down
20 changes: 10 additions & 10 deletions jadx-core/src/main/java/jadx/core/utils/SmaliUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.StringWriter;
import java.nio.file.Path;

import org.jetbrains.annotations.NotNull;
import org.jf.baksmali.Adaptors.ClassDefinition;
import org.jf.baksmali.BaksmaliOptions;
import org.jf.dexlib2.DexFileFactory;
Expand Down Expand Up @@ -34,24 +33,25 @@ public static void assembleDex(String outputDexFile, String inputSmali) {
}
}

@NotNull
public static String getSmaliCode(DexNode dex, int clsDefOffset) {
public static boolean getSmaliCode(DexNode dex, int clsDefOffset, StringWriter stringWriter) {
try {
Path path = dex.getDexFile().getPath();
DexBackedDexFile dexFile = DexFileFactory.loadDexFile(path.toFile(), null);
DexBackedClassDef dexBackedClassDef = new DexBackedClassDef(dexFile, clsDefOffset);
return getSmaliCode(dexBackedClassDef);
getSmaliCode(dexBackedClassDef, stringWriter);
return true;
} catch (Exception e) {
LOG.error("Error generating smali", e);
return "Error generating smali code: " + e.getMessage()
+ '\n' + Utils.getStackTrace(e);
stringWriter.append("Error generating smali code: ");
stringWriter.append(e.getMessage());
stringWriter.append(System.lineSeparator());
stringWriter.append(Utils.getStackTrace(e));
return false;
}
}

private static String getSmaliCode(DexBackedClassDef classDef) throws IOException {
private static void getSmaliCode(DexBackedClassDef classDef, StringWriter stringWriter) throws IOException {
ClassDefinition classDefinition = new ClassDefinition(new BaksmaliOptions(), classDef);
StringWriter sw = new StringWriter();
classDefinition.writeTo(new IndentingWriter(sw));
return sw.toString();
classDefinition.writeTo(new IndentingWriter(stringWriter));
}
}

0 comments on commit c97e504

Please sign in to comment.