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.
To get it programmatically you can use: String exceptionClassName = e. getClass(). getName();
Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur.
getClass() If an instance of an object is available, then the simplest way to get its Class is to invoke Object. getClass() . Of course, this only works for reference types which all inherit from Object .
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With