Search in sources :

Example 1 with FieldInitInsnAttr

use of jadx.core.dex.attributes.FieldInitInsnAttr in project jadx by skylot.

the class ExtractFieldInit method addFieldInitAttr.

private static void addFieldInitAttr(MethodNode mth, FieldNode field, InsnNode insn) {
    InsnNode assignInsn = InsnNode.wrapArg(insn.getArg(0));
    field.addAttr(new FieldInitInsnAttr(mth, assignInsn));
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) FieldInitInsnAttr(jadx.core.dex.attributes.FieldInitInsnAttr)

Example 2 with FieldInitInsnAttr

use of jadx.core.dex.attributes.FieldInitInsnAttr in project jadx by skylot.

the class ClassGen method addField.

public void addField(ICodeWriter code, FieldNode f) {
    if (f.contains(AFlag.DONT_GENERATE)) {
        return;
    }
    if (Consts.DEBUG_USAGE) {
        addFieldUsageInfo(code, f);
    }
    CodeGenUtils.addComments(code, f);
    annotationGen.addForField(code, f);
    boolean addInfoComments = f.checkCommentsLevel(CommentsLevel.INFO);
    if (f.getFieldInfo().isRenamed() && addInfoComments) {
        code.newLine();
        CodeGenUtils.addRenamedComment(code, f, f.getName());
    }
    code.startLine(f.getAccessFlags().makeString(addInfoComments));
    useType(code, f.getType());
    code.add(' ');
    code.attachDefinition(f);
    code.add(f.getAlias());
    FieldInitInsnAttr initInsnAttr = f.get(AType.FIELD_INIT_INSN);
    if (initInsnAttr != null) {
        InsnGen insnGen = makeInsnGen(initInsnAttr.getInsnMth());
        code.add(" = ");
        addInsnBody(insnGen, code, initInsnAttr.getInsn());
    } else {
        EncodedValue constVal = f.get(JadxAttrType.CONSTANT_VALUE);
        if (constVal != null) {
            code.add(" = ");
            if (constVal.getType() == EncodedType.ENCODED_NULL) {
                code.add(TypeGen.literalToString(0, f.getType(), cls, fallback));
            } else {
                Object val = EncodedValueUtils.convertToConstValue(constVal);
                if (val instanceof LiteralArg) {
                    long lit = ((LiteralArg) val).getLiteral();
                    if (!AndroidResourcesUtils.handleResourceFieldValue(cls, code, lit, f.getType())) {
                        // force literal type to be same as field (java bytecode can use different type)
                        code.add(TypeGen.literalToString(lit, f.getType(), cls, fallback));
                    }
                } else {
                    annotationGen.encodeValue(cls.root(), code, constVal);
                }
            }
        }
    }
    code.add(';');
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) FieldInitInsnAttr(jadx.core.dex.attributes.FieldInitInsnAttr) LiteralArg(jadx.core.dex.instructions.args.LiteralArg)

Aggregations

FieldInitInsnAttr (jadx.core.dex.attributes.FieldInitInsnAttr)2 EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)1 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)1 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)1 InsnNode (jadx.core.dex.nodes.InsnNode)1