Search in sources :

Example 1 with MethodOverrideAttr

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

the class MethodGen method addOverrideAnnotation.

private void addOverrideAnnotation(ICodeWriter code, MethodNode mth) {
    MethodOverrideAttr overrideAttr = mth.get(AType.METHOD_OVERRIDE);
    if (overrideAttr == null) {
        return;
    }
    if (!overrideAttr.getBaseMethods().contains(mth)) {
        code.startLine("@Override");
        if (mth.checkCommentsLevel(CommentsLevel.INFO)) {
            code.add(" // ");
            code.add(Utils.listToString(overrideAttr.getOverrideList(), ", ", md -> md.getMethodInfo().getDeclClass().getAliasFullName()));
        }
    }
    if (Consts.DEBUG) {
        code.startLine("// related by override: ");
        code.add(Utils.listToString(overrideAttr.getRelatedMthNodes(), ", ", m -> m.getParentClass().getFullName()));
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) CodeVar(jadx.core.dex.instructions.args.CodeVar) IDexTreeVisitor(jadx.core.dex.visitors.IDexTreeVisitor) JumpInfo(jadx.core.dex.attributes.nodes.JumpInfo) MethodNode(jadx.core.dex.nodes.MethodNode) Consts(jadx.core.Consts) AType(jadx.core.dex.attributes.AType) AFlag(jadx.core.dex.attributes.AFlag) LoggerFactory(org.slf4j.LoggerFactory) InsnType(jadx.core.dex.instructions.InsnType) CatchAttr(jadx.core.dex.trycatch.CatchAttr) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException) AccessInfo(jadx.core.dex.info.AccessInfo) InsnCodeOffset(jadx.api.data.annotations.InsnCodeOffset) IfNode(jadx.core.dex.instructions.IfNode) InsnUtils(jadx.core.utils.InsnUtils) CodeGenUtils(jadx.core.utils.CodeGenUtils) CommentsLevel(jadx.api.CommentsLevel) InsnNode(jadx.core.dex.nodes.InsnNode) SSAVar(jadx.core.dex.instructions.args.SSAVar) VarDeclareRef(jadx.api.data.annotations.VarDeclareRef) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr) CodegenException(jadx.core.utils.exceptions.CodegenException) Jadx(jadx.core.Jadx) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) JadxAttrType(jadx.api.plugins.input.data.attributes.JadxAttrType) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DepthTraversal(jadx.core.dex.visitors.DepthTraversal) BLOCK_DUMP(jadx.core.codegen.MethodGen.FallbackOption.BLOCK_DUMP) AnnotationMethodParamsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr) ConstStringNode(jadx.core.dex.instructions.ConstStringNode) FALLBACK_MODE(jadx.core.codegen.MethodGen.FallbackOption.FALLBACK_MODE) Objects(java.util.Objects) JadxError(jadx.core.dex.attributes.nodes.JadxError) List(java.util.List) Stream(java.util.stream.Stream) AccessFlags(jadx.api.plugins.input.data.AccessFlags) COMMENTED_DUMP(jadx.core.codegen.MethodGen.FallbackOption.COMMENTED_DUMP) Collections(java.util.Collections) ICodeWriter(jadx.api.ICodeWriter) Utils(jadx.core.utils.Utils) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr)

Example 2 with MethodOverrideAttr

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

the class JavaMethod method getOverrideRelatedMethods.

public List<JavaMethod> getOverrideRelatedMethods() {
    MethodOverrideAttr ovrdAttr = mth.get(AType.METHOD_OVERRIDE);
    if (ovrdAttr == null) {
        return Collections.emptyList();
    }
    JadxDecompiler decompiler = getDeclaringClass().getRootDecompiler();
    return ovrdAttr.getRelatedMthNodes().stream().map(m -> ((JavaMethod) decompiler.convertNode(m))).collect(Collectors.toList());
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) List(java.util.List) MethodNode(jadx.core.dex.nodes.MethodNode) AType(jadx.core.dex.attributes.AType) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) AccessInfo(jadx.core.dex.info.AccessInfo) ApiStatus(org.jetbrains.annotations.ApiStatus) Utils(jadx.core.utils.Utils) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr)

Example 3 with MethodOverrideAttr

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

the class OverrideMethodVisitor method processOverrideMethods.

private MethodOverrideAttr processOverrideMethods(MethodNode mth, SuperTypesData superData) {
    MethodOverrideAttr result = mth.get(AType.METHOD_OVERRIDE);
    if (result != null) {
        return result;
    }
    ClassNode cls = mth.getParentClass();
    String signature = mth.getMethodInfo().makeSignature(false);
    List<IMethodDetails> overrideList = new ArrayList<>();
    Set<IMethodDetails> baseMethods = new HashSet<>();
    for (ArgType superType : superData.getSuperTypes()) {
        ClassNode classNode = mth.root().resolveClass(superType);
        if (classNode != null) {
            MethodNode ovrdMth = searchOverriddenMethod(classNode, signature);
            if (ovrdMth != null) {
                if (isMethodVisibleInCls(ovrdMth, cls)) {
                    overrideList.add(ovrdMth);
                    MethodOverrideAttr attr = ovrdMth.get(AType.METHOD_OVERRIDE);
                    if (attr != null) {
                        addBaseMethod(superData, overrideList, baseMethods, superType);
                        return buildOverrideAttr(mth, overrideList, baseMethods, attr);
                    }
                }
            }
        } else {
            ClspClass clsDetails = mth.root().getClsp().getClsDetails(superType);
            if (clsDetails != null) {
                Map<String, ClspMethod> methodsMap = clsDetails.getMethodsMap();
                for (Map.Entry<String, ClspMethod> entry : methodsMap.entrySet()) {
                    String mthShortId = entry.getKey();
                    if (mthShortId.startsWith(signature)) {
                        overrideList.add(entry.getValue());
                        break;
                    }
                }
            }
        }
        addBaseMethod(superData, overrideList, baseMethods, superType);
    }
    return buildOverrideAttr(mth, overrideList, baseMethods, null);
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) ClassNode(jadx.core.dex.nodes.ClassNode) ClspClass(jadx.core.clsp.ClspClass) ClspMethod(jadx.core.clsp.ClspMethod) ArrayList(java.util.ArrayList) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr) MethodNode(jadx.core.dex.nodes.MethodNode) Map(java.util.Map) IMethodDetails(jadx.core.dex.nodes.IMethodDetails) HashSet(java.util.HashSet)

Example 4 with MethodOverrideAttr

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

the class OverrideMethodVisitor method applyOverrideAttr.

private MethodOverrideAttr applyOverrideAttr(MethodNode mth, List<IMethodDetails> overrideList, Set<IMethodDetails> baseMethods, boolean update) {
    // don't rename method if override list contains not resolved method
    boolean dontRename = overrideList.stream().anyMatch(m -> !(m instanceof MethodNode));
    SortedSet<MethodNode> relatedMethods = null;
    List<MethodNode> mthNodes = getMethodNodes(mth, overrideList);
    if (update) {
        // merge related methods from all override attributes
        for (MethodNode mthNode : mthNodes) {
            MethodOverrideAttr ovrdAttr = mthNode.get(AType.METHOD_OVERRIDE);
            if (ovrdAttr != null) {
                // use one of already allocated sets
                relatedMethods = ovrdAttr.getRelatedMthNodes();
                break;
            }
        }
        if (relatedMethods != null) {
            relatedMethods.addAll(mthNodes);
        } else {
            relatedMethods = new TreeSet<>(mthNodes);
        }
        for (MethodNode mthNode : mthNodes) {
            MethodOverrideAttr ovrdAttr = mthNode.get(AType.METHOD_OVERRIDE);
            if (ovrdAttr != null) {
                SortedSet<MethodNode> set = ovrdAttr.getRelatedMthNodes();
                if (relatedMethods != set) {
                    relatedMethods.addAll(set);
                }
            }
        }
    } else {
        relatedMethods = new TreeSet<>(mthNodes);
    }
    int depth = 0;
    for (MethodNode mthNode : mthNodes) {
        if (dontRename) {
            mthNode.add(AFlag.DONT_RENAME);
        }
        if (depth == 0) {
            // skip current (first) method
            depth = 1;
            continue;
        }
        if (update) {
            MethodOverrideAttr ovrdAttr = mthNode.get(AType.METHOD_OVERRIDE);
            if (ovrdAttr != null) {
                ovrdAttr.setRelatedMthNodes(relatedMethods);
                continue;
            }
        }
        mthNode.addAttr(new MethodOverrideAttr(Utils.listTail(overrideList, depth), relatedMethods, baseMethods));
        depth++;
    }
    return new MethodOverrideAttr(overrideList, relatedMethods, baseMethods);
}
Also used : MethodNode(jadx.core.dex.nodes.MethodNode) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr)

Example 5 with MethodOverrideAttr

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

the class FixAccessModifiers method fixMethodVisibility.

private static int fixMethodVisibility(MethodNode mth) {
    AccessInfo accessFlags = mth.getAccessFlags();
    if (accessFlags.isPublic()) {
        return -1;
    }
    MethodOverrideAttr overrideAttr = mth.get(AType.METHOD_OVERRIDE);
    if (overrideAttr != null && !overrideAttr.getOverrideList().isEmpty()) {
        // visibility can't be weaker
        IMethodDetails parentMD = overrideAttr.getOverrideList().get(0);
        AccessInfo parentAccInfo = new AccessInfo(parentMD.getRawAccessFlags(), AccessInfo.AFType.METHOD);
        if (accessFlags.isVisibilityWeakerThan(parentAccInfo)) {
            return parentAccInfo.getVisibility().rawValue();
        }
    }
    if (mth.getUseIn().isEmpty()) {
        return -1;
    }
    ClassNode thisTopParentCls = mth.getParentClass().getTopParentClass();
    for (MethodNode useMth : mth.getUseIn()) {
        ClassNode useInTPCls = useMth.getParentClass().getTopParentClass();
        if (!useInTPCls.equals(thisTopParentCls)) {
            return AccessFlags.PUBLIC;
        }
    }
    return -1;
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) MethodNode(jadx.core.dex.nodes.MethodNode) AccessInfo(jadx.core.dex.info.AccessInfo) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr) IMethodDetails(jadx.core.dex.nodes.IMethodDetails)

Aggregations

MethodOverrideAttr (jadx.core.dex.attributes.nodes.MethodOverrideAttr)7 MethodNode (jadx.core.dex.nodes.MethodNode)6 AccessInfo (jadx.core.dex.info.AccessInfo)3 ArgType (jadx.core.dex.instructions.args.ArgType)3 IMethodDetails (jadx.core.dex.nodes.IMethodDetails)3 AType (jadx.core.dex.attributes.AType)2 ClassNode (jadx.core.dex.nodes.ClassNode)2 Utils (jadx.core.utils.Utils)2 Collections (java.util.Collections)2 List (java.util.List)2 CommentsLevel (jadx.api.CommentsLevel)1 ICodeWriter (jadx.api.ICodeWriter)1 InsnCodeOffset (jadx.api.data.annotations.InsnCodeOffset)1 VarDeclareRef (jadx.api.data.annotations.VarDeclareRef)1 AccessFlags (jadx.api.plugins.input.data.AccessFlags)1 EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)1 JadxAttrType (jadx.api.plugins.input.data.attributes.JadxAttrType)1 AnnotationMethodParamsAttr (jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr)1 Consts (jadx.core.Consts)1 Jadx (jadx.core.Jadx)1