Search in sources :

Example 21 with MethodNode

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

the class ExtractFieldInit method moveCommonFieldsInit.

private static void moveCommonFieldsInit(ClassNode cls) {
    List<MethodNode> constrList = getConstructorsList(cls);
    if (constrList.isEmpty()) {
        return;
    }
    List<InitInfo> infoList = new ArrayList<InitInfo>(constrList.size());
    for (MethodNode constrMth : constrList) {
        if (constrMth.isNoCode() || constrMth.getBasicBlocks().isEmpty()) {
            return;
        }
        InitInfo info = new InitInfo(constrMth);
        infoList.add(info);
        // TODO: check not only first block
        BlockNode blockNode = constrMth.getBasicBlocks().get(0);
        for (InsnNode insn : blockNode.getInstructions()) {
            if (insn.getType() == InsnType.IPUT && checkInsn(insn)) {
                info.getPutInsns().add(insn);
            } else if (!info.getPutInsns().isEmpty()) {
                break;
            }
        }
    }
    // compare collected instructions
    InitInfo common = null;
    for (InitInfo info : infoList) {
        if (common == null) {
            common = info;
        } else if (!compareInsns(common.getPutInsns(), info.getPutInsns())) {
            return;
        }
    }
    if (common == null) {
        return;
    }
    Set<FieldInfo> fields = new HashSet<FieldInfo>();
    for (InsnNode insn : common.getPutInsns()) {
        FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) insn).getIndex();
        FieldNode field = cls.dex().resolveField(fieldInfo);
        if (field == null) {
            return;
        }
        if (!fields.add(fieldInfo)) {
            return;
        }
    }
    // all checks passed
    for (InitInfo info : infoList) {
        for (InsnNode putInsn : info.getPutInsns()) {
            InstructionRemover.remove(info.getConstrMth(), putInsn);
        }
    }
    for (InsnNode insn : common.getPutInsns()) {
        FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) insn).getIndex();
        FieldNode field = cls.dex().resolveField(fieldInfo);
        addFieldInitAttr(common.getConstrMth(), field, insn);
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) MethodNode(jadx.core.dex.nodes.MethodNode) FieldNode(jadx.core.dex.nodes.FieldNode) ArrayList(java.util.ArrayList) FieldInfo(jadx.core.dex.info.FieldInfo) HashSet(java.util.HashSet)

Example 22 with MethodNode

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

the class ExtractFieldInit method moveStaticFieldsInit.

private static void moveStaticFieldsInit(ClassNode cls) {
    MethodNode classInitMth = cls.getClassInitMth();
    if (classInitMth == null) {
        return;
    }
    for (FieldNode field : cls.getFields()) {
        if (field.contains(AFlag.DONT_GENERATE)) {
            continue;
        }
        if (field.getAccessFlags().isStatic()) {
            List<InsnNode> initInsns = getFieldAssigns(classInitMth, field, InsnType.SPUT);
            if (initInsns.size() == 1) {
                InsnNode insn = initInsns.get(0);
                if (checkInsn(insn)) {
                    InstructionRemover.remove(classInitMth, insn);
                    addFieldInitAttr(classInitMth, field, insn);
                }
            }
        }
    }
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) MethodNode(jadx.core.dex.nodes.MethodNode) FieldNode(jadx.core.dex.nodes.FieldNode)

Example 23 with MethodNode

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

the class ClassModifier method removeSyntheticMethods.

private static void removeSyntheticMethods(ClassNode cls) {
    for (MethodNode mth : cls.getMethods()) {
        if (mth.isNoCode()) {
            continue;
        }
        AccessInfo af = mth.getAccessFlags();
        // remove bridge methods
        if (af.isBridge() && af.isSynthetic() && !isMethodUniq(cls, mth)) {
            // TODO add more checks before method deletion
            mth.add(AFlag.DONT_GENERATE);
            continue;
        }
        // remove synthetic constructor for inner classes
        if (af.isSynthetic() && af.isConstructor() && mth.getBasicBlocks().size() == 2) {
            List<InsnNode> insns = mth.getBasicBlocks().get(0).getInstructions();
            if (insns.size() == 1 && insns.get(0).getType() == InsnType.CONSTRUCTOR) {
                ConstructorInsn constr = (ConstructorInsn) insns.get(0);
                List<RegisterArg> args = mth.getArguments(false);
                if (constr.isThis() && !args.isEmpty()) {
                    // remove first arg for non-static class (references to outer class)
                    if (args.get(0).getType().equals(cls.getParentClass().getClassInfo().getType())) {
                        args.get(0).add(AFlag.SKIP_ARG);
                    }
                    // remove unused args
                    for (RegisterArg arg : args) {
                        SSAVar sVar = arg.getSVar();
                        if (sVar != null && sVar.getUseCount() == 0) {
                            arg.add(AFlag.SKIP_ARG);
                        }
                    }
                    mth.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) MethodNode(jadx.core.dex.nodes.MethodNode) SSAVar(jadx.core.dex.instructions.args.SSAVar) AccessInfo(jadx.core.dex.info.AccessInfo) ConstructorInsn(jadx.core.dex.instructions.mods.ConstructorInsn)

Example 24 with MethodNode

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

the class EnumVisitor method processEnumInnerCls.

private static void processEnumInnerCls(ConstructorInsn co, EnumField field, ClassNode innerCls) {
    if (!innerCls.getClassInfo().equals(co.getClassType())) {
        return;
    }
    // remove constructor, because it is anonymous class
    for (MethodNode innerMth : innerCls.getMethods()) {
        if (innerMth.getAccessFlags().isConstructor()) {
            innerMth.add(AFlag.DONT_GENERATE);
        }
    }
    field.setCls(innerCls);
    innerCls.add(AFlag.DONT_GENERATE);
}
Also used : MethodNode(jadx.core.dex.nodes.MethodNode)

Example 25 with MethodNode

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

the class Deobfuscator method processClass.

private void processClass(DexNode dex, ClassNode cls) {
    ClassInfo clsInfo = cls.getClassInfo();
    String fullName = getClassFullName(clsInfo);
    if (!fullName.equals(clsInfo.getFullName())) {
        clsInfo.rename(dex, fullName);
    }
    for (FieldNode field : cls.getFields()) {
        FieldInfo fieldInfo = field.getFieldInfo();
        String alias = getFieldAlias(field);
        if (alias != null) {
            fieldInfo.setAlias(alias);
        }
    }
    for (MethodNode mth : cls.getMethods()) {
        MethodInfo methodInfo = mth.getMethodInfo();
        String alias = getMethodAlias(mth);
        if (alias != null) {
            methodInfo.setAlias(alias);
        }
        if (mth.isVirtual()) {
            resolveOverriding(dex, cls, mth);
        }
    }
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) MethodNode(jadx.core.dex.nodes.MethodNode) MethodInfo(jadx.core.dex.info.MethodInfo) FieldInfo(jadx.core.dex.info.FieldInfo) ClassInfo(jadx.core.dex.info.ClassInfo)

Aggregations

MethodNode (jadx.core.dex.nodes.MethodNode)27 ClassNode (jadx.core.dex.nodes.ClassNode)12 FieldNode (jadx.core.dex.nodes.FieldNode)9 MethodInfo (jadx.core.dex.info.MethodInfo)7 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)7 InsnNode (jadx.core.dex.nodes.InsnNode)7 ClassInfo (jadx.core.dex.info.ClassInfo)5 ConstructorInsn (jadx.core.dex.instructions.mods.ConstructorInsn)5 BlockNode (jadx.core.dex.nodes.BlockNode)5 ConstClassNode (jadx.core.dex.instructions.ConstClassNode)4 ArgType (jadx.core.dex.instructions.args.ArgType)4 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)4 ArrayList (java.util.ArrayList)4 FieldInfo (jadx.core.dex.info.FieldInfo)3 InsnArg (jadx.core.dex.instructions.args.InsnArg)3 IRegion (jadx.core.dex.nodes.IRegion)3 IntegrationTest (jadx.tests.api.IntegrationTest)3 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 EnumClassAttr (jadx.core.dex.attributes.nodes.EnumClassAttr)2