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]
# 1
well, getClass() will get you the class context of the method you are in. But I don't think that's what you want.I think what you want is something like Thread.currentThread().getStackTrace()I think the stacktraceelement you want is going to be length - 2
tjacobs01a at 2007-7-12 20:46:41 > top of Java-index,Java Essentials,Java Programming...
# 2
Why do you need to know this?
BigDaddyLoveHandlesa at 2007-7-12 20:46:41 > top of Java-index,Java Essentials,Java Programming...
# 3

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)?

Orbitala at 2007-7-12 20:46:41 > top of Java-index,Java Essentials,Java Programming...
# 4
You want the declaring class, not the calling class? If so, it's as simple as Logger logger = Logger.getLogger(getClass());~
yawmarka at 2007-7-12 20:46:41 > top of Java-index,Java Essentials,Java Programming...
# 5
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.
Orbitala at 2007-7-12 20:46:41 > top of Java-index,Java Essentials,Java Programming...
# 6

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

}

}

BigDaddyLoveHandlesa at 2007-7-12 20:46:41 > top of Java-index,Java Essentials,Java Programming...