Skip to content

Commit 32b9ea9

Browse files
committed
WebAsyncUtils avoids reflection for creating StandardServletAsyncWebRequest
Issue: SPR-13112
1 parent 4eea675 commit 32b9ea9

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncUtils.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,14 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.context.request.async;
1718

18-
import java.lang.reflect.Constructor;
1919
import javax.servlet.ServletRequest;
2020
import javax.servlet.http.HttpServletRequest;
2121
import javax.servlet.http.HttpServletResponse;
2222

23-
import org.springframework.beans.BeanUtils;
2423
import org.springframework.util.ClassUtils;
2524
import org.springframework.web.context.request.RequestAttributes;
2625
import org.springframework.web.context.request.WebRequest;
@@ -29,13 +28,15 @@
2928
* Utility methods related to processing asynchronous web requests.
3029
*
3130
* @author Rossen Stoyanchev
31+
* @author Juergen Hoeller
3232
* @since 3.2
3333
*/
3434
public abstract class WebAsyncUtils {
3535

3636
public static final String WEB_ASYNC_MANAGER_ATTRIBUTE = WebAsyncManager.class.getName() + ".WEB_ASYNC_MANAGER";
3737

38-
private static Constructor<?> standardAsyncRequestConstructor;
38+
// Determine whether Servlet 3.0's ServletRequest.startAsync method is available
39+
private static final boolean startAsyncAvailable = ClassUtils.hasMethod(ServletRequest.class, "startAsync");
3940

4041

4142
/**
@@ -66,31 +67,27 @@ public static WebAsyncManager getAsyncManager(WebRequest webRequest) {
6667
}
6768

6869
/**
69-
* Create an AsyncWebRequest instance. By default an instance of
70-
* {@link StandardServletAsyncWebRequest} is created if running in Servlet
71-
* 3.0 (or higher) environment or as a fallback, an instance of
72-
* {@link NoSupportAsyncWebRequest} is returned.
73-
*
70+
* Create an AsyncWebRequest instance. By default, an instance of
71+
* {@link StandardServletAsyncWebRequest} gets created when running in
72+
* Servlet 3.0 (or higher) environment - as a fallback, an instance
73+
* of {@link NoSupportAsyncWebRequest} will be returned.
7474
* @param request the current request
7575
* @param response the current response
76-
* @return an AsyncWebRequest instance, never {@code null}
76+
* @return an AsyncWebRequest instance (never {@code null})
7777
*/
7878
public static AsyncWebRequest createAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) {
79-
return ClassUtils.hasMethod(ServletRequest.class, "startAsync") ?
80-
createStandardServletAsyncWebRequest(request, response) : new NoSupportAsyncWebRequest(request, response);
79+
return (startAsyncAvailable ? AsyncWebRequestFactory.createStandardAsyncWebRequest(request, response) :
80+
new NoSupportAsyncWebRequest(request, response));
8181
}
8282

83-
private static AsyncWebRequest createStandardServletAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) {
84-
try {
85-
if (standardAsyncRequestConstructor == null) {
86-
String className = "org.springframework.web.context.request.async.StandardServletAsyncWebRequest";
87-
Class<?> clazz = ClassUtils.forName(className, WebAsyncUtils.class.getClassLoader());
88-
standardAsyncRequestConstructor = clazz.getConstructor(HttpServletRequest.class, HttpServletResponse.class);
89-
}
90-
return (AsyncWebRequest) BeanUtils.instantiateClass(standardAsyncRequestConstructor, request, response);
91-
}
92-
catch (Throwable t) {
93-
throw new IllegalStateException("Failed to instantiate StandardServletAsyncWebRequest", t);
83+
84+
/**
85+
* Inner class to avoid a hard dependency on the Servlet 3.0 API.
86+
*/
87+
private static class AsyncWebRequestFactory {
88+
89+
public static AsyncWebRequest createStandardAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) {
90+
return new StandardServletAsyncWebRequest(request, response);
9491
}
9592
}
9693

0 commit comments

Comments
 (0)