4

Say I have a method that I wish to call which has an argument of a string

To call, I would do something like this.. myFunction(stringValue);

NOW, how would i make that same call, but dynamically if I had a string with the value of 'myFunction'..

something like

method = [convert "myFunction" string to method];
method.invoke(stringValue);

I am currently trying something like

java.lang.reflect.Method method;

method = Class.forName("com.blah.MyActivity").getMethod('myFunction',String.class);
method.invoke (stringValue);

but getting the error IllegalArgumentException Message expected receiver of type com.blah.MyActivity, but got java.lang.String

7
  • 2
    @user353877, you should study this and understand how the revisions were made. Commented Jun 28, 2012 at 18:07
  • you need to pass your instance as your first argument I believe method.invoke( instance, stringValue ) Commented Jun 28, 2012 at 18:07
  • 1
    sorry for the bad title, when i went to edit the question and make its code look correct, i mistakenly changed the title, as opposed to noting what i was editing about the question Commented Jun 28, 2012 at 18:08
  • @user353877: Ah, that makes much more sense. Still, though, basic formatting shouldn't be something other people have to do for you at this point. Commented Jun 28, 2012 at 18:09
  • I don't know how to get the parent (or 'receiver', I believe its called in java) with which to call the method on Commented Jun 28, 2012 at 18:13

2 Answers 2

4

The instruction:

method.invoke (stringValue);

needs the object in which the method will be invoked.

So, if you try something like:

method = Class.forName("com.blah.MyActivity").getMethod('myFunction',String.class);
method.invoke(someInstanceOfMyActivity, stringValue);

it will work.

Documentation: http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you ;) ,but I dont know how to get 'someInstanceOfMyActivity' since I am inside of a class which was called by 'someInstanceOfMyActivity'
Can you take a look at my last comment
then post a separate question, if it isn't already asked @user353877
3

Follow up to my comment above:

do this to invoke com.blah.MyActivity::myFunction:

com.blah.MyActivity activity = new com.blah.MyActivity() ;

method = Class.forName("com.blah.MyActivity").getMethod('myFunction',String.class);
method.invoke (activity, stringValue);

(forgive the c++ syntax)

I'll try to explain why it works like this:

A method is really just a special type of function.. it's a convenience created to let people deal with classes and objects..

As you know, inside a method you always have a this variable.. behind this scenes, this is passed in as a parameter, but instead writing this:

com.blah.MyActivity activity = new com.blah.MyActivity() ;
com.blah.MyActivity::myFunction( activity, stringValue )

you can conveniently write this:

com.blah.MyActivity activity = new com.blah.MyActivity() ;
activity.myFunction(stringValue)

When you get a method using reflection, it's just an "ordinary function". To call it directly, you need to pass in the this parameter.

That's why you are having type errors... myFunction expects the first hidden argument to be an instance of it's containing class, which you omitted.

HTH

1 Comment

Thank you for adding more info and taking the time to explain. The world thanks you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.