Search in sources :

Example 26 with FieldNode

use of jadx.core.dex.nodes.FieldNode in project jadx by skylot.

the class ConstStorage method getConstField.

@Nullable
public FieldNode getConstField(ClassNode cls, Object value, boolean searchGlobal) {
    if (!replaceEnabled) {
        return null;
    }
    RootNode root = cls.root();
    if (value instanceof Integer) {
        FieldNode rField = getResourceField((Integer) value, root);
        if (rField != null) {
            return rField;
        }
    }
    boolean foundInGlobal = globalValues.contains(value);
    if (foundInGlobal && !searchGlobal) {
        return null;
    }
    ClassNode current = cls;
    while (current != null) {
        ValueStorage classValues = classes.get(current);
        if (classValues != null) {
            FieldNode field = classValues.get(value);
            if (field != null) {
                if (foundInGlobal) {
                    return null;
                }
                return field;
            }
        }
        ClassInfo parentClass = current.getClassInfo().getParentClass();
        if (parentClass == null) {
            break;
        }
        current = root.resolveClass(parentClass);
    }
    if (searchGlobal) {
        return globalValues.get(value);
    }
    return null;
}
Also used : RootNode(jadx.core.dex.nodes.RootNode) ClassNode(jadx.core.dex.nodes.ClassNode) FieldNode(jadx.core.dex.nodes.FieldNode) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with FieldNode

use of jadx.core.dex.nodes.FieldNode in project jadx by skylot.

the class ProcessKotlinInternals method getConstString.

@Nullable
private String getConstString(MethodNode mth, InsnNode insn, int arg) {
    InsnArg strArg = insn.getArg(arg);
    if (!strArg.isInsnWrap()) {
        return null;
    }
    InsnNode constInsn = ((InsnWrapArg) strArg).getWrapInsn();
    InsnType insnType = constInsn.getType();
    if (insnType == InsnType.CONST_STR) {
        return ((ConstStringNode) constInsn).getString();
    }
    if (insnType == InsnType.SGET) {
        // revert const field inline :(
        FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) constInsn).getIndex();
        FieldNode fieldNode = mth.root().resolveField(fieldInfo);
        if (fieldNode != null) {
            String str = (String) fieldNode.get(JadxAttrType.CONSTANT_VALUE).getValue();
            InsnArg newArg = InsnArg.wrapArg(new ConstStringNode(str));
            insn.replaceArg(strArg, newArg);
            return str;
        }
    }
    return null;
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) ConstStringNode(jadx.core.dex.instructions.ConstStringNode) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg) InsnType(jadx.core.dex.instructions.InsnType) FieldInfo(jadx.core.dex.info.FieldInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with FieldNode

use of jadx.core.dex.nodes.FieldNode in project jadx by skylot.

the class TestConstReplace method testWithoutReplace.

@Test
public void testWithoutReplace() {
    getArgs().setReplaceConsts(false);
    ClassNode cls = getClassNode(TestCls.class);
    assertThat(cls).code().containsOne("return \"string\";");
    FieldNode constField = cls.searchFieldByName("CONST_VALUE");
    assertThat(constField).isNotNull();
    assertThat(constField.getUseIn()).isEmpty();
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) FieldNode(jadx.core.dex.nodes.FieldNode) Test(org.junit.jupiter.api.Test) IntegrationTest(jadx.tests.api.IntegrationTest)

Example 29 with FieldNode

use of jadx.core.dex.nodes.FieldNode in project jadx by skylot.

the class TestConstReplace method test.

@Test
public void test() {
    ClassNode cls = getClassNode(TestCls.class);
    assertThat(cls).code().containsOne("return CONST_VALUE;");
    MethodNode testMth = cls.searchMethodByShortName("test");
    assertThat(testMth).isNotNull();
    FieldNode constField = cls.searchFieldByName("CONST_VALUE");
    assertThat(constField).isNotNull();
    assertThat(constField.getUseIn()).containsExactly(testMth);
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) MethodNode(jadx.core.dex.nodes.MethodNode) FieldNode(jadx.core.dex.nodes.FieldNode) Test(org.junit.jupiter.api.Test) IntegrationTest(jadx.tests.api.IntegrationTest)

Example 30 with FieldNode

use of jadx.core.dex.nodes.FieldNode in project jadx by skylot.

the class AnonymousClassVisitor method processAnonymousConstructor.

private static void processAnonymousConstructor(MethodNode mth) {
    List<InsnNode> usedInsns = new ArrayList<>();
    Map<InsnArg, FieldNode> argsMap = getArgsToFieldsMapping(mth, usedInsns);
    if (argsMap.isEmpty()) {
        mth.add(AFlag.NO_SKIP_ARGS);
    } else {
        for (Map.Entry<InsnArg, FieldNode> entry : argsMap.entrySet()) {
            FieldNode field = entry.getValue();
            if (field == null) {
                continue;
            }
            InsnArg arg = entry.getKey();
            field.addAttr(new FieldReplaceAttr(arg));
            field.add(AFlag.DONT_GENERATE);
            if (arg.isRegister()) {
                arg.add(AFlag.SKIP_ARG);
                SkipMethodArgsAttr.skipArg(mth, ((RegisterArg) arg));
            }
        }
    }
    for (InsnNode usedInsn : usedInsns) {
        usedInsn.add(AFlag.DONT_GENERATE);
    }
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) ArrayList(java.util.ArrayList) FieldReplaceAttr(jadx.core.dex.attributes.nodes.FieldReplaceAttr) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

FieldNode (jadx.core.dex.nodes.FieldNode)70 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)30 InsnNode (jadx.core.dex.nodes.InsnNode)28 ClassNode (jadx.core.dex.nodes.ClassNode)26 FieldInfo (jadx.core.dex.info.FieldInfo)23 InsnArg (jadx.core.dex.instructions.args.InsnArg)20 MethodNode (jadx.core.dex.nodes.MethodNode)14 ArrayList (java.util.ArrayList)11 ArgType (jadx.core.dex.instructions.args.ArgType)10 InsnWrapArg (jadx.core.dex.instructions.args.InsnWrapArg)10 Nullable (org.jetbrains.annotations.Nullable)10 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)9 ClassInfo (jadx.core.dex.info.ClassInfo)6 ConstClassNode (jadx.core.dex.instructions.ConstClassNode)6 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)6 FieldReplaceAttr (jadx.core.dex.attributes.nodes.FieldReplaceAttr)5 MethodInfo (jadx.core.dex.info.MethodInfo)5 InsnType (jadx.core.dex.instructions.InsnType)5 ConstructorInsn (jadx.core.dex.instructions.mods.ConstructorInsn)5 BlockNode (jadx.core.dex.nodes.BlockNode)4