diff --git a/core/src/main/java/org/kohsuke/stapler/Stapler.java b/core/src/main/java/org/kohsuke/stapler/Stapler.java index 8b12af9da..07c0d83e6 100644 --- a/core/src/main/java/org/kohsuke/stapler/Stapler.java +++ b/core/src/main/java/org/kohsuke/stapler/Stapler.java @@ -1048,6 +1048,7 @@ public ClassLoader getClassLoader() { /** * Gets the current {@link StaplerRequest2} that the calling thread is associated with. + * @return null, if called from outside an HTTP handling thread */ public static StaplerRequest2 getCurrentRequest2() { return CURRENT_REQUEST.get(); @@ -1064,6 +1065,7 @@ public static StaplerRequest getCurrentRequest() { /** * Gets the current {@link StaplerResponse2} that the calling thread is associated with. + * @return null, if called from outside an HTTP handling thread */ public static StaplerResponse2 getCurrentResponse2() { return CURRENT_RESPONSE.get(); @@ -1080,9 +1082,11 @@ public static StaplerResponse getCurrentResponse() { /** * Gets the current {@link Stapler} that the calling thread is associated with. + * @return null, if called from outside an HTTP handling thread */ public static Stapler getCurrent() { - return CURRENT_REQUEST.get().getStapler(); + var req = CURRENT_REQUEST.get(); + return req != null ? req.getStapler() : null; } /** diff --git a/core/src/test/java/org/kohsuke/stapler/StaplerTest.java b/core/src/test/java/org/kohsuke/stapler/StaplerTest.java index 2a9b1a653..6681daf4a 100644 --- a/core/src/test/java/org/kohsuke/stapler/StaplerTest.java +++ b/core/src/test/java/org/kohsuke/stapler/StaplerTest.java @@ -91,4 +91,8 @@ public void testToFileOnUnix() throws Exception { new URL("file:/tmp/" + path)); } } + + public void testGetCurrent() throws Exception { + assertNull("may be called outside an HTTP handling thread", Stapler.getCurrent()); + } }