-2

I am logging Function name using reflection line

this.getClass().getEnclosingMethod().getName()

But Its throwing NULLPointerException as this.getClass().getEnclosingMethod() is returning Null,

While

this.getClass().getMethods()[0].getName() working fine.

Why reflection method getEnclosingMethod() throwing Null pointer exception. What is the fix for it

Java version : 11

dinesh kandpal
  • 738
  • 7
  • 16
  • Why do you expect [`getEnclosingMethod`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getEnclosingMethod--) to return something other than `null`? – Joe Jan 19 '22 at 08:07
  • No Joe, Its not answered , What i askeed is why this is returning Null rather specific functionName this.getClass().getEnclosingMethod().getName() Shoudn't we use this ? I – dinesh kandpal Jan 19 '22 at 08:14
  • Oh, this very much answers your question. Your problem is that your ASSUMPTION is flawed. So, you SHOULD NOT use this. The link given to you tells you how you can get to the method name. – GhostCat Jan 19 '22 at 08:31
  • And hint: the answer is really in the javadocs. READ what those methods do. And finally: `this.getClass().getMethods()[0].getName() working fine.` now, it is not. That will ALWAYS return the name of the FIRST method of that class. It just uses reflection to get the "static" (not changing") list of methods of that class. Try using that in a method with multiple methods. Surprise, it will NOT work. – GhostCat Jan 19 '22 at 08:35

2 Answers2

2

Let's first understand what getEnclosingMethod() is used for.

Example: Main.java

public class Main {
    public Object getName(){
        class Example{

        }
        return new Example();
    }
    public static void main(String[] args) {
        Main main = new Main();
        Class subClass = main.getName().getClass();
        System.out.println("EnclosingMethod of Main: "
                + subClass.getEnclosingMethod());
    }
}

getEnclosingMethod() method returns the enclosing methods of this class if this class is a local class or anonymous class declared in that method or else it returns null. Since in the above example, Example Class is declared inside the method getName(), it'll return the output

EnclosingMethod of Main: public java.lang.Object Main.getName()

Meaning getName() method of Main.class has a local class Example declared. If getName() was nothing like

public Object getName(){
        return "A String";
    }

getEnclosingMethod() would return null as no class declaration was done.

Edit: As @GhostCat mentioned , in both these examples this.getClass().getMethods()[0].getName() will return a non-null value as you are basically calling that method.

Shreyas B
  • 475
  • 2
  • 11
  • 1
    You might want to add that `this.getClass().getMethods()[0].getName()`, yes, does return a non null string. But then, it is also not what the OP wants ;-) – GhostCat Jan 19 '22 at 08:36
0
  • It just returns a Method object representing the immediately enclosing method of the underlying class if and only if this Class object represents a local or anonymous class within a method
  • refer that