Search in sources :

Example 41 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 42 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)

Example 43 with FieldNode

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

the class ReSugarCode method checkEnumMapAccess.

public static EnumMapInfo checkEnumMapAccess(MethodNode mth, InsnNode checkInsn) {
    InsnArg sgetArg = checkInsn.getArg(0);
    InsnArg invArg = checkInsn.getArg(1);
    if (!sgetArg.isInsnWrap() || !invArg.isInsnWrap()) {
        return null;
    }
    InsnNode invInsn = ((InsnWrapArg) invArg).getWrapInsn();
    InsnNode sgetInsn = ((InsnWrapArg) sgetArg).getWrapInsn();
    if (invInsn.getType() != InsnType.INVOKE || sgetInsn.getType() != InsnType.SGET) {
        return null;
    }
    InvokeNode inv = (InvokeNode) invInsn;
    if (!inv.getCallMth().getShortId().equals("ordinal()I")) {
        return null;
    }
    ClassNode enumCls = mth.dex().resolveClass(inv.getCallMth().getDeclClass());
    if (enumCls == null || !enumCls.isEnum()) {
        return null;
    }
    Object index = ((IndexInsnNode) sgetInsn).getIndex();
    if (!(index instanceof FieldInfo)) {
        return null;
    }
    FieldNode enumMapField = mth.dex().resolveField((FieldInfo) index);
    if (enumMapField == null || !enumMapField.getAccessFlags().isSynthetic()) {
        return null;
    }
    return new EnumMapInfo(inv.getArg(0), enumMapField);
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) ClassNode(jadx.core.dex.nodes.ClassNode) FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) InvokeNode(jadx.core.dex.instructions.InvokeNode) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) FieldInfo(jadx.core.dex.info.FieldInfo)

Example 44 with FieldNode

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

the class ReSugarCode method addToEnumMap.

private static void addToEnumMap(MethodNode mth, EnumMapAttr mapAttr, InsnNode aputInsn) {
    InsnArg litArg = aputInsn.getArg(2);
    if (!litArg.isLiteral()) {
        return;
    }
    EnumMapInfo mapInfo = checkEnumMapAccess(mth, aputInsn);
    if (mapInfo == null) {
        return;
    }
    InsnArg enumArg = mapInfo.getArg();
    FieldNode field = mapInfo.getMapField();
    if (field == null || !enumArg.isInsnWrap()) {
        return;
    }
    InsnNode sget = ((InsnWrapArg) enumArg).getWrapInsn();
    if (!(sget instanceof IndexInsnNode)) {
        return;
    }
    Object index = ((IndexInsnNode) sget).getIndex();
    if (!(index instanceof FieldInfo)) {
        return;
    }
    FieldNode fieldNode = mth.dex().resolveField((FieldInfo) index);
    if (fieldNode == null) {
        return;
    }
    int literal = (int) ((LiteralArg) litArg).getLiteral();
    mapAttr.add(field, literal, fieldNode);
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) FieldInfo(jadx.core.dex.info.FieldInfo)

Example 45 with FieldNode

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

the class ReSugarCode method processEnumSwitch.

private static InsnNode processEnumSwitch(MethodNode mth, SwitchNode insn) {
    InsnArg arg = insn.getArg(0);
    if (!arg.isInsnWrap()) {
        return null;
    }
    InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
    if (wrapInsn.getType() != InsnType.AGET) {
        return null;
    }
    EnumMapInfo enumMapInfo = checkEnumMapAccess(mth, wrapInsn);
    if (enumMapInfo == null) {
        return null;
    }
    FieldNode enumMapField = enumMapInfo.getMapField();
    InsnArg invArg = enumMapInfo.getArg();
    EnumMapAttr.KeyValueMap valueMap = getEnumMap(mth, enumMapField);
    if (valueMap == null) {
        return null;
    }
    Object[] keys = insn.getKeys();
    for (Object key : keys) {
        Object newKey = valueMap.get(key);
        if (newKey == null) {
            return null;
        }
    }
    // replace confirmed
    if (!insn.replaceArg(arg, invArg)) {
        return null;
    }
    for (int i = 0; i < keys.length; i++) {
        keys[i] = valueMap.get(keys[i]);
    }
    enumMapField.add(AFlag.DONT_GENERATE);
    checkAndHideClass(enumMapField.getParentClass());
    return null;
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) EnumMapAttr(jadx.core.dex.attributes.nodes.EnumMapAttr) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg)

Aggregations

FieldNode (jadx.core.dex.nodes.FieldNode)70 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)30 InsnNode (jadx.core.dex.nodes.InsnNode)28 ClassNode (jadx.core.dex.nodes.ClassNode)26 FieldInfo (jadx.core.dex.info.FieldInfo)23 InsnArg (jadx.core.dex.instructions.args.InsnArg)20 MethodNode (jadx.core.dex.nodes.MethodNode)14 ArrayList (java.util.ArrayList)11 ArgType (jadx.core.dex.instructions.args.ArgType)10 InsnWrapArg (jadx.core.dex.instructions.args.InsnWrapArg)10 Nullable (org.jetbrains.annotations.Nullable)10 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)9 ClassInfo (jadx.core.dex.info.ClassInfo)6 ConstClassNode (jadx.core.dex.instructions.ConstClassNode)6 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)6 FieldReplaceAttr (jadx.core.dex.attributes.nodes.FieldReplaceAttr)5 MethodInfo (jadx.core.dex.info.MethodInfo)5 InsnType (jadx.core.dex.instructions.InsnType)5 ConstructorInsn (jadx.core.dex.instructions.mods.ConstructorInsn)5 BlockNode (jadx.core.dex.nodes.BlockNode)4