Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the method name from within that method?

I am trying to create a function that returns the method name from within that method:

  public static String getMethodName(final int depth)
  {
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    return ste[ste.length - 1 - depth].getMethodName();
  }

However, when I call this method from Activity.onCreate(), it returns "main" instead of "onCreate".

How do I get the actual method name from within that method?

like image 510
Android Eve Avatar asked Apr 05 '11 18:04

Android Eve


People also ask

How do you get the method name inside the method?

Method Class | getName() Method in Java Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects. Return Value: It returns the name of the method, as String.

What is getClass () getName () in Java?

The getName() method of java Class class is used to get the name of the entity, and that entity can be class, interface, array, enum, method, etc. of the class object. Element Type.

How do you write a method name?

While writing a method name we should follow the camel case i.e. first letter of the first word should be small and the first letters of the remaining (later) words should be capital.


4 Answers

return ste[1+depth].getMethodName();

If you change return statement as above, you would get immediate calling method name , of cource depth shoould be zero..

like image 181
Gursel Koca Avatar answered Oct 05 '22 09:10

Gursel Koca


Despite the fact initiating an Exception is more expensive way, I would do it anyway.

Log.d("CurrentMethod", new Exception().getStackTrace()[0].getMethodName());

Works if called in onCreate.

like image 23
harism Avatar answered Oct 05 '22 09:10

harism


A singleton to manage logs:

public class ActiveLog {
public static final String TAG = "TRACE LOG";
private static ActiveLog instance;
private static boolean actif;

public static ActiveLog getInstance() {
    if (null == instance) 
        instance = new ActiveLog();
    return instance;
}

private ActiveLog() {
    ActiveLog.setActif(true);
}

public void log() {
    if(isActif())
        Log.d(TAG, "" + (new Exception().getStackTrace()[1].getClassName())
                      + ": "
                      + (new Exception().getStackTrace()[1].getMethodName()));
}

public static boolean isActif() {
    return actif;
}

public static void setActif(boolean actif) {
    ActiveLog.actif = actif;
}}

An example of use:

public class MyTest {
  public void test() {
    ActiveLog.getInstance().log();
  }
}

The result:

09-05 14:37:09.822: D/TRACE LOG(XXXX): com.TestProject.MyTest: test
like image 32
Gndm Avatar answered Oct 05 '22 09:10

Gndm


I think your problem maybe you are accessing the stack upside down. In the returned value element 0 is the most recent call (which would be getStackTrace()). I think what you are intending to do is:

public static String getMethodName(final int depth) {
  final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
  return ste[1 + depth].getMethodName();
}

This will access the most recent call in the stack (outside of the call to getStackTrace()). For example if you have a method:

public void foo() {
  System.out.println(getMethodName(0));
}

This will print "foo" with the above implementation of the function. Of course you may also want to add some bounds checking to the function since it could easily go outside the array.

like image 42
M. Jessup Avatar answered Oct 05 '22 08:10

M. Jessup