fix: use empty line before field in correct place (#2101)
This commit is contained in:
@@ -421,18 +421,19 @@ public class ClassGen {
|
||||
if (f.contains(AFlag.DONT_GENERATE)) {
|
||||
return;
|
||||
}
|
||||
if (f.contains(JadxAttrType.ANNOTATION_LIST) || f.contains(AType.JADX_COMMENTS) || f.getFieldInfo().hasAlias()) {
|
||||
code.newLine();
|
||||
}
|
||||
if (Consts.DEBUG_USAGE) {
|
||||
addFieldUsageInfo(code, f);
|
||||
}
|
||||
if (f.getFieldInfo().hasAlias()) {
|
||||
CodeGenUtils.addRenamedComment(code, f, f.getName());
|
||||
}
|
||||
CodeGenUtils.addComments(code, f);
|
||||
annotationGen.addForField(code, f);
|
||||
|
||||
boolean addInfoComments = f.checkCommentsLevel(CommentsLevel.INFO);
|
||||
if (f.getFieldInfo().hasAlias() && addInfoComments) {
|
||||
code.newLine();
|
||||
CodeGenUtils.addRenamedComment(code, f, f.getName());
|
||||
}
|
||||
code.startLine(f.getAccessFlags().makeString(addInfoComments));
|
||||
code.startLine(f.getAccessFlags().makeString(f.checkCommentsLevel(CommentsLevel.INFO)));
|
||||
useType(code, f.getType());
|
||||
code.add(' ');
|
||||
code.attachDefinition(f);
|
||||
|
||||
@@ -18,6 +18,7 @@ import jadx.core.dex.nodes.FieldNode;
|
||||
import jadx.core.dex.nodes.MethodNode;
|
||||
import jadx.core.dex.nodes.PackageNode;
|
||||
import jadx.core.dex.nodes.RootNode;
|
||||
import jadx.core.utils.StringUtils;
|
||||
|
||||
public class UserRenames {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UserRenames.class);
|
||||
@@ -57,7 +58,12 @@ public class UserRenames {
|
||||
case FIELD:
|
||||
FieldNode fieldNode = cls.searchFieldByShortId(nodeRef.getShortId());
|
||||
if (fieldNode == null) {
|
||||
LOG.warn("Field reference not found: {}", nodeRef);
|
||||
String fieldName = StringUtils.getPrefix(nodeRef.getShortId(), ":");
|
||||
String fieldSign = cls.getFields().stream()
|
||||
.filter(f -> f.getFieldInfo().getName().equals(fieldName))
|
||||
.map(f -> f.getFieldInfo().getShortId())
|
||||
.collect(Collectors.joining());
|
||||
LOG.warn("Field reference not found: {}. Fields with same name: {}", nodeRef, fieldSign);
|
||||
} else {
|
||||
fieldNode.rename(rename.getNewName());
|
||||
}
|
||||
|
||||
@@ -124,8 +124,7 @@ public class CodeGenUtils {
|
||||
code.startLine("/* renamed from: ").add(origName);
|
||||
RenameReasonAttr renameReasonAttr = node.get(AType.RENAME_REASON);
|
||||
if (renameReasonAttr != null) {
|
||||
code.add(" reason: ");
|
||||
code.add(renameReasonAttr.getDescription());
|
||||
code.add(", reason: ").add(renameReasonAttr.getDescription());
|
||||
}
|
||||
code.add(" */");
|
||||
}
|
||||
|
||||
@@ -374,6 +374,14 @@ public class StringUtils {
|
||||
return str;
|
||||
}
|
||||
|
||||
public static @Nullable String getPrefix(String str, String delim) {
|
||||
int idx = str.indexOf(delim);
|
||||
if (idx != -1) {
|
||||
return str.substring(0, idx);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getDateText() {
|
||||
return new SimpleDateFormat("HH:mm:ss").format(new Date());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package jadx.tests.integration.rename;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import jadx.api.data.ICodeRename;
|
||||
import jadx.api.data.IJavaNodeRef.RefType;
|
||||
import jadx.api.data.impl.JadxCodeData;
|
||||
import jadx.api.data.impl.JadxCodeRename;
|
||||
import jadx.api.data.impl.JadxNodeRef;
|
||||
import jadx.tests.api.IntegrationTest;
|
||||
|
||||
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
|
||||
|
||||
public class TestFieldRenameFormat extends IntegrationTest {
|
||||
|
||||
@SuppressWarnings({ "unused", "NonSerializableClassWithSerialVersionUID" })
|
||||
public static class TestCls {
|
||||
private static final long serialVersionUID = -2619335455376089892L;
|
||||
@SerializedName("id")
|
||||
private int b;
|
||||
@SerializedName("title")
|
||||
private String c;
|
||||
@SerializedName("images")
|
||||
private List<String> d;
|
||||
@SerializedName("authors")
|
||||
private List<String> e;
|
||||
@SerializedName("description")
|
||||
private String f;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
noDebugInfo();
|
||||
|
||||
String baseClsId = TestCls.class.getName();
|
||||
List<ICodeRename> renames = Arrays.asList(
|
||||
fieldRename(baseClsId, "b:I", "id"),
|
||||
fieldRename(baseClsId, "c:Ljava/lang/String;", "title"),
|
||||
fieldRename(baseClsId, "e:Ljava/util/List;", "authors"));
|
||||
|
||||
JadxCodeData codeData = new JadxCodeData();
|
||||
codeData.setRenames(renames);
|
||||
getArgs().setCodeData(codeData);
|
||||
getArgs().setDeobfuscationOn(false);
|
||||
|
||||
assertThat(getClassNode(TestCls.class))
|
||||
.code()
|
||||
.containsOne("private int id;")
|
||||
.containsOne("private List<String> authors;")
|
||||
.containsLines(1,
|
||||
"",
|
||||
"/* renamed from: c */",
|
||||
"@SerializedName(\"title\")",
|
||||
"private String title;",
|
||||
"");
|
||||
}
|
||||
|
||||
private static JadxCodeRename fieldRename(String baseClsId, String shortId, String id) {
|
||||
return new JadxCodeRename(new JadxNodeRef(RefType.FIELD, baseClsId, shortId), id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user