-1

I want to know if is possible to get the name of the class and function (or at least the function) in the callMyFunc method below.

I need to print something like "ClsTest/myFuncInTest" or simple "myFuncInTest".


/** Main.java */
import java.util.concurrent.Callable;
public class Main
{
    public static void main (String[]args) throws Exception
    {
        ClsTest C = new ClsTest();
        callMyFunc(C::myFuncInTest);
    }
    
    private static void callMyFunc(Callable<Boolean> theFunc) throws Exception {
        //System.out.println(theFunc.getName());     <-- HERE!!!!
        theFunc.call();
    }
}

/** ClsTest.java */
public class ClsTest {
    public Boolean myFuncInTest() {
        return true;
    }
}

  • Does this help? https://stackoverflow.com/questions/421280/how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection – GhostCat May 06 '22 at 12:05
  • But note: in current day java, a callable might just be a lambda, so the "name" would be some lambda$$$ whatever name mangled string that doesn't tell you much. – GhostCat May 06 '22 at 12:06
  • As a matter of fact, the class `ClsTest` is not involved when inspecting the concrete `Callable` in your method `callMyFunc`. It is only involved when creating it. So what you want to do is indeed not possible. – Seelenvirtuose May 06 '22 at 12:21
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 06 '22 at 19:45

0 Answers0