lundi 7 février 2022

Java: Is there a way to have a method to get the calling method name?

To get the current method name we can use the following:

new Object(){}.getClass().getEnclosingMethod().getName();

which should be called from within the method body.

Is there a way to have a service method which will return the method name for each calling method?

For example:

private void method1(){

   String thisMethodName = getMethodName(); //will return "method1"

}

private void method2(){

   String thisMethodName = getMethodName(); //will return "method2"

}

private void getMethodName(/*Any params?*/){

   //Is there an implementation for that? (In the same class or in some service class)

}




Aucun commentaire:

Enregistrer un commentaire