Search in sources :

Example 31 with FieldNode

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

the class AnonymousClassVisitor method getArgsToFieldsMapping.

private static Map<InsnArg, FieldNode> getArgsToFieldsMapping(MethodNode mth, List<InsnNode> usedInsns) {
    MethodInfo callMth = mth.getMethodInfo();
    ClassNode cls = mth.getParentClass();
    List<RegisterArg> argList = mth.getArgRegs();
    ClassNode outerCls = mth.getUseIn().get(0).getParentClass();
    int startArg = 0;
    if (callMth.getArgsCount() != 0 && callMth.getArgumentsTypes().get(0).equals(outerCls.getClassInfo().getType())) {
        startArg = 1;
    }
    Map<InsnArg, FieldNode> map = new LinkedHashMap<>();
    int argsCount = argList.size();
    for (int i = startArg; i < argsCount; i++) {
        RegisterArg arg = argList.get(i);
        InsnNode useInsn = getParentInsnSkipMove(arg);
        if (useInsn == null) {
            return Collections.emptyMap();
        }
        switch(useInsn.getType()) {
            case IPUT:
                FieldNode fieldNode = cls.searchField((FieldInfo) ((IndexInsnNode) useInsn).getIndex());
                if (fieldNode == null || !fieldNode.getAccessFlags().isSynthetic()) {
                    return Collections.emptyMap();
                }
                map.put(arg, fieldNode);
                usedInsns.add(useInsn);
                break;
            case CONSTRUCTOR:
                ConstructorInsn superConstr = (ConstructorInsn) useInsn;
                if (!superConstr.isSuper()) {
                    return Collections.emptyMap();
                }
                usedInsns.add(useInsn);
                break;
            default:
                return Collections.emptyMap();
        }
    }
    return map;
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) 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) MethodInfo(jadx.core.dex.info.MethodInfo) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) ConstructorInsn(jadx.core.dex.instructions.mods.ConstructorInsn) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with FieldNode

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

the class ClassModifier method getArgsToFieldsMapping.

private static Map<InsnArg, FieldNode> getArgsToFieldsMapping(MethodNode mth, List<InsnNode> usedInsns) {
    MethodInfo callMth = mth.getMethodInfo();
    ClassNode cls = mth.getParentClass();
    List<RegisterArg> argList = mth.getArgRegs();
    ClassNode outerCls = mth.getUseIn().get(0).getParentClass();
    int startArg = 0;
    if (callMth.getArgsCount() != 0 && callMth.getArgumentsTypes().get(0).equals(outerCls.getClassInfo().getType())) {
        startArg = 1;
    }
    Map<InsnArg, FieldNode> map = new LinkedHashMap<>();
    int argsCount = argList.size();
    for (int i = startArg; i < argsCount; i++) {
        RegisterArg arg = argList.get(i);
        InsnNode useInsn = getParentInsnSkipMove(arg);
        if (useInsn == null) {
            return Collections.emptyMap();
        }
        switch(useInsn.getType()) {
            case IPUT:
                FieldNode fieldNode = cls.searchField((FieldInfo) ((IndexInsnNode) useInsn).getIndex());
                if (fieldNode == null || !fieldNode.getAccessFlags().isSynthetic()) {
                    return Collections.emptyMap();
                }
                map.put(arg, fieldNode);
                usedInsns.add(useInsn);
                break;
            case CONSTRUCTOR:
                ConstructorInsn superConstr = (ConstructorInsn) useInsn;
                if (!superConstr.isSuper()) {
                    return Collections.emptyMap();
                }
                usedInsns.add(useInsn);
                break;
            default:
                return Collections.emptyMap();
        }
    }
    return map;
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) 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) MethodInfo(jadx.core.dex.info.MethodInfo) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) ConstructorInsn(jadx.core.dex.instructions.mods.ConstructorInsn) LinkedHashMap(java.util.LinkedHashMap)

Example 33 with FieldNode

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

the class ClassModifier method removeSyntheticFields.

/**
 * Remove synthetic fields if type is outer class or class will be inlined (anonymous)
 */
private static void removeSyntheticFields(ClassNode cls) {
    boolean inline = cls.isAnonymous();
    if (inline || cls.getClassInfo().isInner()) {
        for (FieldNode field : cls.getFields()) {
            if (field.getAccessFlags().isSynthetic() && field.getType().isObject()) {
                ClassInfo clsInfo = ClassInfo.fromType(cls.root(), field.getType());
                ClassNode fieldsCls = cls.root().resolveClass(clsInfo);
                ClassInfo parentClass = cls.getClassInfo().getParentClass();
                if (fieldsCls != null && (inline || Objects.equals(parentClass, fieldsCls.getClassInfo()))) {
                    int found = 0;
                    for (MethodNode mth : cls.getMethods()) {
                        if (removeFieldUsageFromConstructor(mth, field, fieldsCls)) {
                            found++;
                        }
                    }
                    if (found != 0) {
                        field.addAttr(new FieldReplaceAttr(fieldsCls.getClassInfo()));
                        field.add(AFlag.DONT_GENERATE);
                    }
                }
            }
        }
    }
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) FieldNode(jadx.core.dex.nodes.FieldNode) MethodNode(jadx.core.dex.nodes.MethodNode) FieldReplaceAttr(jadx.core.dex.attributes.nodes.FieldReplaceAttr) ClassInfo(jadx.core.dex.info.ClassInfo)

Example 34 with FieldNode

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

the class EnumVisitor method processEnumFieldByWrappedInsn.

private EnumField processEnumFieldByWrappedInsn(ClassNode cls, InsnNode wrappedInsn, BlockNode staticBlock, List<InsnNode> toRemove) {
    if (wrappedInsn.getType() == InsnType.SGET) {
        return processEnumFieldByField(cls, wrappedInsn, staticBlock, toRemove);
    }
    ConstructorInsn constructorInsn = castConstructorInsn(wrappedInsn);
    if (constructorInsn != null) {
        FieldNode enumFieldNode = createFakeField(cls, "EF" + constructorInsn.getOffset());
        cls.addField(enumFieldNode);
        return createEnumFieldByConstructor(cls, enumFieldNode, constructorInsn);
    }
    return null;
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) ConstructorInsn(jadx.core.dex.instructions.mods.ConstructorInsn)

Example 35 with FieldNode

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

the class TestRFieldRestore method test.

@Test
public void test() {
    // unknown R class
    disableCompilation();
    Map<Integer, String> map = new HashMap<>();
    int buttonConstValue = 2131230730;
    map.put(buttonConstValue, "id.Button");
    setResMap(map);
    ClassNode cls = getClassNode(TestCls.class);
    String code = cls.getCode().toString();
    assertThat(code, containsOne("return R.id.Button;"));
    assertThat(code, not(containsString("import R;")));
    // check 'R' class
    ClassNode rCls = cls.root().searchClassByFullAlias("R");
    assertThat(rCls, notNullValue());
    // check inner 'id' class
    List<ClassNode> innerClasses = rCls.getInnerClasses();
    assertThat(innerClasses, hasSize(1));
    ClassNode idCls = innerClasses.get(0);
    assertThat(idCls.getShortName(), is("id"));
    // check 'Button' field
    FieldNode buttonField = idCls.searchFieldByName("Button");
    assertThat(buttonField, notNullValue());
    EncodedValue constVal = buttonField.get(JadxAttrType.CONSTANT_VALUE);
    Integer buttonValue = (Integer) constVal.getValue();
    assertThat(buttonValue, is(buttonConstValue));
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) FieldNode(jadx.core.dex.nodes.FieldNode) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test) IntegrationTest(jadx.tests.api.IntegrationTest)

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