Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed on Reflection

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I need to do something like this,

But I do not want to say it in double quotes...like "myMethod" is there any means of doing it using Reflection?
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HelloClass.class.getMethod("METHOD_YOUWANTTOCALL", null).invoke(null, null);
[ July 16, 2008: Message edited by: Pratap koritala ]
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pratap Chowdary:
public class HelloClass
{
//some methods
}


HelloClass.class.getMethod("METHOD_YOUWANTTOCALL", null).invoke(null, null);


HelloClass hcobj=new HelloClass();

hcobj.getClass().getMethod("METHOD_YOUWANTTOCALL", null).invoke(null, null);

Those parameters with NULLS are for Calling parameterized Methods

[ July 11, 2008: Message edited by: Pratap Chowdary ]


I think you did not understand my question, I want to print the name of the method inside the same method.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are the person who coded ( and also hard coded) the method and its body, why don't hard code the method name also...


Why you wanted to do like that..?
[ July 16, 2008: Message edited by: Pratap koritala ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jothi,

Do you want to log the name of the method automatically (as ENTER and EXIT for each methods) without hardcoding it explicitly?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds to me like you want Thread.currentThread().getStackTrace() and take the first element. You can also get the method name like this:This is a lot of work (and inefficient) for debugging purposes though - better to hard code it if you can.

The easiest way is to create (or throw) a new exception there and log that. Exceptions have the entire stack trace built in!
[ July 11, 2008: Message edited by: Charles Lyons ]
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Lyons:
Sounds to me like you want Thread.currentThread().getStackTrace() and take the first element. You can also get the method name like this:This is a lot of work (and inefficient) for debugging purposes though - better to hard code it if you can.

The easiest way is to create (or throw) a new exception there and log that. Exceptions have the entire stack trace built in!

[ July 11, 2008: Message edited by: Charles Lyons ]


I was thinking on the same lines and I made use of StackTraceElement array and found the solution.

Thanks guys for the participation.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan,

You are right, I didn't want to hard code it. I mean the method name...
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great! Hope you got the solution now
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the point in doint that....

Whats the point in getting programatically in the method itself...
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pratap Chowdary:
Whats the point in doint that....

Whats the point in getting programatically in the method itself...


You don't need to 'hardcode' the method name as Jothi quoted in his first post!
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pratap Chowdary:
Whats the point in doint that....

Whats the point in getting programatically in the method itself...


I simpley don't have to hard code the method names. I have a class where I have 50 odd methods and I really don't want to put anything in "double quotes".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic