Is invoke-direct really invoking the methods that are defined in the instantiated object?

StackOverflow https://stackoverflow.com/questions/14252163

  •  14-01-2022
  •  | 
  •  

Question

I have some test code, which is a ConcreteClass inheriting the AbstractClassForInterface1. In the generated smali code for the constructor of ConcreteClass:

# direct methods
.method public constructor <init>()V
    .locals 0

    .prologue
    .line 4
    invoke-direct {p0}, Lorg/ucomb/AbstractClassForInterface1;-><init>()V 

    return-void
.end method

p0 is actually the instantiated object of type ConcreteClass, since it's from the instantiation:

new-instance v0, Lorg/ucomb/ConcreteClass1;

invoke-direct {v0}, Lorg/ucomb/ConcreteClass1;-><init>()V

and it is easy to understand that the object v0 has the method Lorg/ucomb/ConcreteClass1;-><init>()V,

Then in the body, why using invoke-direct on the p0 to invoke the super class's constructor like this:

invoke-direct {p0}, Lorg/ucomb/AbstractClassForInterface1;-><init>()V ?

Isn't the invoke-direct supposed to invoke method without virtual method resolution?

Was it helpful?

Solution

When calling a direct method, the exact method that is being called is specified. In this case, Lorg/ucomb/AbstractClassForInterface1;-><init>()V. This is not a virtual table lookup - it is guaranteed to be the AbstractClassForInterface1 constructor. If it were a virtual table lookup, it would resolve to the most specialized override of the <init>()V method, which would be the <init>()V method in your ConcreteClass1 class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top