skylot / jadx

Dex to Java decompiler
Apache License 2.0
41.82k stars 4.88k forks source link

[core] method.getInstructions() is null when using jadx as a library #1727

Closed fa1conn closed 1 year ago

fa1conn commented 2 years ago

I follow the issue #1482 and want to print the instructions of method,but it return null. Other member variables are normal, such as getInsnsCount() could return the count of instructions.

jpstotz commented 2 years ago

Have you tried to call MethodNode.load()? Afterwards the list of instructions should be initialized.

skylot commented 2 years ago

Well, MethodNode is considered internal, so better try to not use it. But if you really want to, there are several solutions:

  1. Call method MethodNode.load() as @jpstotz mentioned
  2. As mentioned in # 1482 you can insert additional pass, but in this case you should put in after ProcessInstructionsVisitor, so insert line should be:
    addCustomPassAfter(passes, ProcessInstructionsVisitor.class, customPass);

    This will work because instructions array available only in first passes working on input instructions, next it replaced with basic blocks tree.

Also, getInsnsCount() return an approximation of instructions count, actually it is a size of code block in code units (2 bytes for DEX). Looks like I forget to rename this property after I rename ICodeReader.getUnitsCount().