aspose file tools*
The moose likes Java in General and the fly likes Help needed on Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
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: 9521
    
    2

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, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
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: 9521
    
    2

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: 252
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: 3355

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: 9521
    
    2

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: 9521
    
    2

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: 3355

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

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

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: 9521
    
    2

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://aspose.com/file-tools
 
subject: Help needed on Reflection