smali (Android): Operating on an Instance Variable
Question
i'm trying to do the following using smali:
- add a private instance
ArrayList
field to a class - instantiate the instance field in the class' constructor
- do an
add
operation on the field in one of the class' methods.
These are the relevant code snippets. (My class name is com.mypackage.MyClass
(i.e. com/mypackage/MyClass
))
In the # instance fields
section of the smali code, i added the line
.field private myList:Ljava/util/List;
In the public constructor method (.method public constructor <init>()V
), i added the lines
new-instance v1, Ljava/util/ArrayList;
invoke-direct {v1}, Ljava/util/ArrayList;-><init>()V
iput-object v1, p0, Lcom/mypackage/MyClass;->myList:Ljava/util/List;
And finally, in the instance methods where i want to perform the add
method calls, i added the lines
iget-object v6, p0, Lcom/mypackage/MyClass;->myList:Ljava/util/List;
invoke-interface {v6, v3}, Ljava/util/List;->add(Ljava/lang/Object;)Z
invoke-interface {v6, v4}, Ljava/util/List;->add(Ljava/lang/Object;)Z
However, the line
iget-object v6, p0, Lcom/mypackage/MyClass;->myList:Ljava/util/List;
gives me the error (according to logcat)
10-29 15:47:58.999: W/dalvikvm(518): VFY: 'this' arg 'Ljava/util/List;' not instance of 'Lcom/mypackage/MyClass;'
I compared what I did with a similar method call in the same method, and couldn't figure out what was wrong.
Any idea?
Thanks!
Solution
The p0 register contains the "this" reference when the method enters, but anything can modify it afterwards. Based on that error message, it looks like the p0 regsiter has a List object in it, at the point where the iget-object is.