How to determine current procedure/class name

In perl i can user the caller() function to give me the name of the current (or even better the callers) class and procedure name, in gcc theres __PRETTY_FUNCTION__, is there any way of doing this in java.

e.g. I want to do this
String x = "I got called by class " + getcallerclass() + " method " + getcallermethod()


time4teaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_lychee_Commented:
a method is to generate an exception, catch it and parse the stack trace....
refer to

http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10222663 

look at the last comment made so far (by heyhey_)

special emphasis goes to the fact that the stack trace may vary between jvms...

0
time4teaAuthor Commented:
This is quite a nice solution.. however would be (I guess) quite slow...especially if I'm gonna call the function lots of times... I'm looking for something than can tell me where my debug statements are coming from...

However, I'll check this out - So thanks for the pointer!

I'd be interested if there are any other possible ideas before closing this one....
0
JodCommented:
The easiest way to do this is to just pass in the callers class as a parameter to the method:

//call method
c.calling("bbb",this.class);

then at the other end you will receive a class object as a parameter, so define your method as:

public class c {

  calling (String s, Class c) {
    String x = "I got called by class " + c.toString();
  }

this.class gets you the class object for the class you are currently in. It is returned as a class object and you can use as you would any other object.


0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

chintal_studCommented:
class test
{
      
public static void a(Object  
         obj,String name,Class[] params)
{
  try
  {
  System.out.println(obj.getClass()  
   .toString()+obj.getClass() .getMethod
     (name,params));
  }catch(Exception e)
  {
     System.out.println(e);
  }
}
      
}

public class trial2{
public static void main(String args[])
{
trial2 trial=new trial2();
trial.b();
}

public void b()
{
      String name="b";
      Class[] cl=null;
      test.a(this,name,cl);
}

}

run this and see.
at any time the function "a(...)" of class "test"
displyas you  the class and the method of the class which is invoking this method a(..);  

if you are not clear you can ask again.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_lychee_Commented:
hmmm.... if u're gonna do that y dun u just do something like

public static void a(Object b, String n) {
   System.out.println(b.getClass(), n);
}

and instead of passing in the method name and param list, just pass in the whole thing as a string? seems alot more convenient...
ie.
test.a(this, "b()");
0
Ravindra76Commented:
Difficult to judge the points winner
0
jsridharCommented:
there is support for such debugging in JDI (Java Debug Interface)

http://java.sun.com/products/jpda/doc/jdi/overview-summary.html

look at com.sun.jdi.*

it provides api to access the stack frames
0
time4teaAuthor Commented:
Theres a lot of info here... so thanks to you all.... !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.