diff --git a/jadx-core/src/main/java/jadx/core/codegen/ClassGen.java b/jadx-core/src/main/java/jadx/core/codegen/ClassGen.java index 7c9ff67c1..11bde57ab 100644 --- a/jadx-core/src/main/java/jadx/core/codegen/ClassGen.java +++ b/jadx-core/src/main/java/jadx/core/codegen/ClassGen.java @@ -155,8 +155,6 @@ public class ClassGen { if (Consts.DEBUG_USAGE) { addClassUsageInfo(code, cls); } - CodeGenUtils.addErrorsAndComments(code, cls); - CodeGenUtils.addSourceFileInfo(code, cls); addClassDeclaration(code); addClassBody(code); } @@ -177,9 +175,13 @@ public class ClassGen { af = af.remove(AccessFlags.STATIC).remove(AccessFlags.PRIVATE); } - annotationGen.addForClass(clsCode); - insertRenameInfo(clsCode, cls); + CodeGenUtils.addComments(clsCode, cls); + CodeGenUtils.addClassRenamedComment(clsCode, cls); + CodeGenUtils.addErrors(clsCode, cls); + CodeGenUtils.addSourceFileInfo(clsCode, cls); CodeGenUtils.addInputFileInfo(clsCode, cls); + + annotationGen.addForClass(clsCode); clsCode.startLineWithNum(cls.getSourceLine()).add(af.makeString(cls.checkCommentsLevel(CommentsLevel.INFO))); if (af.isInterface()) { if (af.isAnnotation()) { @@ -434,10 +436,10 @@ public class ClassGen { if (Consts.DEBUG_USAGE) { addFieldUsageInfo(code, f); } + CodeGenUtils.addComments(code, f); if (f.getFieldInfo().hasAlias()) { CodeGenUtils.addRenamedComment(code, f, f.getName()); } - CodeGenUtils.addComments(code, f); annotationGen.addForField(code, f); code.startLine(f.getAccessFlags().makeString(f.checkCommentsLevel(CommentsLevel.INFO))); @@ -802,13 +804,6 @@ public class ClassGen { return root.getClsp().isClsKnown(currentPkg + '.' + shortName); } - private void insertRenameInfo(ICodeWriter code, ClassNode cls) { - ClassInfo classInfo = cls.getClassInfo(); - if (classInfo.hasAlias() && cls.checkCommentsLevel(CommentsLevel.INFO)) { - CodeGenUtils.addRenamedComment(code, cls, classInfo.getType().getObject()); - } - } - private static void addClassUsageInfo(ICodeWriter code, ClassNode cls) { List deps = cls.getDependencies(); code.startLine("// deps - ").add(Integer.toString(deps.size())); diff --git a/jadx-core/src/main/java/jadx/core/codegen/utils/CodeGenUtils.java b/jadx-core/src/main/java/jadx/core/codegen/utils/CodeGenUtils.java index b703df797..9be1fa6e8 100644 --- a/jadx-core/src/main/java/jadx/core/codegen/utils/CodeGenUtils.java +++ b/jadx-core/src/main/java/jadx/core/codegen/utils/CodeGenUtils.java @@ -1,6 +1,7 @@ package jadx.core.codegen.utils; import java.util.List; +import java.util.function.BiConsumer; import java.util.regex.Pattern; import org.jetbrains.annotations.Nullable; @@ -16,6 +17,7 @@ import jadx.core.dex.attributes.nodes.JadxCommentsAttr; import jadx.core.dex.attributes.nodes.JadxError; import jadx.core.dex.attributes.nodes.NotificationAttrNode; import jadx.core.dex.attributes.nodes.RenameReasonAttr; +import jadx.core.dex.info.ClassInfo; import jadx.core.dex.instructions.args.CodeVar; import jadx.core.dex.instructions.args.RegisterArg; import jadx.core.dex.instructions.args.SSAVar; @@ -26,8 +28,8 @@ import jadx.core.utils.Utils; public class CodeGenUtils { public static void addErrorsAndComments(ICodeWriter code, NotificationAttrNode node) { - addErrors(code, node); addComments(code, node); + addErrors(code, node); } public static void addErrors(ICodeWriter code, NotificationAttrNode node) { @@ -86,9 +88,37 @@ public class CodeGenUtils { } else { code.add(' '); } - CommentStyle style = comment.getStyle(); + addCommentWithStyle(code, comment.getStyle(), comment.getComment()); + } + + public static void addJadxNodeComment(ICodeWriter code, NotificationAttrNode node, + CommentsLevel level, BiConsumer commentFunc) { + if (node.checkCommentsLevel(level)) { + code.startLine(); + addCommentWithStyle(code, CommentStyle.BLOCK_CONDENSED, (commentCode, newLinePrefix) -> { + commentCode.add("JADX ").add(level.name()).add(": "); + commentFunc.accept(commentCode, newLinePrefix); + }); + } + } + + public static void addJadxComment(ICodeWriter code, CommentsLevel level, String commentStr) { + code.startLine(); + addCommentWithStyle(code, CommentStyle.BLOCK_CONDENSED, "JADX " + level.name() + ": " + commentStr); + } + + private static void addCommentWithStyle(ICodeWriter code, CommentStyle style, String commentStr) { appendMultiLineString(code, "", style.getStart()); - appendMultiLineString(code, style.getOnNewLine(), comment.getComment()); + appendMultiLineString(code, style.getOnNewLine(), commentStr); + appendMultiLineString(code, "", style.getEnd()); + } + + /** + * Insert comment with function, use second arg as new line prefix + */ + private static void addCommentWithStyle(ICodeWriter code, CommentStyle style, BiConsumer commentFunc) { + appendMultiLineString(code, "", style.getStart()); + commentFunc.accept(code, style.getOnNewLine()); appendMultiLineString(code, "", style.getEnd()); } @@ -107,16 +137,21 @@ public class CodeGenUtils { } } + public static void addClassRenamedComment(ICodeWriter code, ClassNode cls) { + ClassInfo classInfo = cls.getClassInfo(); + if (classInfo.hasAlias()) { + addRenamedComment(code, cls, classInfo.getType().getObject()); + } + } + public static void addRenamedComment(ICodeWriter code, NotificationAttrNode node, String origName) { - if (!node.checkCommentsLevel(CommentsLevel.INFO)) { - return; - } - code.startLine("/* renamed from: ").add(origName); - RenameReasonAttr renameReasonAttr = node.get(AType.RENAME_REASON); - if (renameReasonAttr != null) { - code.add(", reason: ").add(renameReasonAttr.getDescription()); - } - code.add(" */"); + addJadxNodeComment(code, node, CommentsLevel.INFO, (commentCode, newLinePrefix) -> { + commentCode.add("renamed from: ").add(origName); + RenameReasonAttr renameReasonAttr = node.get(AType.RENAME_REASON); + if (renameReasonAttr != null) { + commentCode.add(", reason: ").add(renameReasonAttr.getDescription()); + } + }); } public static void addSourceFileInfo(ICodeWriter code, ClassNode node) { @@ -131,7 +166,7 @@ public class CodeGenUtils { // ignore similar name return; } - code.startLine("/* compiled from: ").add(fileName).add(" */"); + addJadxComment(code, CommentsLevel.INFO, "compiled from: " + fileName); } } @@ -146,7 +181,7 @@ public class CodeGenUtils { // don't add same comment for inner classes return; } - code.startLine("/* loaded from: ").add(inputFileName).add(" */"); + addJadxComment(code, CommentsLevel.INFO, "loaded from: " + inputFileName); } } } diff --git a/jadx-core/src/test/java/jadx/tests/integration/deobf/TestRenameOverriddenMethod.java b/jadx-core/src/test/java/jadx/tests/integration/deobf/TestRenameOverriddenMethod.java index 2558ed593..a34e11d79 100644 --- a/jadx-core/src/test/java/jadx/tests/integration/deobf/TestRenameOverriddenMethod.java +++ b/jadx-core/src/test/java/jadx/tests/integration/deobf/TestRenameOverriddenMethod.java @@ -35,7 +35,7 @@ public class TestRenameOverriddenMethod extends IntegrationTest { assertThat(getClassNode(TestCls.class)) .code() .countString(2, "@Override") - .countString(3, "/* renamed from: m */") + .countString(3, "renamed from: m") .containsOne("void mo0m();") .countString(2, "public void mo0m() {"); } diff --git a/jadx-core/src/test/java/jadx/tests/integration/rename/TestFieldRenameFormat.java b/jadx-core/src/test/java/jadx/tests/integration/rename/TestFieldRenameFormat.java index ec62ae019..93baae6df 100644 --- a/jadx-core/src/test/java/jadx/tests/integration/rename/TestFieldRenameFormat.java +++ b/jadx-core/src/test/java/jadx/tests/integration/rename/TestFieldRenameFormat.java @@ -54,7 +54,7 @@ public class TestFieldRenameFormat extends IntegrationTest { .containsOne("private List authors;") .containsLines(1, "", - "/* renamed from: c */", + "/* JADX INFO: renamed from: c */", "@SerializedName(\"title\")", "private String title;", ""); diff --git a/jadx-core/src/test/java/jadx/tests/integration/rename/TestUsingSourceFileName.java b/jadx-core/src/test/java/jadx/tests/integration/rename/TestUsingSourceFileName.java index 6f1d9ac17..ce24a455a 100644 --- a/jadx-core/src/test/java/jadx/tests/integration/rename/TestUsingSourceFileName.java +++ b/jadx-core/src/test/java/jadx/tests/integration/rename/TestUsingSourceFileName.java @@ -41,7 +41,7 @@ public class TestUsingSourceFileName extends SmaliTest { assertThat(searchCls(loadFromSmaliFiles(), "b")) .code() .containsOne("class C0000b {") - .containsOne("/* compiled from: a.java */"); + .containsOne("compiled from: a.java"); } @Test @@ -52,7 +52,7 @@ public class TestUsingSourceFileName extends SmaliTest { assertThat(searchCls(loadFromSmaliFiles(), "b")) .code() .containsOne("class a {") - .containsOne("/* compiled from: a.java */"); + .containsOne("compiled from: a.java"); } @Test @@ -63,7 +63,7 @@ public class TestUsingSourceFileName extends SmaliTest { assertThat(searchCls(loadFromSmaliFiles(), "b")) .code() .containsOne("class a {") - .containsOne("/* compiled from: a.java */"); + .containsOne("compiled from: a.java"); } @Test @@ -93,7 +93,7 @@ public class TestUsingSourceFileName extends SmaliTest { assertThat(searchCls(loadFromSmaliFiles(), "b")) .code() .containsOne("class C0000b {") - .containsOne("/* compiled from: a.java */"); + .containsOne("compiled from: a.java"); } @Test @@ -105,6 +105,6 @@ public class TestUsingSourceFileName extends SmaliTest { assertThat(searchCls(loadFromSmaliFiles(), "b")) .code() .containsOne("class a {") - .containsOne("/* compiled from: a.java */"); + .containsOne("compiled from: a.java"); } }