5

Yes, getting the class name by using an exception is a plausible solution, but I'm looking for something that is a bit more elegant.

String className = new Exception().getStackTrace()[1].getClassName();

This will be used mainly for logging purposes and making sure my cache keywords are component/caller-class specific.

Peeter
  • 9,282
  • 5
  • 36
  • 53
  • possible duplicate of [How to find method name from inside that method? (Java)](http://stackoverflow.com/questions/4641540/how-to-find-method-name-from-inside-that-method-java) – jmj Mar 21 '11 at 09:35
  • Well it's more an addendum of this question. Erik answer is a lot more 'eye friendly' and giving the same result. Useful methods on stack are `getClassName(), getMethodName(), getLineNumber(), getFileName()` – Michael Laffargue Mar 21 '11 at 09:40
  • https://stackoverflow.com/questions/11306811/how-to-get-the-caller-class-in-java – larsaars Aug 18 '20 at 14:27

3 Answers3

12

a) no need to use Exception, you can do this: Thread.currentThread().getStackTrace()

b) whatever you are trying to do, don't do it that way. That sounds awful. I guess you should be looking into logging via AOP (here's a small tutorial that looks reasonable).

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • There are good reasons to get the calling class. One of the common ones is to call Class.getResourceAsStream() on the caller's class rather than the method's class ("get my class's resource") – Wheezil Feb 22 '17 at 14:52
  • @Wheezil sure, but that sounds hacky, as most scenarios will. I'm not saying I never go the hacky way, but I prefer the "clean" way whenever I can. – Sean Patrick Floyd Feb 22 '17 at 14:56
  • Also note that the calling class.method is element [2] in the stack trace. Element [1] is the current class.method, and element[0] is Thread.getStackTrace – Wheezil Feb 22 '17 at 15:26
  • Agreed this is hacky. I've only ever done this for testing, if I want to centralize the load-from-resource-name into a utility method. – Wheezil Feb 22 '17 at 15:27
  • Yes, I also have been guilty of that, just as I use reflection a lot in tests – Sean Patrick Floyd Feb 22 '17 at 15:28
3
Thread.currentThread().getStackTrace()
Erik
  • 88,732
  • 13
  • 198
  • 189
  • By the way Thread method is the slowest, the fastest way is a custom SecurityManager as measured here: http://stackoverflow.com/a/2924426/1386911 – Daniel Hári Mar 18 '17 at 19:30
3

On the Oracle JVM you can use the non-standard sun.reflect.Reflection.getCallerClass(2) . This is much faster but should only be used with care. (As it is not cross platform and could change between versions of Java)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    It did changed between Java7u10 and u11. Beside of this it will be [removed](http://www.infoq.com/news/2013/07/Oracle-Removes-getCallerClass) in Java8. What a pity, it was the fastest method so far (~ 100x faster then `Thread.currentThread().getStackTrace()`) and I see no replacement. – Dag Jul 04 '13 at 12:47