"Robert Mark Bram" <none> wrote:
> I should have been more specific.. what I wanted to do is have a line of
> code to create a logger for each class, but I didn't want to edit the line -
> just blindly paste it into each class as a static..
No, there's no good way to do that. Technically speaking, I believe it
could be accomplished with:
Class c = Class.forName(
new Throwable().getStackTrace()[0].getClassName());
But that's *really* ugly, and perhaps worse than the problem you're
trying to solve in the first place. Furthermore, the results of
getStackTrace() aren't really well-defined enough to have confidence
that this will work.
In general, though, you can't do this easily because it's assumed that
when writing a class, you know the class name. By contrast, getClass()
is available on an instance because the actual class may be a subclass
of the one you're writing; and thus the class isn't known at compile
time.
> "The latest generation of virtual machines perform extensive optimizations
> when JITing and may entirely remove stack frames, making it impossible to
> reliably locate the calling class and method."
Note that this will also be a problem with the code I wrote above.
However, there just isn't a compile-time way to get that information
without using the class name in an expression.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation