Search in sources :

Example 1 with FieldInitAttr

use of jadx.core.dex.nodes.parser.FieldInitAttr in project jadx by skylot.

the class InsnUtils method getConstValueByInsn.

/**
	 * Return constant value from insn or null if not constant.
	 *
	 * @return LiteralArg, String, ArgType or null
	 */
@Nullable
public static Object getConstValueByInsn(DexNode dex, InsnNode insn) {
    switch(insn.getType()) {
        case CONST:
            return insn.getArg(0);
        case CONST_STR:
            return ((ConstStringNode) insn).getString();
        case CONST_CLASS:
            return ((ConstClassNode) insn).getClsType();
        case SGET:
            FieldInfo f = (FieldInfo) ((IndexInsnNode) insn).getIndex();
            FieldNode fieldNode = dex.resolveField(f);
            if (fieldNode != null) {
                FieldInitAttr attr = fieldNode.get(AType.FIELD_INIT);
                if (attr != null) {
                    return attr.getValue();
                }
            } else {
                LOG.warn("Field {} not found in dex {}", f, dex);
            }
            break;
    }
    return null;
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) ConstStringNode(jadx.core.dex.instructions.ConstStringNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) FieldInfo(jadx.core.dex.info.FieldInfo) FieldInitAttr(jadx.core.dex.nodes.parser.FieldInitAttr) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with FieldInitAttr

use of jadx.core.dex.nodes.parser.FieldInitAttr 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 3 with FieldInitAttr

use of jadx.core.dex.nodes.parser.FieldInitAttr 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)

Aggregations

FieldNode (jadx.core.dex.nodes.FieldNode)3 FieldInitAttr (jadx.core.dex.nodes.parser.FieldInitAttr)3 FieldInfo (jadx.core.dex.info.FieldInfo)1 ConstClassNode (jadx.core.dex.instructions.ConstClassNode)1 ConstStringNode (jadx.core.dex.instructions.ConstStringNode)1 SwitchNode (jadx.core.dex.instructions.SwitchNode)1 InsnArg (jadx.core.dex.instructions.args.InsnArg)1 IContainer (jadx.core.dex.nodes.IContainer)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1 Nullable (org.jetbrains.annotations.Nullable)1