fix: don't use OS specific new line chars (#861)

This commit is contained in:
Skylot
2020-02-23 15:37:07 +00:00
parent b56fd4d29a
commit fa0a38d3aa
13 changed files with 52 additions and 26 deletions
@@ -3,6 +3,7 @@ package jadx.core.dex.attributes;
import java.util.ArrayList;
import java.util.List;
import jadx.core.codegen.CodeWriter;
import jadx.core.utils.Utils;
public class AttrList<T> implements IAttribute {
@@ -25,6 +26,6 @@ public class AttrList<T> implements IAttribute {
@Override
public String toString() {
return Utils.listToString(list, "\n");
return Utils.listToString(list, CodeWriter.NL);
}
}
@@ -4,6 +4,7 @@ import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import jadx.core.codegen.CodeWriter;
import jadx.core.utils.Utils;
public class JadxError implements Comparable<JadxError> {
@@ -58,7 +59,7 @@ public class JadxError implements Comparable<JadxError> {
str.append(cause.getClass());
str.append(':');
str.append(cause.getMessage());
str.append('\n');
str.append(CodeWriter.NL);
str.append(Utils.getStackTrace(cause));
}
return str.toString();
@@ -7,6 +7,8 @@ import jadx.core.dex.attributes.IAttribute;
import jadx.core.dex.visitors.debuginfo.LocalVar;
import jadx.core.utils.Utils;
import static jadx.core.codegen.CodeWriter.NL;
public class LocalVarsDebugInfoAttr implements IAttribute {
private final List<LocalVar> localVars;
@@ -25,6 +27,6 @@ public class LocalVarsDebugInfoAttr implements IAttribute {
@Override
public String toString() {
return "Debug Info:\n " + Utils.listToString(localVars, "\n ");
return "Debug Info:" + NL + " " + Utils.listToString(localVars, NL + " ");
}
}
@@ -7,6 +7,8 @@ import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.IAttribute;
import jadx.core.dex.instructions.PhiInsn;
import static com.google.common.base.Ascii.NL;
public class PhiListAttr implements IAttribute {
private final List<PhiInsn> list = new LinkedList<>();
@@ -28,7 +30,7 @@ public class PhiListAttr implements IAttribute {
sb.append('r').append(phiInsn.getResult().getRegNum()).append(' ');
}
for (PhiInsn phiInsn : list) {
sb.append("\n ").append(phiInsn).append(' ').append(phiInsn.getAttributesString());
sb.append(NL).append(" ").append(phiInsn).append(' ').append(phiInsn.getAttributesString());
}
return sb.toString();
}
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable;
import com.android.dx.io.instructions.DecodedInstruction;
import jadx.core.codegen.CodeWriter;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.nodes.LineAttrNode;
import jadx.core.dex.instructions.InsnType;
@@ -408,9 +409,9 @@ public class InsnNode extends LineAttrNode {
sb.append(argsStr);
} else {
// wrap args
String separator = "\n ";
String separator = CodeWriter.NL + " ";
sb.append(separator).append(Utils.listToString(arguments, separator));
sb.append('\n');
sb.append(CodeWriter.NL);
}
}
@@ -28,6 +28,8 @@ import jadx.core.dex.visitors.typeinference.TypeCompareEnum;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.codegen.CodeWriter.NL;
@JadxVisitor(
name = "MethodInvokeVisitor",
desc = "Process additional info for method invocation (overload, vararg)",
@@ -249,13 +251,13 @@ public class MethodInvokeVisitor extends AbstractVisitor {
if (argsCount == 1) {
return mthDetails.getArgTypes();
}
// TODO: try to minimize casts count
parentMth.addComment("JADX DEBUG: Failed to find minimal casts for resolve overloaded methods, cast all args instead"
+ "\n method: " + mthDetails
+ "\n arg types: " + compilerVarTypes
+ "\n candidates:\n " + Utils.listToString(overloadedMethods, "\n "));
+ NL + " method: " + mthDetails
+ NL + " arg types: " + compilerVarTypes
+ NL + " candidates:"
+ NL + " " + Utils.listToString(overloadedMethods, NL + " "));
// not resolved -> cast all args
return mthDetails.getArgTypes();
}
@@ -20,6 +20,8 @@ import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxException;
import static jadx.core.codegen.CodeWriter.NL;
@JadxVisitor(
name = "Debug Info Parser",
desc = "Parse debug information (variable names and types, instruction lines)",
@@ -42,7 +44,7 @@ public class DebugInfoParseVisitor extends AbstractVisitor {
} catch (Exception e) {
mth.addComment("JADX WARNING: Error to parse debug info: "
+ ErrorsCounter.formatMsg(mth, e.getMessage())
+ '\n' + Utils.getStackTrace(e));
+ NL + Utils.getStackTrace(e));
}
}
@@ -21,6 +21,8 @@ import jadx.core.dex.visitors.PrepareForCodeGen;
import jadx.core.dex.visitors.RenameVisitor;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.codegen.CodeWriter.NL;
/**
* Check invariants and information consistency for registers and SSA variables
*/
@@ -86,7 +88,8 @@ public class DebugChecks {
List<RegisterArg> useList = sVar.getUseList();
boolean assignReg = insn.getResult() == reg;
if (!assignReg && !Utils.containsInListByRef(useList, reg)) {
throw new JadxRuntimeException("Incorrect use list in ssa var: " + sVar + ", register not listed.\n insn: " + insn);
throw new JadxRuntimeException("Incorrect use list in ssa var: " + sVar + ", register not listed."
+ NL + " insn: " + insn);
}
for (RegisterArg useArg : useList) {
checkRegisterArg(mth, useArg);
@@ -107,8 +110,8 @@ public class DebugChecks {
}
BlockNode parentInsnBlock = BlockUtils.getBlockByInsn(mth, parentInsn);
if (parentInsnBlock == null) {
parentInsnBlock = BlockUtils.getBlockByInsn(mth, parentInsn);
throw new JadxRuntimeException("Parent insn not found in blocks tree for: " + reg + ",\n insn: " + parentInsn);
throw new JadxRuntimeException("Parent insn not found in blocks tree for: " + reg
+ NL + " insn: " + parentInsn);
}
}
}
@@ -31,6 +31,8 @@ import jadx.core.dex.visitors.regions.TracedRegionVisitor;
import jadx.core.utils.exceptions.CodegenException;
import jadx.core.utils.exceptions.JadxException;
import static jadx.core.codegen.CodeWriter.NL;
@Deprecated
@TestOnly
public class DebugUtils {
@@ -98,7 +100,7 @@ public class DebugUtils {
CodeWriter cw = new CodeWriter();
cw.startLine('|').add(mth.toString());
printRegion(mth, region, cw, "| ", printInsns);
LOG.debug("\n{}", cw.finish().getCodeStr());
LOG.debug("{}{}", NL, cw.finish().getCodeStr());
}
private static void printRegion(MethodNode mth, IRegion region, CodeWriter cw, String indent, boolean printInsns) {
@@ -126,7 +128,7 @@ public class DebugUtils {
ig.makeInsn(insn, code);
String codeStr = code.finish().getCodeStr();
List<String> insnStrings = Arrays.stream(codeStr.split(CodeWriter.NL))
List<String> insnStrings = Arrays.stream(codeStr.split(NL))
.filter(StringUtils::notBlank)
.map(s -> "|> " + s)
.collect(Collectors.toList());
@@ -148,7 +150,7 @@ public class DebugUtils {
private static void printWithAttributes(CodeWriter cw, String indent, String codeStr, IAttributeNode attrNode) {
String str = attrNode.isAttrStorageEmpty() ? codeStr : codeStr + ' ' + attrNode.getAttributesString();
List<String> attrStrings = Arrays.stream(str.split(CodeWriter.NL))
List<String> attrStrings = Arrays.stream(str.split(NL))
.filter(StringUtils::notBlank)
.collect(Collectors.toList());
Iterator<String> it = attrStrings.iterator();
@@ -20,6 +20,8 @@ import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.codegen.CodeWriter.NL;
/**
* Helper class for correct instructions removing,
* can be used while iterating over instructions list
@@ -130,10 +132,10 @@ public class InsnRemover {
return;
}
if (Consts.DEBUG) { // TODO: enable this
throw new JadxRuntimeException("Can't remove SSA var, still in use, count: " + useCount
+ ", list:\n " + ssaVar.getUseList().stream()
throw new JadxRuntimeException("Can't remove SSA var, still in use, count: " + useCount + ", list:"
+ NL + " " + ssaVar.getUseList().stream()
.map(arg -> arg + " from " + arg.getParentInsn())
.collect(Collectors.joining("\n ")));
.collect(Collectors.joining(NL + " ")));
}
}
@@ -168,8 +170,10 @@ public class InsnRemover {
}
}
if (!found && Consts.DEBUG) { // TODO: enable this
throw new JadxRuntimeException("Can't remove insn:\n " + rem
+ "\nnot found in list:\n " + Utils.listToString(insns, "\n "));
throw new JadxRuntimeException("Can't remove insn:"
+ NL + " " + rem
+ NL + " not found in list:"
+ NL + " " + Utils.listToString(insns, NL + " "));
}
}
}
@@ -26,6 +26,7 @@ import jadx.core.utils.exceptions.DecodeException;
import jadx.core.utils.exceptions.JadxException;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.codegen.CodeWriter.NL;
import static jadx.core.utils.files.FileUtils.isApkFile;
import static jadx.core.utils.files.FileUtils.isZipDexFile;
@@ -197,7 +198,7 @@ public class InputFile {
}
return pathList;
} catch (Exception e) {
throw new DecodeException("java class to dex conversion error:\n " + e.getMessage(), e);
throw new DecodeException("java class to dex conversion error:" + NL + " " + e.getMessage(), e);
} finally {
if (j2d.isError()) {
LOG.warn("dx message: {}", j2d.getDxErrors());
@@ -17,11 +17,14 @@ import jadx.api.ResourceFileContent;
import jadx.api.ResourceType;
import jadx.api.ResourcesLoader;
import jadx.api.impl.SimpleCodeInfo;
import jadx.core.utils.Utils;
import jadx.core.xmlgen.ResContainer;
import jadx.gui.utils.NLS;
import jadx.gui.utils.OverlayIcon;
import jadx.gui.utils.UiUtils;
import static jadx.core.codegen.CodeWriter.NL;
public class JResource extends JLoadableNode implements Comparable<JResource> {
private static final long serialVersionUID = -201018424302612434L;
@@ -151,7 +154,7 @@ public class JResource extends JLoadableNode implements Comparable<JResource> {
return ResourcesLoader.loadToCodeWriter(is);
});
} catch (Exception e) {
return new SimpleCodeInfo("Failed to load resource file: \n" + jadx.core.utils.Utils.getStackTrace(e));
return new SimpleCodeInfo("Failed to load resource file:" + NL + Utils.getStackTrace(e));
}
case DECODED_DATA:
@@ -18,6 +18,8 @@ import jadx.core.xmlgen.ResContainer;
import jadx.gui.treemodel.JResource;
import jadx.gui.ui.codearea.AbstractCodeArea;
import static jadx.core.codegen.CodeWriter.NL;
public class ImagePanel extends ContentPanel {
private static final long serialVersionUID = 4071356367073142688L;
@@ -30,7 +32,7 @@ public class ImagePanel extends ContentPanel {
add(imageViewer.getComponent());
} catch (Exception e) {
RSyntaxTextArea textArea = AbstractCodeArea.getDefaultArea(panel.getMainWindow());
textArea.setText("Image load error: \n" + Utils.getStackTrace(e));
textArea.setText("Image load error:" + NL + Utils.getStackTrace(e));
add(textArea);
}
}