NPE on getEnclosingMethod()

Claes Redestad claes.redestad at oracle.com
Tue Dec 17 00:14:41 UTC 2019


Hi Dennis,

On 2019-12-17 00:12, Dennis Gesker wrote:
> Hello OpenJDK list...
> 
> I do hope this is the correct list for user questions. 

not really, but since you suspected this was a bug I had a look. :-)

> I'm getting a NULL when calling:
> 
> this.getClass().getEnclosingMethod()

according to the javadoc[1], Class::getEnclosingMethod return "the
immediately enclosing method of the underlying class, if that class is a
local or anonymous class; otherwise *null*."

In your test case, 'this' seems to be an instance of the unit test,
which is a regular class. So this.getClass().getEnclosingMethod()
returning null seems to be the expected result.

To demonstrate, see this example of getEnclosingMethod() on a local,
anonymous and a regular class:

public class Enclosing {

   public void foo() {
     class Foo {}; // Local class
     System.out.println(Foo.class.getEnclosingMethod());

     // Anonymous class
     Object bar = new Object() {}; // Anonymous class
     System.out.println(bar.getClass().getEnclosingMethod());

     System.out.println(this.getClass().getEnclosingMethod());
   }

   public static void main(String ... args) {
     new Enclosing().foo();
   }
}

.. which prints:

public void Enclosing.foo()
public void Enclosing.foo()
null

Hope this makes sense!

/Claes

[1] 
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getEnclosingMethod()

> 
> I was going to file a bug but I'm not yet a contributor and hope to get
> login credentials soon. Any chance an existing member could take a look at
> my unit test below? And, if verified and not duplicate file a bug report on
> my behalf?
> 
> Thanks,
> Dennis
> 
> Using:
> 
> openjdk version "11.0.5-ea" 2019-10-15
> OpenJDK Runtime Environment (build 11.0.5-ea+10-post-Ubuntu-0ubuntu1)
> OpenJDK 64-Bit Server VM (build 11.0.5-ea+10-post-Ubuntu-0ubuntu1, mixed
> mode, sharing)
> 
> The following test compiles (-Xlint:all -Werror) but throws and NPE on
> execution:
> 
> package test;
> 
> import static org.junit.Assert.assertTrue;
> import java.util.logging.Level;
> import java.util.logging.Logger;
> import org.junit.jupiter.api.Test;
> 
> public class CheckForNullEnclosingMethod {
> private Logger logger = Logger.getLogger(this.getClass().getSimpleName());
> 
> @Test
> public void iSeemToBeLost() {
> 
> if (this.getClass().getEnclosingMethod() == null) {
> logger.logp(Level.SEVERE, this.getClass().getName(),
> this.getClass().getEnclosingMethod().getName(),
> "What method am I in?");
> assertTrue(this.getClass().getEnclosingMethod() == null);
> assertTrue(this.getClass().getEnclosingMethod().getName() == null);
> return;
> }
> 
> logger.logp(Level.INFO, this.getClass().getName(),
> this.getClass().getEnclosingMethod().getName(),
> "No worries, I'm not lost");
> assertTrue(this.getClass().getEnclosingMethod() != null);
> assertTrue(this.getClass().getEnclosingMethod().getName() != null);
> 
> }
> }
> 


More information about the jdk-dev mailing list