Professzionális internetfüggő • Játékrajongó • Technikafejlesztő
Professzionális internetfüggő • Játékrajongó • Technikafejlesztő

Hogyan lehet megkapni egy metódust meghívó másik metódust Java-ban

Hogyan lehet megszerezni egy Java metódus hívóját?
Ezt az oldalt angolról fordították a rendkívül motivált mesterséges intelligencia gyakornokaim a kényelmed érdekében. Még tanulnak, így előfordulhatott, hogy néhány hiba kimaradt. A legpontosabb információkért kérjük, tekintse meg az angol verziót.
Otthon Blog Hogyan lehet megkapni egy metódust meghívó másik metódust Java-ban

Felhívjuk figyelmét, hogy ez a blogbejegyzés 2010 októberében jelent meg, így attól függően, hogy mikor olvassa el, bizonyos részei elavultak lehetnek. Sajnos nem mindig tudom ezeket a bejegyzéseket teljesen naprakészen tartani, hogy biztosítsam az információk pontosságát.

    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(); }

    Írta: Special Agent Squeaky. Először megjelent: 2010.10.10. Utolsó frissítés: 2010.10.10.

    📺 Nézd meg Squeaky legújabb videóját!

    Hogyan adj hozzá egyszerű valós idejű feliratokat az élő közvetítésedhez