Search in sources :

Example 1 with MethodNode

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

the class RenameVisitor method checkMethods.

private void checkMethods(ClassNode cls) {
    Set<String> names = new HashSet<String>();
    for (MethodNode mth : cls.getMethods()) {
        if (mth.contains(AFlag.DONT_GENERATE)) {
            continue;
        }
        MethodInfo methodInfo = mth.getMethodInfo();
        String signature = makeMethodSignature(methodInfo);
        if (!names.add(signature)) {
            methodInfo.setAlias(deobfuscator.makeMethodAlias(mth));
        }
    }
}
Also used : MethodNode(jadx.core.dex.nodes.MethodNode) MethodInfo(jadx.core.dex.info.MethodInfo) HashSet(java.util.HashSet)

Example 2 with MethodNode

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

the class ModVisitor method processAnonymousConstructor.

private static void processAnonymousConstructor(MethodNode mth, ConstructorInsn co) {
    MethodInfo callMth = co.getCallMth();
    MethodNode callMthNode = mth.dex().resolveMethod(callMth);
    if (callMthNode == null) {
        return;
    }
    ClassNode classNode = callMthNode.getParentClass();
    ClassInfo classInfo = classNode.getClassInfo();
    ClassNode parentClass = mth.getParentClass();
    if (!classInfo.isInner() || !Character.isDigit(classInfo.getShortName().charAt(0)) || !parentClass.getInnerClasses().contains(classNode)) {
        return;
    }
    if (!classNode.getAccessFlags().isStatic() && (callMth.getArgsCount() == 0 || !callMth.getArgumentsTypes().get(0).equals(parentClass.getClassInfo().getType()))) {
        return;
    }
    // TODO: calculate this constructor and other constructor usage
    Map<InsnArg, FieldNode> argsMap = getArgsToFieldsMapping(callMthNode, co);
    if (argsMap.isEmpty()) {
        return;
    }
    // all checks passed
    classNode.add(AFlag.ANONYMOUS_CLASS);
    callMthNode.add(AFlag.DONT_GENERATE);
    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()) {
            RegisterArg reg = (RegisterArg) arg;
            SSAVar sVar = reg.getSVar();
            if (sVar != null) {
                sVar.add(AFlag.FINAL);
                sVar.add(AFlag.DONT_INLINE);
            }
            reg.add(AFlag.SKIP_ARG);
        }
    }
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) MethodNode(jadx.core.dex.nodes.MethodNode) FieldNode(jadx.core.dex.nodes.FieldNode) SSAVar(jadx.core.dex.instructions.args.SSAVar) InsnArg(jadx.core.dex.instructions.args.InsnArg) FieldReplaceAttr(jadx.core.dex.attributes.nodes.FieldReplaceAttr) MethodInfo(jadx.core.dex.info.MethodInfo) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ClassInfo(jadx.core.dex.info.ClassInfo)

Example 3 with MethodNode

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

the class CheckRegions method visit.

@Override
public void visit(MethodNode mth) throws JadxException {
    if (mth.isNoCode() || mth.getBasicBlocks().isEmpty() || mth.contains(AType.JADX_ERROR)) {
        return;
    }
    // check if all blocks included in regions
    final Set<BlockNode> blocksInRegions = new HashSet<BlockNode>();
    DepthRegionTraversal.traverse(mth, new AbstractRegionVisitor() {

        @Override
        public void processBlock(MethodNode mth, IBlock container) {
            if (!(container instanceof BlockNode)) {
                return;
            }
            BlockNode block = (BlockNode) container;
            if (blocksInRegions.add(block)) {
                return;
            }
            if (!block.contains(AFlag.RETURN) && !block.contains(AFlag.SKIP) && !block.contains(AFlag.SYNTHETIC) && !block.getInstructions().isEmpty()) {
                // TODO
                // mth.add(AFlag.INCONSISTENT_CODE);
                LOG.debug(" Duplicated block: {} in {}", block, mth);
            }
        }
    });
    if (mth.getBasicBlocks().size() != blocksInRegions.size()) {
        for (BlockNode block : mth.getBasicBlocks()) {
            if (!blocksInRegions.contains(block) && !block.getInstructions().isEmpty() && !block.contains(AFlag.SKIP)) {
                mth.add(AFlag.INCONSISTENT_CODE);
                LOG.debug(" Missing block: {} in {}", block, mth);
            }
        }
    }
    // check loop conditions
    DepthRegionTraversal.traverse(mth, new AbstractRegionVisitor() {

        @Override
        public boolean enterRegion(MethodNode mth, IRegion region) {
            if (region instanceof LoopRegion) {
                BlockNode loopHeader = ((LoopRegion) region).getHeader();
                if (loopHeader != null && loopHeader.getInstructions().size() != 1) {
                    ErrorsCounter.methodError(mth, "Incorrect condition in loop: " + loopHeader);
                }
            }
            return true;
        }
    });
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) IBlock(jadx.core.dex.nodes.IBlock) MethodNode(jadx.core.dex.nodes.MethodNode) LoopRegion(jadx.core.dex.regions.loops.LoopRegion) IRegion(jadx.core.dex.nodes.IRegion) HashSet(java.util.HashSet)

Example 4 with MethodNode

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

the class ClassGen method addEnumFields.

private void addEnumFields(CodeWriter code) throws CodegenException {
    EnumClassAttr enumFields = cls.get(AType.ENUM_CLASS);
    if (enumFields == null) {
        return;
    }
    InsnGen igen = null;
    for (Iterator<EnumField> it = enumFields.getFields().iterator(); it.hasNext(); ) {
        EnumField f = it.next();
        code.startLine(f.getField().getAlias());
        ConstructorInsn constrInsn = f.getConstrInsn();
        if (constrInsn.getArgsCount() > f.getStartArg()) {
            if (igen == null) {
                igen = makeInsnGen(enumFields.getStaticMethod());
            }
            MethodNode callMth = cls.dex().resolveMethod(constrInsn.getCallMth());
            igen.generateMethodArguments(code, constrInsn, f.getStartArg(), callMth);
        }
        if (f.getCls() != null) {
            code.add(' ');
            new ClassGen(f.getCls(), this).addClassBody(code);
        }
        if (it.hasNext()) {
            code.add(',');
        }
    }
    if (isMethodsPresents() || isFieldsPresents() || isInnerClassesPresents()) {
        if (enumFields.getFields().isEmpty()) {
            code.startLine();
        }
        code.add(';');
        if (isFieldsPresents()) {
            code.startLine();
        }
    }
}
Also used : EnumField(jadx.core.dex.attributes.nodes.EnumClassAttr.EnumField) MethodNode(jadx.core.dex.nodes.MethodNode) ConstructorInsn(jadx.core.dex.instructions.mods.ConstructorInsn) EnumClassAttr(jadx.core.dex.attributes.nodes.EnumClassAttr)

Example 5 with MethodNode

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

the class InsnGen method makeConstructor.

private void makeConstructor(ConstructorInsn insn, CodeWriter code) throws CodegenException {
    ClassNode cls = mth.dex().resolveClass(insn.getClassType());
    if (cls != null && cls.contains(AFlag.ANONYMOUS_CLASS) && !fallback) {
        inlineAnonymousConstr(code, cls, insn);
        return;
    }
    if (insn.isSelf()) {
        throw new JadxRuntimeException("Constructor 'self' invoke must be removed!");
    }
    if (insn.isSuper()) {
        code.add("super");
    } else if (insn.isThis()) {
        code.add("this");
    } else {
        code.add("new ");
        useClass(code, insn.getClassType());
    }
    MethodNode callMth = mth.dex().resolveMethod(insn.getCallMth());
    generateMethodArguments(code, insn, 0, callMth);
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) MethodNode(jadx.core.dex.nodes.MethodNode) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

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