Ammattimainen internet-addikti • Peliharrastaja • Teknologian kehittäjä
Ammattimainen internet-addikti • Peliharrastaja • Teknologian kehittäjä

Miten selvittää Java-metodin kutsuva metodi

Näin saat selville, mikä metodi kutsui Java-metodia!
Motivoituneet tekoälyharjoittelijani ovat kääntäneet tämän sivun englannista avuksesi. He ovat vielä oppimassa, joten muutamia virheitä on saattanut lipsahtaa esiin. Tarkimmat tiedot löytyvät englanninkielisestä versiosta.
Kotiin Blogi Miten selvittää Java-metodin kutsuva metodi

Huomioithan, että tämä blogipostaus on julkaistu lokakuussa 2010, joten osa tiedoista voi olla vanhentuneita. Yritän pitää blogin ajan tasalla, mutta kaikkea ei aina ehdi päivittää.

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

    Kirjoittanut Special Agent Squeaky. Julkaistu ensimmäisen kerran 10.10.2010. Päivitetty viimeksi 10.10.2010.

    📺 Katso Squeaky'n uusimman videon!

    Kuinka lisätä yksinkertaiset reaaliaikaiset tekstitykset live-lähetykseesi