Professionele internetverslaafde • Game-enthousiasteling • Tech-maker
Professionele internetverslaafde • Game-enthousiasteling • Tech-maker

Een methode-aanroepmethode in Java ophalen

Hoe je de aanroeper van een Java-methode krijgt!
Deze pagina is voor uw gemak vertaald uit het Engels door mijn zeer gemotiveerde AI-stagiaires. Ze zijn nog in ontwikkeling, dus er kunnen een paar foutjes doorheen zijn geglipt. Raadpleeg de Engelse versie voor de meest accurate informatie.
Thuis Blog Een methode-aanroepmethode in Java ophalen

Houd er rekening mee dat deze blogpost in oktober 2010 is gepubliceerd. Afhankelijk van wanneer u hem leest, kunnen bepaalde delen dus verouderd zijn. Helaas kan ik deze berichten niet altijd volledig up-to-date houden om ervoor te zorgen dat de informatie accuraat blijft.

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

    Geschreven door Special Agent Squeaky. Eerste publicatie 2010-10-10. Laatste update 2010-10-10.

    📺 Bekijk de nieuwste video van Squeaky!

    Hoe voeg je eenvoudige realtime ondertitels toe aan je livestream