提交 98d80150 编写于 作者: S Skylot

refactor: split field init attribute

上级 42a44f21
...@@ -22,8 +22,7 @@ import jadx.core.Consts; ...@@ -22,8 +22,7 @@ import jadx.core.Consts;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.AttrNode; import jadx.core.dex.attributes.AttrNode;
import jadx.core.dex.attributes.FieldInitAttr; import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.attributes.FieldInitAttr.InitType;
import jadx.core.dex.attributes.nodes.EnumClassAttr; import jadx.core.dex.attributes.nodes.EnumClassAttr;
import jadx.core.dex.attributes.nodes.EnumClassAttr.EnumField; import jadx.core.dex.attributes.nodes.EnumClassAttr.EnumField;
import jadx.core.dex.attributes.nodes.JadxError; import jadx.core.dex.attributes.nodes.JadxError;
...@@ -395,14 +394,14 @@ public class ClassGen { ...@@ -395,14 +394,14 @@ public class ClassGen {
FieldInitAttr fv = f.get(AType.FIELD_INIT); FieldInitAttr fv = f.get(AType.FIELD_INIT);
if (fv != null) { if (fv != null) {
code.add(" = "); code.add(" = ");
if (fv.getValueType() == InitType.CONST) { if (fv.isConst()) {
EncodedValue encodedValue = fv.getEncodedValue(); EncodedValue encodedValue = fv.getEncodedValue();
if (encodedValue.getType() == EncodedType.ENCODED_NULL) { if (encodedValue.getType() == EncodedType.ENCODED_NULL) {
code.add(TypeGen.literalToString(0, f.getType(), cls, fallback)); code.add(TypeGen.literalToString(0, f.getType(), cls, fallback));
} else { } else {
annotationGen.encodeValue(cls.root(), code, encodedValue); annotationGen.encodeValue(cls.root(), code, encodedValue);
} }
} else if (fv.getValueType() == InitType.INSN) { } else if (fv.isInsn()) {
InsnGen insnGen = makeInsnGen(fv.getInsnMth()); InsnGen insnGen = makeInsnGen(fv.getInsnMth());
addInsnBody(insnGen, code, fv.getInsn()); addInsnBody(insnGen, code, fv.getInsn());
} }
......
...@@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory; ...@@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.FieldInitAttr; import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.attributes.nodes.DeclareVariablesAttr; import jadx.core.dex.attributes.nodes.DeclareVariablesAttr;
import jadx.core.dex.attributes.nodes.ForceReturnAttr; import jadx.core.dex.attributes.nodes.ForceReturnAttr;
import jadx.core.dex.attributes.nodes.LoopLabelAttr; import jadx.core.dex.attributes.nodes.LoopLabelAttr;
...@@ -287,10 +287,10 @@ public class RegionGen extends InsnGen { ...@@ -287,10 +287,10 @@ public class RegionGen extends InsnGen {
staticField(code, fn.getFieldInfo()); staticField(code, fn.getFieldInfo());
// print original value, sometimes replaced with incorrect field // print original value, sometimes replaced with incorrect field
FieldInitAttr valueAttr = fn.get(AType.FIELD_INIT); FieldInitAttr valueAttr = fn.get(AType.FIELD_INIT);
if (valueAttr != null && valueAttr.getValueType() == FieldInitAttr.InitType.CONST) { if (valueAttr != null && valueAttr.isConst()) {
Object value = valueAttr.getEncodedValue(); Object value = valueAttr.getEncodedValue().getValue();
if (value != null) { if (value != null) {
code.add(" /*").add(value.toString()).add("*/"); code.add(" /* ").add(value.toString()).add(" */");
} }
} }
} }
......
...@@ -6,6 +6,7 @@ import java.util.Set; ...@@ -6,6 +6,7 @@ import java.util.Set;
import jadx.core.dex.attributes.annotations.AnnotationsList; import jadx.core.dex.attributes.annotations.AnnotationsList;
import jadx.core.dex.attributes.annotations.MethodParameters; import jadx.core.dex.attributes.annotations.MethodParameters;
import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.attributes.nodes.DeclareVariablesAttr; import jadx.core.dex.attributes.nodes.DeclareVariablesAttr;
import jadx.core.dex.attributes.nodes.EdgeInsnAttr; import jadx.core.dex.attributes.nodes.EdgeInsnAttr;
import jadx.core.dex.attributes.nodes.EnumClassAttr; import jadx.core.dex.attributes.nodes.EnumClassAttr;
......
package jadx.core.dex.attributes; package jadx.core.dex.attributes.fldinit;
import java.util.Objects;
import jadx.api.plugins.input.data.annotations.EncodedValue; import jadx.api.plugins.input.data.annotations.EncodedValue;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.IAttribute;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.exceptions.JadxRuntimeException;
public class FieldInitAttr implements IAttribute { public abstract class FieldInitAttr implements IAttribute {
public static final FieldInitAttr NULL_VALUE = constValue(EncodedValue.NULL);
public enum InitType { public static FieldInitAttr constValue(EncodedValue value) {
CONST, if (Objects.equals(value, EncodedValue.NULL)) {
INSN return FieldInitConstAttr.NULL_VALUE;
}
return new FieldInitConstAttr(value);
} }
private final Object value; public static FieldInitAttr insnValue(MethodNode mth, InsnNode insn) {
private final InitType valueType; return new FieldInitInsnAttr(mth, insn);
private final MethodNode insnMth;
private FieldInitAttr(InitType valueType, Object value, MethodNode insnMth) {
this.value = value;
this.valueType = valueType;
this.insnMth = insnMth;
} }
public static FieldInitAttr constValue(EncodedValue value) { public boolean isConst() {
return new FieldInitAttr(InitType.CONST, value, null); return false;
} }
public static FieldInitAttr insnValue(MethodNode mth, InsnNode insn) { public boolean isInsn() {
return new FieldInitAttr(InitType.INSN, insn, mth); return false;
} }
public EncodedValue getEncodedValue() { public EncodedValue getEncodedValue() {
return (EncodedValue) value; throw new JadxRuntimeException("Wrong init type");
} }
public InsnNode getInsn() { public InsnNode getInsn() {
return (InsnNode) value; throw new JadxRuntimeException("Wrong init type");
}
public InitType getValueType() {
return valueType;
} }
public MethodNode getInsnMth() { public MethodNode getInsnMth() {
return insnMth; throw new JadxRuntimeException("Wrong init type");
} }
@Override @Override
public AType<FieldInitAttr> getType() { public AType<FieldInitAttr> getType() {
return AType.FIELD_INIT; return AType.FIELD_INIT;
} }
@Override
public String toString() {
return "V=" + value;
}
} }
package jadx.core.dex.attributes.fldinit;
import jadx.api.plugins.input.data.annotations.EncodedValue;
import static java.util.Objects.requireNonNull;
public final class FieldInitConstAttr extends FieldInitAttr {
public static final FieldInitAttr NULL_VALUE = new FieldInitConstAttr(EncodedValue.NULL);
private final EncodedValue value;
FieldInitConstAttr(EncodedValue value) {
this.value = requireNonNull(value);
}
@Override
public EncodedValue getEncodedValue() {
return value;
}
@Override
public boolean isConst() {
return true;
}
@Override
public String toString() {
return "INIT{" + value + '}';
}
}
package jadx.core.dex.attributes.fldinit;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode;
import static java.util.Objects.requireNonNull;
public final class FieldInitInsnAttr extends FieldInitAttr {
private final MethodNode mth;
private final InsnNode insn;
FieldInitInsnAttr(MethodNode mth, InsnNode insn) {
this.mth = requireNonNull(mth);
this.insn = requireNonNull(insn);
}
@Override
public InsnNode getInsn() {
return insn;
}
@Override
public MethodNode getInsnMth() {
return mth;
}
@Override
public boolean isInsn() {
return true;
}
@Override
public String toString() {
return "INIT{" + insn + '}';
}
}
...@@ -13,7 +13,7 @@ import org.jetbrains.annotations.Nullable; ...@@ -13,7 +13,7 @@ import org.jetbrains.annotations.Nullable;
import jadx.api.JadxArgs; import jadx.api.JadxArgs;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.FieldInitAttr; import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.instructions.args.LiteralArg; import jadx.core.dex.instructions.args.LiteralArg;
import jadx.core.dex.instructions.args.PrimitiveType; import jadx.core.dex.instructions.args.PrimitiveType;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
...@@ -85,10 +85,7 @@ public class ConstStorage { ...@@ -85,10 +85,7 @@ public class ConstStorage {
AccessInfo accFlags = f.getAccessFlags(); AccessInfo accFlags = f.getAccessFlags();
if (accFlags.isStatic() && accFlags.isFinal()) { if (accFlags.isStatic() && accFlags.isFinal()) {
FieldInitAttr fv = f.get(AType.FIELD_INIT); FieldInitAttr fv = f.get(AType.FIELD_INIT);
if (fv != null if (fv != null && fv.isConst() && fv.getEncodedValue().getValue() != null) {
&& fv.getValueType() == FieldInitAttr.InitType.CONST
&& fv != FieldInitAttr.NULL_VALUE
&& fv.getEncodedValue() != null) {
addConstField(cls, f, fv.getEncodedValue().getValue(), accFlags.isPublic()); addConstField(cls, f, fv.getEncodedValue().getValue(), accFlags.isPublic());
} }
} }
......
...@@ -23,8 +23,9 @@ import jadx.api.plugins.input.data.annotations.IAnnotation; ...@@ -23,8 +23,9 @@ import jadx.api.plugins.input.data.annotations.IAnnotation;
import jadx.core.Consts; import jadx.core.Consts;
import jadx.core.ProcessClass; import jadx.core.ProcessClass;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.FieldInitAttr;
import jadx.core.dex.attributes.annotations.AnnotationsList; import jadx.core.dex.attributes.annotations.AnnotationsList;
import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.attributes.fldinit.FieldInitConstAttr;
import jadx.core.dex.attributes.nodes.NotificationAttrNode; import jadx.core.dex.attributes.nodes.NotificationAttrNode;
import jadx.core.dex.attributes.nodes.SourceFileAttr; import jadx.core.dex.attributes.nodes.SourceFileAttr;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
...@@ -164,7 +165,7 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN ...@@ -164,7 +165,7 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN
for (FieldNode f : staticFields) { for (FieldNode f : staticFields) {
if (f.getAccessFlags().isFinal()) { if (f.getAccessFlags().isFinal()) {
// incorrect initialization will be removed if assign found in constructor // incorrect initialization will be removed if assign found in constructor
f.addAttr(FieldInitAttr.NULL_VALUE); f.addAttr(FieldInitConstAttr.NULL_VALUE);
} }
} }
try { try {
......
...@@ -8,7 +8,7 @@ import java.util.Set; ...@@ -8,7 +8,7 @@ import java.util.Set;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.FieldInitAttr; import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.FieldInfo; import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.instructions.IndexInsnNode; import jadx.core.dex.instructions.IndexInsnNode;
......
...@@ -8,7 +8,7 @@ import org.slf4j.LoggerFactory; ...@@ -8,7 +8,7 @@ import org.slf4j.LoggerFactory;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.FieldInitAttr; import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.info.FieldInfo; import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.instructions.ConstClassNode; import jadx.core.dex.instructions.ConstClassNode;
import jadx.core.dex.instructions.ConstStringNode; import jadx.core.dex.instructions.ConstStringNode;
...@@ -101,7 +101,7 @@ public class InsnUtils { ...@@ -101,7 +101,7 @@ public class InsnUtils {
return null; return null;
} }
FieldInitAttr attr = fieldNode.get(AType.FIELD_INIT); FieldInitAttr attr = fieldNode.get(AType.FIELD_INIT);
if (attr != null && attr.getValueType() == FieldInitAttr.InitType.CONST) { if (attr != null && attr.isConst()) {
return EncodedValueUtils.convertToConstValue(root, attr.getEncodedValue()); return EncodedValueUtils.convertToConstValue(root, attr.getEncodedValue());
} }
return null; return null;
......
...@@ -17,7 +17,7 @@ import jadx.core.codegen.CodeWriter; ...@@ -17,7 +17,7 @@ import jadx.core.codegen.CodeWriter;
import jadx.core.deobf.NameMapper; import jadx.core.deobf.NameMapper;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.FieldInitAttr; import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.ClassInfo; import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.ConstStorage; import jadx.core.dex.info.ConstStorage;
......
...@@ -7,7 +7,7 @@ import java.util.Map; ...@@ -7,7 +7,7 @@ import java.util.Map;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.FieldInitAttr; import jadx.core.dex.attributes.fldinit.FieldInitAttr;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.FieldNode; import jadx.core.dex.nodes.FieldNode;
import jadx.tests.api.IntegrationTest; import jadx.tests.api.IntegrationTest;
......
package jadx.api.plugins.input.data.annotations; package jadx.api.plugins.input.data.annotations;
import java.util.Objects;
public class EncodedValue { public class EncodedValue {
public static final EncodedValue NULL = new EncodedValue(EncodedType.ENCODED_NULL, new Object()); public static final EncodedValue NULL = new EncodedValue(EncodedType.ENCODED_NULL, null);
private final EncodedType type; private final EncodedType type;
private final Object value; private final Object value;
...@@ -19,9 +21,28 @@ public class EncodedValue { ...@@ -19,9 +21,28 @@ public class EncodedValue {
return value; return value;
} }
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
EncodedValue that = (EncodedValue) o;
return type == that.getType() && Objects.equals(value, that.getValue());
}
@Override
public int hashCode() {
return Objects.hash(getType(), getValue());
}
@Override @Override
public String toString() { public String toString() {
switch (type) { switch (type) {
case ENCODED_NULL:
return "null";
case ENCODED_STRING: case ENCODED_STRING:
return (String) value; return (String) value;
case ENCODED_ARRAY: case ENCODED_ARRAY:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册