Commit 07402ba4 authored by Skylot's avatar Skylot

core: fix "null" enum field

parent d6069820
package jadx.core.dex.instructions.args; package jadx.core.dex.instructions.args;
import jadx.core.dex.attributes.AttributeType;
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;
import jadx.core.dex.instructions.IndexInsnNode;
import jadx.core.dex.instructions.InsnType; import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.nodes.DexNode;
import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.parser.FieldValueAttr;
import jadx.core.dex.visitors.InstructionRemover; import jadx.core.dex.visitors.InstructionRemover;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RegisterArg extends InsnArg { public class RegisterArg extends InsnArg {
private static final Logger LOG = LoggerFactory.getLogger(RegisterArg.class);
protected final int regNum; protected final int regNum;
public RegisterArg(int rn) { public RegisterArg(int rn) {
...@@ -45,7 +56,7 @@ public class RegisterArg extends InsnArg { ...@@ -45,7 +56,7 @@ public class RegisterArg extends InsnArg {
* *
* @return LiteralArg, String or ArgType * @return LiteralArg, String or ArgType
*/ */
public Object getConstValue() { public Object getConstValue(DexNode dex) {
InsnNode parInsn = getAssignInsn(); InsnNode parInsn = getAssignInsn();
if (parInsn != null) { if (parInsn != null) {
InsnType insnType = parInsn.getType(); InsnType insnType = parInsn.getType();
...@@ -56,6 +67,18 @@ public class RegisterArg extends InsnArg { ...@@ -56,6 +67,18 @@ public class RegisterArg extends InsnArg {
return ((ConstStringNode) parInsn).getString(); return ((ConstStringNode) parInsn).getString();
case CONST_CLASS: case CONST_CLASS:
return ((ConstClassNode) parInsn).getClsType(); return ((ConstClassNode) parInsn).getClsType();
case SGET:
FieldInfo f = (FieldInfo) ((IndexInsnNode) parInsn).getIndex();
FieldNode fieldNode = dex.resolveField(f);
if (fieldNode != null) {
FieldValueAttr attr = (FieldValueAttr) fieldNode.getAttributes().get(AttributeType.FIELD_VALUE);
if (attr != null) {
return attr.getValue();
}
} else {
LOG.warn("Field {} not found in dex {}", f, dex);
}
break;
} }
} }
return null; return null;
......
package jadx.core.dex.nodes; package jadx.core.dex.nodes;
import jadx.core.dex.info.ClassInfo; import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.info.MethodInfo; import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.ArgType;
import jadx.core.utils.exceptions.DecodeException; import jadx.core.utils.exceptions.DecodeException;
...@@ -64,6 +65,14 @@ public class DexNode { ...@@ -64,6 +65,14 @@ public class DexNode {
return null; return null;
} }
public FieldNode resolveField(FieldInfo field) {
ClassNode cls = resolveClass(field.getDeclClass());
if (cls != null) {
return cls.searchField(field);
}
return null;
}
public Map<Object, FieldNode> getConstFields() { public Map<Object, FieldNode> getConstFields() {
return constFields; return constFields;
} }
......
...@@ -117,7 +117,10 @@ public class EnumVisitor extends AbstractVisitor { ...@@ -117,7 +117,10 @@ public class EnumVisitor extends AbstractVisitor {
RegisterArg nameArg = (RegisterArg) insn.getArg(0); RegisterArg nameArg = (RegisterArg) insn.getArg(0);
// InsnArg pos = insn.getArg(1); // InsnArg pos = insn.getArg(1);
// TODO add check: pos == j // TODO add check: pos == j
String name = (String) nameArg.getConstValue(); String name = (String) nameArg.getConstValue(cls.dex());
if (name == null) {
throw new JadxException("Unknown enum field name: " + cls);
}
EnumField field = new EnumField(name, insn.getArgsCount() - 2); EnumField field = new EnumField(name, insn.getArgsCount() - 2);
attr.getFields().add(field); attr.getFields().add(field);
......
...@@ -11,6 +11,12 @@ public class TestEnum extends AbstractTest { ...@@ -11,6 +11,12 @@ public class TestEnum extends AbstractTest {
NORTH, SOUTH, EAST, WEST NORTH, SOUTH, EAST, WEST
} }
public static final String DOG = "DOG";
public enum Animal {
CAT, DOG
}
private static int three = 3; private static int three = 3;
public enum Numbers { public enum Numbers {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment