• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

calling the funciton name

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a scenario where i am getting the function name from another class, so i am storing it in a string. Then i need to call the function which is in string.

for example i got some function name like "sampleFunction" then i store this name in a string. i need to trigger this function in the next step. how to do this.

Thanks in advance
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to accomplish this, you will need to use java reflection which is often considered an advanced topic in java.

The method you will need to use is the invoke method on the Method class.

I encourage you to do a google search on 'java reflection' and pick a tutorial to read in order to learn how to use the reflection libraries.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you give us a small description of the problem you're trying to solve using reflection.
Because most of the time reflection is not necessary and the problem can be solved in a better way.
 
Vishal Kumar Aruru
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

But here my scenario is different, when i use method.invoke() function it returns an object, which i don't want this. My function returns a list of classes. So, when i use method.invoke() function, i need to typecast the list of the classes. but i will get the class name dynamically, so how to use this for a generic way.

Let me narrate you.

I will send a classname as parameter to a fucntion. that function will gets the fields name, in that fields having any list type fields, I have to traverse in to that list. So for traversing i used the getter of that list and i invoked that function it returns object. but i need to typecast the list of the classes. but i will get the class name dynamically, so how to use this for a generic way.
 
Marshal
Posts: 80764
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds too difficult for "beginning". Moving thread
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Kumar Aruru wrote:Thanks,

But here my scenario is different, when i use method.invoke() function it returns an object, which i don't want this. My function returns a list of classes. So, when i use method.invoke() function, i need to typecast the list of the classes. but i will get the class name dynamically, so how to use this for a generic way.

Let me narrate you.

I will send a classname as parameter to a fucntion. that function will gets the fields name, in that fields having any list type fields, I have to traverse in to that list. So for traversing i used the getter of that list and i invoked that function it returns object. but i need to typecast the list of the classes. but i will get the class name dynamically, so how to use this for a generic way.



This kinda smells more of a polymorphism and less of a reflection to me,
Should'nt interfact solve such issues ?

Vishal Kumar Aruru wrote:i will get the class name dynamically


- Builder pattern.

 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Kumar Aruru wrote:
But here my scenario is different, when i use method.invoke() function it returns an object, which i don't want this. My function returns a list of classes. So, when i use method.invoke() function, i need to typecast the list of the classes. but i will get the class name dynamically, so how to use this for a generic way.



It doesn't matter if your function returns a list of classes. A "list of classes" IS-A object. Heck, it doesn't even matter if the method returns primatives (which aren't objects). The invoke() method will box any primitives returns for you, and hence, the return IS-A object.

Another reflection items which may help. The Method object has a method that tells you what is the type being returned.

Henry
 
Mazer Lao Tzu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but I think I am having trouble with your English. I am having trouble understanding the following:

that function will gets the fields name, in that fields having any list type fields, I have to traverse in to that list. So for traversing i used the getter of that list



Are you retrieving a list of all of the fields of the class and then invoking a getter for any of the fields that have a type implementing java.util.List?

I agree with Wouter Oet and salvin francis: it sounds like your solution to whatever problem you are trying to solve is an overcomplicated one. There could be a much more straightforward approach.
reply
    Bookmark Topic Watch Topic
  • New Topic