• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method Class

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to use the Method class to get information about the method I am in. I can't get any code to compile. What is the correct syntax to get an instance of this class so I can use the methods within in?
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the Code??
 
Kay Crocker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a little class to test the syntax

import java.lang.reflect.*;
public class MethodNames {
private static void executeStaticTask() {
// System.out.println(Method.getName());
}
private void executeTask() {
Method method = new Method();
System.out.println(Class.getMethod().method.getName());
}
public static void main(String[] args) {
MethodNames methodNames = new MethodNames();
methodNames.executeTask();
executeStaticTask();
}
}
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the Class class. There are methods in there to get an array of declared Methods, and the Method object for a given method signature. You cannot instantiate a Method directly, as it is completely dependent on the class.
You can get the Class by calling getClass() on an object, by using the class literal (YourClassName.class), or by calling Class.forName("YourClassName").
- Peter

[This message has been edited by Peter den Haan (edited November 01, 2001).]
 
Kay Crocker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I got that far but I want to get the name of the method that I am currently in. The arguments for getName() takes a String which is described as the simple name of the method. What is this?
Basically I am trying to println each method I hit to the dos window. (Like a logwriter -- put I want to reuse this bit of code so I could cut and paste.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think I know what Kay means. She needs a vanilla way to output the method name to a log writing utility, to track execution, or whatever. For example, she wants to convert code that looks like this:
public void myMethod1() {
LogWriter.print("myMethod1 called");
}
public void myMethod2() {
LogWriter.print("myMethod2 called");
}
to this:
public void myMethod1() {
LogWriter.print(getClass().getMethod().getName() + " called");
}
public void myMethod2() {
LogWriter.print(getClass().getMethod().getName() + " called");
}
so that you can just paste this into every method, without having to worry about changing the hard-coded method name in the string all the time.
The problem seems to be that the getMethod() method requires the name of the method as a string - but if we hard-code that, then we haven't bought anything. That's a great question, Kay!
 
Kay Crocker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,
You hit the nail on the head. That is exactly what I am trying to do.
Can you help with the code? Where did that bartender go?
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an age-old question in Java.
One old hack is as follows, create some method as follows:

You can parse the resulting stack trace to get the desired method, usually the second on in the stack.

I think the new JDK 1.4 logging API has support for this, as well.


Taken from: http://java.sun.com/j2se/1.4/docs/guide/util/logging/overview.html
[B]Second, there are a set of methods that do not take explicit source class or source method names. These are intended for
developers who want easy-to-use logging and do not require detailed source information.

For this second set of methods, the Logging framework will make a "best effort" to determine which class and method called
into the logging framework and will add this information into the LogRecord. However, it is important to realize that this
automatically inferred information may only be approximate. The latest generation of virtual machines perform extensive
optimizations when JITing and may entirely remove stack frames, making it impossible to reliably locate the calling class and
method.
[/B]


Warning
JITs can interfere with this type of tracing, because they often change around the byte code and/or its execution.

--Mark
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That bartender went to sleep
Got very little to add, except one last word: don't reinvent the wheel, use Log4J. It's about as good as logging gets; small wonder the J2SE 1.4 logging facilities were patterned after it.
- Peter
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic