Java reflection is powerful, but there are much more things to watch out.
I went into below error when invoke a method using reflection:
java.lang.IllegalArgumentException: object is not an instance of declaring class
It take me almost one whole day to find out the reason, that is I invoke method on class loaded from different Class loader, although the Class looks like the same.
When debugging Java code in eclipse, every variable has a (id=**) at the end. It can be used to determine if tow objects, including Class, are the same one.
I know WordPress is far better than MS live space, I didn’t make the change simply because I am lazy. I should say thank you to MS for the tolerable service all these years, and this easy and smooth migration to WordPress.
PS. I know this migration from CCTV news broadcast, not from Internet:)
1. The “gcc -framework cocoa” does not work on linux.
2. The name of the makefile have to be “GNUmakefile”.
Here is a sample GNUmakefile:
TOOL_NAME = Go
Go_OBJC_FILES = main.m Person.m
1. Install cocoa.vim: unzip cocoa.zip -d ~/.vim
This include Objective-C filetype and syntax.
2. Install snipMate: unzip snipMate.zip -d ~/.vim
This include Objective-C snippets
3. Set filetype for .h and .m file
echo "au BufNewFile,BufRead *.h,*.m set filetype=objc" > ~/.vim/ftdetect/objc.vim
I have gone through a painful afternoon just because I mess up with “this”. Let me tell you what has happened.
I’ll put it in the simplest form: I have a class “Invoker”, it simply invoke all methods of a object.
and here is ClassA which has two methods just print their names.
With these two classes, I could at least prove I am not doing anything wrong by run this line:
OK, it works. Now there comes ClassB, it want do something different in method m1 while still keep using the implementation of ClassA.m2
Let’s run ClassB
Ooops, where is the output of method m2? why m2 is not working anymore? Isn’t ClassB.m2 inherited the ClassA.m2? Why m2 stop working while run is still working?
Yes, ClassB is inherit from ClassA, that’s the reason why we could still get some output because run method of ClassB is still working. Remember we are not directly calling m2, but by reflection. Let’s look at the run method:
The run method takes in a instance of Invoker and run it with “this”. So when we call run on ClassB, the Invoker.invoke method call with a instance of ClassB, and because this is no m2 defined on ClassB, there is actually no m2 method call in the call of of Invoker.invoke.
But knowing m2 won’t get things any better, we still need to reuse the default implementations of ClassA, what should we do? It won’t be a problem if I have made myself understood: Implement the m2 method and call its super class’s method. So our ClassB finally looks like this:
To summary, take care of “this” when subclassing a Class involved in reflection.
Watch this code run, if you still mad about ruby block.