Skip to content

Commit

Permalink
feat: add options to JadxArgs to change code new line and indent (#1945
Browse files Browse the repository at this point in the history
…, #1948)
  • Loading branch information
skylot committed Apr 8, 2024
1 parent 41d6b00 commit 6e8affc
Show file tree
Hide file tree
Showing 36 changed files with 194 additions and 172 deletions.
2 changes: 0 additions & 2 deletions jadx-core/src/main/java/jadx/api/ICodeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import jadx.api.metadata.ICodeNodeRef;

public interface ICodeWriter {
String NL = System.getProperty("line.separator");
String INDENT_STR = " ";

boolean isMetadataSupported();

Expand Down
23 changes: 23 additions & 0 deletions jadx-core/src/main/java/jadx/api/JadxArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class JadxArgs implements Closeable {

public static final int DEFAULT_THREADS_COUNT = Math.max(1, Runtime.getRuntime().availableProcessors() / 2);

public static final String DEFAULT_NEW_LINE_STR = System.lineSeparator();
public static final String DEFAULT_INDENT_STR = " ";

public static final String DEFAULT_OUT_DIR = "jadx-output";
public static final String DEFAULT_SRC_DIR = "sources";
public static final String DEFAULT_RES_DIR = "resources";
Expand Down Expand Up @@ -144,6 +147,10 @@ public enum OutputFormatEnum {

private ICodeData codeData;

private String codeNewLineStr = DEFAULT_NEW_LINE_STR;

private String codeIndentStr = DEFAULT_INDENT_STR;

private CommentsLevel commentsLevel = CommentsLevel.INFO;

private IntegerFormat integerFormat = IntegerFormat.AUTO;
Expand Down Expand Up @@ -622,6 +629,22 @@ public void setCodeData(ICodeData codeData) {
this.codeData = codeData;
}

public String getCodeIndentStr() {
return codeIndentStr;
}

public void setCodeIndentStr(String codeIndentStr) {
this.codeIndentStr = codeIndentStr;
}

public String getCodeNewLineStr() {
return codeNewLineStr;
}

public void setCodeNewLineStr(String codeNewLineStr) {
this.codeNewLineStr = codeNewLineStr;
}

public CommentsLevel getCommentsLevel() {
return commentsLevel;
}
Expand Down
11 changes: 4 additions & 7 deletions jadx-core/src/main/java/jadx/api/impl/AnnotatedCodeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public class AnnotatedCodeWriter extends SimpleCodeWriter implements ICodeWriter
private Map<Integer, ICodeAnnotation> annotations = Collections.emptyMap();
private Map<Integer, Integer> lineMap = Collections.emptyMap();

public AnnotatedCodeWriter() {
}

public AnnotatedCodeWriter(JadxArgs args) {
super(args);
}
Expand All @@ -35,9 +32,9 @@ public boolean isMetadataSupported() {

@Override
public AnnotatedCodeWriter addMultiLine(String str) {
if (str.contains(NL)) {
buf.append(str.replace(NL, NL + indentStr));
line += StringUtils.countMatches(str, NL);
if (str.contains(newLineStr)) {
buf.append(str.replace(newLineStr, newLineStr + indentStr));
line += StringUtils.countMatches(str, newLineStr);
offset = 0;
} else {
buf.append(str);
Expand Down Expand Up @@ -84,7 +81,7 @@ public ICodeWriter add(ICodeWriter cw) {

@Override
protected void addLine() {
buf.append(NL);
buf.append(newLineStr);
line++;
offset = 0;
}
Expand Down
58 changes: 27 additions & 31 deletions jadx-core/src/main/java/jadx/api/impl/SimpleCodeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,39 @@
import jadx.core.utils.Utils;

/**
* CodeWriter implementation without meta information support (only strings builder)
* CodeWriter implementation without meta information support
*/
public class SimpleCodeWriter implements ICodeWriter {
private static final Logger LOG = LoggerFactory.getLogger(SimpleCodeWriter.class);

private static final String[] INDENT_CACHE = {
"",
INDENT_STR,
INDENT_STR + INDENT_STR,
INDENT_STR + INDENT_STR + INDENT_STR,
INDENT_STR + INDENT_STR + INDENT_STR + INDENT_STR,
INDENT_STR + INDENT_STR + INDENT_STR + INDENT_STR + INDENT_STR,
};

protected StringBuilder buf = new StringBuilder();
protected String indentStr = "";
protected int indent = 0;

private final boolean insertLineNumbers;

public SimpleCodeWriter() {
this.insertLineNumbers = false;
}
protected final boolean insertLineNumbers;
protected final String singleIndentStr;
protected final String newLineStr;

public SimpleCodeWriter(JadxArgs args) {
this.insertLineNumbers = args.isInsertDebugLines();
this.singleIndentStr = args.getCodeIndentStr();
this.newLineStr = args.getCodeNewLineStr();
if (insertLineNumbers) {
incIndent(3);
add(indentStr);
}
}

/**
* Constructor with JadxArgs should be used.
*/
@Deprecated
public SimpleCodeWriter() {
this.insertLineNumbers = false;
this.singleIndentStr = JadxArgs.DEFAULT_INDENT_STR;
this.newLineStr = JadxArgs.DEFAULT_INDENT_STR;
}

@Override
public boolean isMetadataSupported() {
return false;
Expand Down Expand Up @@ -96,8 +97,8 @@ public SimpleCodeWriter startLineWithNum(int sourceLine) {

@Override
public SimpleCodeWriter addMultiLine(String str) {
if (str.contains(NL)) {
buf.append(str.replace(NL, NL + indentStr));
if (str.contains(newLineStr)) {
buf.append(str.replace(newLineStr, newLineStr + indentStr));
} else {
buf.append(str);
}
Expand Down Expand Up @@ -130,12 +131,12 @@ public SimpleCodeWriter newLine() {

@Override
public SimpleCodeWriter addIndent() {
add(INDENT_STR);
add(singleIndentStr);
return this;
}

protected void addLine() {
buf.append(NL);
buf.append(newLineStr);
}

protected SimpleCodeWriter addLineIndent() {
Expand All @@ -144,12 +145,7 @@ protected SimpleCodeWriter addLineIndent() {
}

private void updateIndent() {
int curIndent = indent;
if (curIndent < INDENT_CACHE.length) {
this.indentStr = INDENT_CACHE[curIndent];
} else {
this.indentStr = Utils.strRepeat(INDENT_STR, curIndent);
}
this.indentStr = Utils.strRepeat(singleIndentStr, indent);
}

@Override
Expand Down Expand Up @@ -219,17 +215,17 @@ public void attachSourceLine(int sourceLine) {

@Override
public ICodeInfo finish() {
removeFirstEmptyLine();
String code = buf.toString();
String code = getStringWithoutFirstEmptyLine();
buf = null;
return new SimpleCodeInfo(code);
}

protected void removeFirstEmptyLine() {
int len = NL.length();
if (buf.length() > len && buf.substring(0, len).equals(NL)) {
buf.delete(0, len);
private String getStringWithoutFirstEmptyLine() {
int len = newLineStr.length();
if (buf.length() > len && buf.substring(0, len).equals(newLineStr)) {
return buf.substring(len);
}
return buf.toString();
}

@Override
Expand Down
28 changes: 20 additions & 8 deletions jadx-core/src/main/java/jadx/api/utils/CodeUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package jadx.api.utils;

import jadx.api.ICodeWriter;

public class CodeUtils {

public static String getLineForPos(String code, int pos) {
Expand All @@ -11,18 +9,32 @@ public static String getLineForPos(String code, int pos) {
}

public static int getLineStartForPos(String code, int pos) {
String newLine = ICodeWriter.NL;
int start = code.lastIndexOf(newLine, pos);
return start == -1 ? 0 : start + newLine.length();
int start = getNewLinePosBefore(code, pos);
return start == -1 ? 0 : start + 1;
}

public static int getLineEndForPos(String code, int pos) {
int end = code.indexOf(ICodeWriter.NL, pos);
int end = getNewLinePosAfter(code, pos);
return end == -1 ? code.length() : end;
}

public static int getLineNumForPos(String code, int pos) {
String newLine = ICodeWriter.NL;
public static int getNewLinePosAfter(String code, int startPos) {
int pos = code.indexOf('\n', startPos);
if (pos != -1) {
// check for '\r\n'
int prev = pos - 1;
if (code.charAt(prev) == '\r') {
return prev;
}
}
return pos;
}

public static int getNewLinePosBefore(String code, int startPos) {
return code.lastIndexOf('\n', startPos);
}

public static int getLineNumForPos(String code, int pos, String newLine) {
int newLineLen = newLine.length();
int line = 1;
int prev = 0;
Expand Down
12 changes: 6 additions & 6 deletions jadx-core/src/main/java/jadx/core/codegen/json/JsonCodeGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private JsonClass processCls(ClassNode cls, @Nullable ClassGen parentCodeGen) {
jsonCls.setInterfaces(Utils.collectionMap(cls.getInterfaces(), clsType -> getTypeAlias(classGen, clsType)));
}

ICodeWriter cw = new SimpleCodeWriter();
ICodeWriter cw = new SimpleCodeWriter(args);
CodeGenUtils.addErrorsAndComments(cw, cls);
classGen.addClassDeclaration(cw);
jsonCls.setDeclaration(cw.getCodeStr());
Expand Down Expand Up @@ -130,7 +130,7 @@ private void addFields(ClassNode cls, JsonClass jsonCls, ClassGen classGen) {
jsonField.setAlias(field.getAlias());
}

ICodeWriter cw = new SimpleCodeWriter();
ICodeWriter cw = new SimpleCodeWriter(args);
classGen.addField(cw, field);
jsonField.setDeclaration(cw.getCodeStr());
jsonField.setAccessFlags(field.getAccessFlags().rawValue());
Expand All @@ -154,7 +154,7 @@ private void addMethods(ClassNode cls, JsonClass jsonCls, ClassGen classGen) {
jsonMth.setArguments(Utils.collectionMap(mth.getMethodInfo().getArgumentsTypes(), clsType -> getTypeAlias(classGen, clsType)));

MethodGen mthGen = new MethodGen(classGen, mth);
ICodeWriter cw = new AnnotatedCodeWriter();
ICodeWriter cw = new AnnotatedCodeWriter(args);
mthGen.addDefinition(cw);
jsonMth.setDeclaration(cw.getCodeStr());
jsonMth.setAccessFlags(mth.getAccessFlags().rawValue());
Expand All @@ -181,15 +181,15 @@ private List<JsonCodeLine> fillMthCode(MethodNode mth, MethodGen mthGen) {
return Collections.emptyList();
}

String[] lines = codeStr.split(ICodeWriter.NL);
String[] lines = codeStr.split(args.getCodeNewLineStr());
Map<Integer, Integer> lineMapping = code.getCodeMetadata().getLineMapping();
ICodeMetadata metadata = code.getCodeMetadata();
long mthCodeOffset = mth.getMethodCodeOffset() + 16;

int linesCount = lines.length;
List<JsonCodeLine> codeLines = new ArrayList<>(linesCount);
int lineStartPos = 0;
int newLineLen = ICodeWriter.NL.length();
int newLineLen = args.getCodeNewLineStr().length();
for (int i = 0; i < linesCount; i++) {
String codeLine = lines[i];
int line = i + 2;
Expand All @@ -208,7 +208,7 @@ private List<JsonCodeLine> fillMthCode(MethodNode mth, MethodGen mthGen) {
}

private String getTypeAlias(ClassGen classGen, ArgType clsType) {
ICodeWriter code = new SimpleCodeWriter();
ICodeWriter code = new SimpleCodeWriter(args);
classGen.useType(code, clsType);
return code.getCodeStr();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.jetbrains.annotations.NotNull;

import jadx.api.ICodeWriter;
import jadx.core.utils.Utils;

public class JadxError implements Comparable<JadxError> {
Expand Down Expand Up @@ -55,7 +54,7 @@ public String toString() {
str.append(cause.getClass());
str.append(':');
str.append(cause.getMessage());
str.append(ICodeWriter.NL);
str.append('\n');
str.append(Utils.getStackTrace(cause));
}
return str.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import jadx.api.ICodeWriter;
import jadx.api.plugins.input.data.ILocalVar;
import jadx.api.plugins.input.data.attributes.IJadxAttribute;
import jadx.core.dex.attributes.AType;
Expand All @@ -26,6 +25,6 @@ public AType<LocalVarsDebugInfoAttr> getAttrType() {

@Override
public String toString() {
return "Debug Info:" + ICodeWriter.NL + " " + Utils.listToString(localVars, ICodeWriter.NL + " ");
return "Debug Info:\n " + Utils.listToString(localVars, "\n ");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jadx.core.dex.attributes.nodes;

import jadx.api.CommentsLevel;
import jadx.api.ICodeWriter;
import jadx.api.data.CommentStyle;
import jadx.core.codegen.utils.CodeComment;
import jadx.core.dex.attributes.AFlag;
Expand Down Expand Up @@ -39,7 +38,7 @@ public void addWarnComment(String warn) {
}

public void addWarnComment(String warn, Throwable exc) {
String commentStr = warn + ICodeWriter.NL + Utils.getStackTrace(exc);
String commentStr = warn + root().getArgs().getCodeNewLineStr() + Utils.getStackTrace(exc);
JadxCommentsAttr.add(this, CommentsLevel.WARN, commentStr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.ArrayList;
import java.util.List;

import jadx.api.ICodeWriter;
import jadx.api.plugins.input.data.attributes.IJadxAttribute;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.instructions.PhiInsn;
Expand Down Expand Up @@ -33,7 +32,7 @@ public String toString() {
}
}
for (PhiInsn phiInsn : list) {
sb.append(ICodeWriter.NL).append(" ").append(phiInsn);
sb.append('\n').append(" ").append(phiInsn);
}
return sb.toString();
}
Expand Down
Loading

0 comments on commit 6e8affc

Please sign in to comment.