Search in sources :

Example 1 with FieldReplaceAttr

use of jadx.core.dex.attributes.nodes.FieldReplaceAttr in project jadx by skylot.

the class ModVisitor method processAnonymousConstructor.

private static void processAnonymousConstructor(MethodNode mth, ConstructorInsn co) {
    MethodInfo callMth = co.getCallMth();
    MethodNode callMthNode = mth.dex().resolveMethod(callMth);
    if (callMthNode == null) {
        return;
    }
    ClassNode classNode = callMthNode.getParentClass();
    ClassInfo classInfo = classNode.getClassInfo();
    ClassNode parentClass = mth.getParentClass();
    if (!classInfo.isInner() || !Character.isDigit(classInfo.getShortName().charAt(0)) || !parentClass.getInnerClasses().contains(classNode)) {
        return;
    }
    if (!classNode.getAccessFlags().isStatic() && (callMth.getArgsCount() == 0 || !callMth.getArgumentsTypes().get(0).equals(parentClass.getClassInfo().getType()))) {
        return;
    }
    // TODO: calculate this constructor and other constructor usage
    Map<InsnArg, FieldNode> argsMap = getArgsToFieldsMapping(callMthNode, co);
    if (argsMap.isEmpty()) {
        return;
    }
    // all checks passed
    classNode.add(AFlag.ANONYMOUS_CLASS);
    callMthNode.add(AFlag.DONT_GENERATE);
    for (Map.Entry<InsnArg, FieldNode> entry : argsMap.entrySet()) {
        FieldNode field = entry.getValue();
        if (field == null) {
            continue;
        }
        InsnArg arg = entry.getKey();
        field.addAttr(new FieldReplaceAttr(arg));
        field.add(AFlag.DONT_GENERATE);
        if (arg.isRegister()) {
            RegisterArg reg = (RegisterArg) arg;
            SSAVar sVar = reg.getSVar();
            if (sVar != null) {
                sVar.add(AFlag.FINAL);
                sVar.add(AFlag.DONT_INLINE);
            }
            reg.add(AFlag.SKIP_ARG);
        }
    }
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) MethodNode(jadx.core.dex.nodes.MethodNode) FieldNode(jadx.core.dex.nodes.FieldNode) SSAVar(jadx.core.dex.instructions.args.SSAVar) InsnArg(jadx.core.dex.instructions.args.InsnArg) FieldReplaceAttr(jadx.core.dex.attributes.nodes.FieldReplaceAttr) MethodInfo(jadx.core.dex.info.MethodInfo) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ClassInfo(jadx.core.dex.info.ClassInfo)

Example 2 with FieldReplaceAttr

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

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

Aggregations

FieldReplaceAttr (jadx.core.dex.attributes.nodes.FieldReplaceAttr)3 ClassNode (jadx.core.dex.nodes.ClassNode)3 FieldNode (jadx.core.dex.nodes.FieldNode)3 ClassInfo (jadx.core.dex.info.ClassInfo)2 ConstClassNode (jadx.core.dex.instructions.ConstClassNode)2 MethodNode (jadx.core.dex.nodes.MethodNode)2 MethodInfo (jadx.core.dex.info.MethodInfo)1 InsnArg (jadx.core.dex.instructions.args.InsnArg)1 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)1 SSAVar (jadx.core.dex.instructions.args.SSAVar)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1