Search in sources :

Example 11 with FieldNode

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

the class RegionGen method makeSwitch.

private CodeWriter makeSwitch(SwitchRegion sw, CodeWriter code) throws CodegenException {
    SwitchNode insn = (SwitchNode) sw.getHeader().getInstructions().get(0);
    InsnArg arg = insn.getArg(0);
    code.startLine("switch (");
    addArg(code, arg, false);
    code.add(") {");
    code.incIndent();
    int size = sw.getKeys().size();
    for (int i = 0; i < size; i++) {
        List<Object> keys = sw.getKeys().get(i);
        IContainer c = sw.getCases().get(i);
        for (Object k : keys) {
            code.startLine("case ");
            if (k instanceof FieldNode) {
                FieldNode fn = (FieldNode) k;
                if (fn.getParentClass().isEnum()) {
                    code.add(fn.getAlias());
                } else {
                    staticField(code, fn.getFieldInfo());
                    // print original value, sometimes replace with incorrect field
                    FieldInitAttr valueAttr = fn.get(AType.FIELD_INIT);
                    if (valueAttr != null && valueAttr.getValue() != null) {
                        code.add(" /*").add(valueAttr.getValue().toString()).add("*/");
                    }
                }
            } else if (k instanceof Integer) {
                code.add(TypeGen.literalToString((Integer) k, arg.getType(), mth));
            } else {
                throw new JadxRuntimeException("Unexpected key in switch: " + (k != null ? k.getClass() : null));
            }
            code.add(':');
        }
        makeRegionIndent(code, c);
    }
    if (sw.getDefaultCase() != null) {
        code.startLine("default:");
        makeRegionIndent(code, sw.getDefaultCase());
    }
    code.decIndent();
    code.startLine('}');
    return code;
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IContainer(jadx.core.dex.nodes.IContainer) SwitchNode(jadx.core.dex.instructions.SwitchNode) FieldInitAttr(jadx.core.dex.nodes.parser.FieldInitAttr)

Example 12 with FieldNode

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

the class InsnGen method instanceField.

private void instanceField(CodeWriter code, FieldInfo field, InsnArg arg) throws CodegenException {
    ClassNode pCls = mth.getParentClass();
    FieldNode fieldNode = pCls.searchField(field);
    while (fieldNode == null && pCls.getParentClass() != pCls && pCls.getParentClass() != null) {
        pCls = pCls.getParentClass();
        fieldNode = pCls.searchField(field);
    }
    if (fieldNode != null) {
        FieldReplaceAttr replace = fieldNode.get(AType.FIELD_REPLACE);
        if (replace != null) {
            switch(replace.getReplaceType()) {
                case CLASS_INSTANCE:
                    useClass(code, replace.getClsRef());
                    code.add(".this");
                    break;
                case VAR:
                    addArg(code, replace.getVarRef());
                    break;
            }
            return;
        }
    }
    addArgDot(code, arg);
    if (fieldNode != null) {
        code.attachAnnotation(fieldNode);
    }
    code.add(field.getAlias());
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) FieldNode(jadx.core.dex.nodes.FieldNode) FieldReplaceAttr(jadx.core.dex.attributes.nodes.FieldReplaceAttr)

Example 13 with FieldNode

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

the class InsnGen method makeStaticFieldAccess.

public static void makeStaticFieldAccess(CodeWriter code, FieldInfo field, ClassGen clsGen) {
    ClassInfo declClass = field.getDeclClass();
    boolean fieldFromThisClass = clsGen.getClassNode().getClassInfo().equals(declClass);
    if (!fieldFromThisClass) {
        // Android specific resources class handler
        if (!handleAppResField(code, clsGen, declClass)) {
            clsGen.useClass(code, declClass);
        }
        code.add('.');
    }
    FieldNode fieldNode = clsGen.getClassNode().dex().resolveField(field);
    if (fieldNode != null) {
        code.attachAnnotation(fieldNode);
    }
    code.add(field.getAlias());
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) ClassInfo(jadx.core.dex.info.ClassInfo)

Example 14 with FieldNode

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

the class ClassGen method addFields.

private void addFields(CodeWriter code) throws CodegenException {
    addEnumFields(code);
    for (FieldNode f : cls.getFields()) {
        if (f.contains(AFlag.DONT_GENERATE)) {
            continue;
        }
        annotationGen.addForField(code, f);
        code.startLine(f.getAccessFlags().makeString());
        useType(code, f.getType());
        code.add(' ');
        code.attachDefinition(f);
        code.add(f.getAlias());
        FieldInitAttr fv = f.get(AType.FIELD_INIT);
        if (fv != null) {
            code.add(" = ");
            if (fv.getValue() == null) {
                code.add(TypeGen.literalToString(0, f.getType(), cls));
            } else {
                if (fv.getValueType() == InitType.CONST) {
                    annotationGen.encodeValue(code, fv.getValue());
                } else if (fv.getValueType() == InitType.INSN) {
                    InsnGen insnGen = makeInsnGen(fv.getInsnMth());
                    addInsnBody(insnGen, code, fv.getInsn());
                }
            }
        }
        code.add(';');
    }
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) FieldInitAttr(jadx.core.dex.nodes.parser.FieldInitAttr)

Example 15 with FieldNode

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

the class ModVisitor method makeFilledArrayInsn.

private static InsnNode makeFilledArrayInsn(MethodNode mth, FillArrayNode insn) {
    ArgType insnArrayType = insn.getResult().getType();
    ArgType insnElementType = insnArrayType.getArrayElement();
    ArgType elType = insn.getElementType();
    if (!elType.isTypeKnown() && insnElementType.isPrimitive()) {
        if (elType.contains(insnElementType.getPrimitiveType())) {
            elType = insnElementType;
        }
    }
    if (!elType.equals(insnElementType) && !insnArrayType.equals(ArgType.OBJECT)) {
        ErrorsCounter.methodError(mth, "Incorrect type for fill-array insn " + InsnUtils.formatOffset(insn.getOffset()) + ", element type: " + elType + ", insn element type: " + insnElementType);
    }
    if (!elType.isTypeKnown()) {
        LOG.warn("Unknown array element type: {} in mth: {}", elType, mth);
        elType = insnElementType.isTypeKnown() ? insnElementType : elType.selectFirst();
        if (elType == null) {
            throw new JadxRuntimeException("Null array element type");
        }
    }
    insn.mergeElementType(mth.dex(), elType);
    elType = insn.getElementType();
    List<LiteralArg> list = insn.getLiteralArgs();
    InsnNode filledArr = new FilledNewArrayNode(elType, list.size());
    filledArr.setResult(insn.getResult());
    for (LiteralArg arg : list) {
        FieldNode f = mth.getParentClass().getConstFieldByLiteralArg(arg);
        if (f != null) {
            InsnNode fGet = new IndexInsnNode(InsnType.SGET, f.getFieldInfo(), 0);
            filledArr.addArg(InsnArg.wrapArg(fGet));
        } else {
            filledArr.addArg(arg);
        }
    }
    return filledArr;
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) FieldNode(jadx.core.dex.nodes.FieldNode) FilledNewArrayNode(jadx.core.dex.instructions.FilledNewArrayNode) LiteralArg(jadx.core.dex.instructions.args.LiteralArg) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode)

Aggregations

FieldNode (jadx.core.dex.nodes.FieldNode)24 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)10 ClassNode (jadx.core.dex.nodes.ClassNode)10 InsnNode (jadx.core.dex.nodes.InsnNode)10 MethodNode (jadx.core.dex.nodes.MethodNode)9 FieldInfo (jadx.core.dex.info.FieldInfo)8 InsnArg (jadx.core.dex.instructions.args.InsnArg)7 ClassInfo (jadx.core.dex.info.ClassInfo)5 ConstClassNode (jadx.core.dex.instructions.ConstClassNode)5 ArgType (jadx.core.dex.instructions.args.ArgType)4 ArrayList (java.util.ArrayList)4 FieldReplaceAttr (jadx.core.dex.attributes.nodes.FieldReplaceAttr)3 MethodInfo (jadx.core.dex.info.MethodInfo)3 InsnWrapArg (jadx.core.dex.instructions.args.InsnWrapArg)3 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)3 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)3 BlockNode (jadx.core.dex.nodes.BlockNode)3 FieldInitAttr (jadx.core.dex.nodes.parser.FieldInitAttr)3 FilledNewArrayNode (jadx.core.dex.instructions.FilledNewArrayNode)2 SwitchNode (jadx.core.dex.instructions.SwitchNode)2