Search in sources :

Example 6 with MethodNode

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

the class InsnGen method inlineAnonymousConstr.

private void inlineAnonymousConstr(CodeWriter code, ClassNode cls, ConstructorInsn insn) throws CodegenException {
    // anonymous class construction
    if (cls.contains(AFlag.DONT_GENERATE)) {
        code.add("/* anonymous class already generated */");
        ErrorsCounter.methodError(mth, "Anonymous class already generated: " + cls);
        return;
    }
    ArgType parent;
    if (cls.getInterfaces().size() == 1) {
        parent = cls.getInterfaces().get(0);
    } else {
        parent = cls.getSuperClass();
    }
    cls.add(AFlag.DONT_GENERATE);
    MethodNode defCtr = cls.getDefaultConstructor();
    if (defCtr != null) {
        if (RegionUtils.notEmpty(defCtr.getRegion())) {
            defCtr.add(AFlag.ANONYMOUS_CONSTRUCTOR);
        } else {
            defCtr.add(AFlag.DONT_GENERATE);
        }
    }
    code.add("new ");
    if (parent == null) {
        code.add("Object");
    } else {
        useClass(code, parent);
    }
    MethodNode callMth = mth.dex().resolveMethod(insn.getCallMth());
    generateMethodArguments(code, insn, 0, callMth);
    code.add(' ');
    new ClassGen(cls, mgen.getClassGen().getParentGen()).addClassBody(code);
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) MethodNode(jadx.core.dex.nodes.MethodNode)

Example 7 with MethodNode

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

the class InsnGen method makeInvoke.

private void makeInvoke(InvokeNode insn, CodeWriter code) throws CodegenException {
    MethodInfo callMth = insn.getCallMth();
    // inline method
    MethodNode callMthNode = mth.dex().deepResolveMethod(callMth);
    if (callMthNode != null) {
        if (inlineMethod(callMthNode, insn, code)) {
            return;
        }
        callMth = callMthNode.getMethodInfo();
    }
    int k = 0;
    InvokeType type = insn.getInvokeType();
    switch(type) {
        case DIRECT:
        case VIRTUAL:
        case INTERFACE:
            InsnArg arg = insn.getArg(0);
            // FIXME: add 'this' for equals methods in scope
            if (!arg.isThis()) {
                addArgDot(code, arg);
            }
            k++;
            break;
        case SUPER:
            // use 'super' instead 'this' in 0 arg
            code.add("super").add('.');
            k++;
            break;
        case STATIC:
            ClassInfo insnCls = mth.getParentClass().getAlias();
            ClassInfo declClass = callMth.getDeclClass();
            if (!insnCls.equals(declClass)) {
                useClass(code, declClass);
                code.add('.');
            }
            break;
    }
    if (callMthNode != null) {
        code.attachAnnotation(callMthNode);
    }
    code.add(callMth.getAlias());
    generateMethodArguments(code, insn, k, callMthNode);
}
Also used : MethodNode(jadx.core.dex.nodes.MethodNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) InvokeType(jadx.core.dex.instructions.InvokeType) MethodInfo(jadx.core.dex.info.MethodInfo) ClassInfo(jadx.core.dex.info.ClassInfo)

Example 8 with MethodNode

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

the class ClassModifier method removeSyntheticFields.

private static void removeSyntheticFields(ClassNode cls) {
    if (!cls.getClassInfo().isInner() || cls.getAccessFlags().isStatic()) {
        return;
    }
    // remove fields if it is synthetic and type is a outer class
    for (FieldNode field : cls.getFields()) {
        if (field.getAccessFlags().isSynthetic() && field.getType().isObject()) {
            ClassInfo clsInfo = ClassInfo.fromType(cls.dex(), field.getType());
            ClassNode fieldsCls = cls.dex().resolveClass(clsInfo);
            ClassInfo parentClass = cls.getClassInfo().getParentClass();
            if (fieldsCls != null && parentClass.equals(fieldsCls.getClassInfo()) && field.getName().startsWith("this$")) /* TODO: don't check name */
            {
                int found = 0;
                for (MethodNode mth : cls.getMethods()) {
                    if (removeFieldUsageFromConstructor(mth, field, fieldsCls)) {
                        found++;
                    }
                }
                if (found != 0) {
                    field.addAttr(new FieldReplaceAttr(parentClass));
                    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 9 with MethodNode

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

the class JavaClass method load.

private void load() {
    JadxDecompiler rootDecompiler = getRootDecompiler();
    int inClsCount = cls.getInnerClasses().size();
    if (inClsCount != 0) {
        List<JavaClass> list = new ArrayList<JavaClass>(inClsCount);
        for (ClassNode inner : cls.getInnerClasses()) {
            if (!inner.contains(AFlag.DONT_GENERATE)) {
                JavaClass javaClass = new JavaClass(inner, this);
                javaClass.load();
                list.add(javaClass);
                rootDecompiler.getClassesMap().put(inner, javaClass);
            }
        }
        this.innerClasses = Collections.unmodifiableList(list);
    }
    int fieldsCount = cls.getFields().size();
    if (fieldsCount != 0) {
        List<JavaField> flds = new ArrayList<JavaField>(fieldsCount);
        for (FieldNode f : cls.getFields()) {
            if (!f.contains(AFlag.DONT_GENERATE)) {
                JavaField javaField = new JavaField(f, this);
                flds.add(javaField);
                rootDecompiler.getFieldsMap().put(f, javaField);
            }
        }
        this.fields = Collections.unmodifiableList(flds);
    }
    int methodsCount = cls.getMethods().size();
    if (methodsCount != 0) {
        List<JavaMethod> mths = new ArrayList<JavaMethod>(methodsCount);
        for (MethodNode m : cls.getMethods()) {
            if (!m.contains(AFlag.DONT_GENERATE)) {
                JavaMethod javaMethod = new JavaMethod(this, m);
                mths.add(javaMethod);
                rootDecompiler.getMethodsMap().put(m, javaMethod);
            }
        }
        Collections.sort(mths, new Comparator<JavaMethod>() {

            @Override
            public int compare(JavaMethod o1, JavaMethod o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        this.methods = Collections.unmodifiableList(mths);
    }
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) FieldNode(jadx.core.dex.nodes.FieldNode) ArrayList(java.util.ArrayList) MethodNode(jadx.core.dex.nodes.MethodNode)

Example 10 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)

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