This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Help needed on Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Help needed on Reflection" Watch "Help needed on Reflection" New topic
Author

Help needed on Reflection

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

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?


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 251
HelloClass.class.getMethod("METHOD_YOUWANTTOCALL", null).invoke(null, null);
[ July 16, 2008: Message edited by: Pratap koritala ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

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.
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 251
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

Joined: Apr 20, 2006
Posts: 3327

Jothi,

Do you want to log the name of the method automatically (as ENTER and EXIT for each methods) without hardcoding it explicitly?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

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

Joined: Sep 26, 2006
Posts: 8795

Raghavan,

You are right, I didn't want to hard code it. I mean the method name...
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

That's great! Hope you got the solution now
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 251
Whats the point in doint that....

Whats the point in getting programatically in the method itself...
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

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

Joined: Sep 26, 2006
Posts: 8795

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".
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Help needed on Reflection
 
Similar Threads
Class.getMethod with variable args - part 2
return int in parameter
Retrieving a method's name from within that very method
Reflection API
Retrieving a method's name from within that very method