get current method's class
I think this question was asked before but the answer was not clear enough.
What is the equivalent statement in Java to this C# code:
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
I only need a String but if a whole object is returned like the C# code above, things would be much easier.
Thank you very much.
[356 byte] By [
Orbitala] at [2007-11-27 8:45:31]

I usually use log4net in C# for logging debug information.
Something like:
ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Now I need to do the same thing with java log4j:
Logger logger = Logger.getLogger(String);
tjacobs01, how'd you know that it is Stack length - 2? What if it is a method calling another method (recursive method for example)?
> This won't compile as I expect the Logger to be
> static.
> One way (quite ugly) that I'm using is that I
> manually type in the class name in the
> getLogger(String name) method.
Less ugly, less likely to have a typo:
static Logger logger = Logger.getLogger(YourClassHere.class);
A small hack I learned in these forums, to turn the previous line
into "cut-n-paste-friendly" code:
private static class Marker {}
static Logger logger = Logger.getLogger(Marker.class.getEnclosingClass());
Demo:
class Wrap {
private Object object;
public Wrap(Object object) {
this.object = object;
}
public String toString() {
return String.valueOf(object);
}
}
public class OuterClass {
private static class Marker {}
static Wrap wrap0 = new Wrap(OuterClass.class);
static Wrap wrap1 = new Wrap(Marker.class);
static Wrap wrap2 = new Wrap(Marker.class.getEnclosingClass());
public static void main(String[] args) {
System.out.println("wrap0=" + wrap0);
System.out.println("wrap1=" + wrap1);
System.out.println("wrap2=" + wrap2);
}
}