Profesionálny internetový závislák • Herný nadšenec • Technologický tvorca
Profesionálny internetový závislák • Herný nadšenec • Technologický tvorca

Získanie metódy, ktorá volala inú metódu v Jave

Ako získať volajúceho metódy v Jave!
Túto stránku preložili z angličtiny moji vysoko motivovaní stážisti v oblasti umelej inteligencie pre vaše pohodlie. Stále sa učia, takže sa mohlo vyskytnúť niekoľko chýb. Najpresnejšie informácie nájdete v anglickej verzii.
Domov Blog Získanie metódy, ktorá volala inú metódu v Jave

Upozorňujeme, že tento blogový príspevok bol publikovaný v októbri 2010, takže v závislosti od toho, kedy ho čítate, môžu byť niektoré časti zastarané. Bohužiaľ, nemôžem tieto príspevky vždy udržiavať úplne aktuálne, aby som zabezpečil presnosť informácií.

    In some situations it could be handy to figure out who called a specific method at runtime, especially when a specific method is used by hundreds of methods and you have no clue how to recreate the execution.
    This can either be done by using external Java profilers which reads and analyze a Java applications memory usage or you can get the caller method programmatically within the application at runtime.
    This is done by basically instantly dumping a stack trace without throwing an Exception. However dumping the stack trace (with or without creating an Exception) has a huge performance cost and should really only be done at temporarily special circumstances.
    If you would like to get the stack traces of all active threads you can use:
    If you only need the stack trace for the current thread you can use:
    To get the calling method you can create a fancy method like:
    private static String getCallingMethodName() { StackTraceElement stack = Thread.currentThread().getStackTrace()[3]; return stack.getClassName() + "." + stack.getMethodName(); }

    Napísal/a Special Agent Squeaky. Prvýkrát publikované 10. 10. 2010. Naposledy aktualizované 10. 10. 2010.

    📺 Sledujte najnovšie video od Squeakyho!

    Ako pridať jednoduché titulky v reálnom čase k vášmu živému prenosu