10

How do I get the enclosing method name at compile time?

And why has it been difficult for me to figure this out? Why wouldn't Java want me to do this? I don't see any inherent problems with a feature like this, and it's useful for logging method names without duplication between the method signature and the string constant like so:

private void methodName() {
   final String MN = "methodName";
   ...

Someone might change the method name without changing the MN constant, and then I can't find it in the log.

I know Java has a (reflective?) method for doing this, but why anyone would want to over-complicate things and create the possibility of errors with a run-time solution (for this particular problem) is beyond me.

Apparently macros are bad practice, but I think this is a pretty good use case for them. That's what I'm basically going for, something like a macro, a constant.

Btw, this question is pretty similar:

Getting the name of the current executing method

None of the solutions are constants derived at compile time, though. They're all run-time computations.


EDIT: Why can't it be done at compile time?

Please don't give a trivial answer like "that's the way Java was designed", because then I'll just ask "why ?"

Community
  • 1
  • 1
Mike
  • 201
  • 2
  • 4
  • *What* method name? Please explain more clearly what exactly you're trying to accomplish. (The answer may be that you need to use Java 8 method handles.) – chrylis -cautiouslyoptimistic- Feb 16 '15 at 23:38
  • If you use a logging framework, then capturing the method name is already taken care of. – Oliver Charlesworth Feb 16 '15 at 23:40
  • 3
    possible duplicate of [Getting the name of the current executing method](http://stackoverflow.com/questions/442747/getting-the-name-of-the-current-executing-method) – merlin2011 Feb 16 '15 at 23:42
  • 2
    i don't think it's a dupe, im looking for a compile time solution which is not mentioned in that one – Mike Feb 17 '15 at 00:00
  • Have you looked at *all* of the answers in the linked questions and verified that *all* of them are run-time? – merlin2011 Feb 17 '15 at 00:19
  • If so, please edit your question to link to that question and mention that none of the answers there will work for your question because you are looking for a compile-time solution. – merlin2011 Feb 17 '15 at 00:20
  • 3
    Can't be done at compile time. – Radiodef Feb 17 '15 at 01:00
  • There's no good reason why this can't be done since this could easily be done by a compiler or preprocessor. In fact, other languages support this: http://stackoverflow.com/questions/2770307/nslog-the-method-name-with-objective-c-in-iphone – Necreaux Apr 14 '17 at 16:06

2 Answers2

2

From the answer https://stackoverflow.com/a/5891326/4353712:

String name = new Object(){}.getClass().getEnclosingMethod().getName();

However, as mentioned in the answer, this generates a new .class file every time you use it.

Community
  • 1
  • 1
0

Not at compile time, but still might work out for you:

public class test1 {
    public void display() {
        try {
            final String s = this.getClass().getMethod("display",null).toString();
            System.out.println(s);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }
    public static void main(String args[]) { 
        test1 obj = new test1();
        obj.display();
    }
}
ddarellis
  • 3,912
  • 3
  • 25
  • 53
Rusheel Jain
  • 843
  • 6
  • 20
  • 3
    How is this any better than his solution? You still have to type the method name by hand... – Sándor Mátyás Márton Mar 30 '16 at 08:27
  • 1
    From java docs, getEnclosingMethod() - If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class. So as per my understanding, this works only if there exists a class within a method. For my solution, you have to type the method name but you get a fully qualified name in return. If you want to get name of the method on-the-fly then I dont think that is possible. – Rusheel Jain Mar 30 '16 at 15:27