Skip to content

Commit b78d3aa

Browse files
committed
fix: do not replace constant fields which still used in code (#2414)
1 parent 4644d1d commit b78d3aa

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

jadx-core/src/main/java/jadx/core/Jadx.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public static List<IDexTreeVisitor> getPreDecompilePassesList() {
102102
passes.add(new SignatureProcessor());
103103
passes.add(new OverrideMethodVisitor());
104104
passes.add(new AddAndroidConstants());
105-
passes.add(new CollectConstValues());
106105

107106
// rename and deobfuscation
108107
passes.add(new DeobfuscatorVisitor());
@@ -111,6 +110,7 @@ public static List<IDexTreeVisitor> getPreDecompilePassesList() {
111110
passes.add(new SaveDeobfMapping());
112111

113112
passes.add(new UsageInfoVisitor());
113+
passes.add(new CollectConstValues());
114114
passes.add(new ProcessAnonymous());
115115
passes.add(new ProcessMethodsForInline());
116116
return passes;

jadx-core/src/main/java/jadx/core/dex/visitors/prepare/CollectConstValues.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
import jadx.core.dex.nodes.RootNode;
1212
import jadx.core.dex.visitors.AbstractVisitor;
1313
import jadx.core.dex.visitors.JadxVisitor;
14+
import jadx.core.dex.visitors.usage.UsageInfoVisitor;
1415
import jadx.core.utils.exceptions.JadxException;
1516

1617
@JadxVisitor(
1718
name = "CollectConstValues",
18-
desc = "Collect and store values from static final fields"
19+
desc = "Collect and store values from static final fields",
20+
runAfter = {
21+
UsageInfoVisitor.class // check field usage (do not restore if used somewhere)
22+
}
1923
)
2024
public class CollectConstValues extends AbstractVisitor {
2125

@@ -44,12 +48,17 @@ public boolean visit(ClassNode cls) throws JadxException {
4448

4549
public static @Nullable Object getFieldConstValue(FieldNode fld) {
4650
AccessInfo accFlags = fld.getAccessFlags();
47-
if (accFlags.isStatic() && accFlags.isFinal()) {
48-
EncodedValue constVal = fld.get(JadxAttrType.CONSTANT_VALUE);
49-
if (constVal != null && constVal != EncodedValue.NULL) {
50-
return constVal.getValue();
51-
}
51+
if (!accFlags.isStatic() || !accFlags.isFinal()) {
52+
return null;
53+
}
54+
EncodedValue constVal = fld.get(JadxAttrType.CONSTANT_VALUE);
55+
if (constVal == null || constVal == EncodedValue.NULL) {
56+
return null;
57+
}
58+
if (!fld.getUseIn().isEmpty()) {
59+
// field still used somewhere and not inlined by compiler, so we don't need to restore it
60+
return null;
5261
}
53-
return null;
62+
return constVal.getValue();
5463
}
5564
}

0 commit comments

Comments
 (0)