Optimize object creation PartialMatchHelper

See gh-29634
This commit is contained in:
koo.taejin
2022-12-04 15:18:22 +09:00
committed by rstoyanchev
parent f457c17c78
commit 5ac97b16a8
4 changed files with 53 additions and 9 deletions

View File

@@ -246,7 +246,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
protected HandlerMethod handleNoMatch(
Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException {
PartialMatchHelper helper = new PartialMatchHelper(infos, request);
PartialMatchHelper helper = PartialMatchHelper.from(infos, request);
if (helper.isEmpty()) {
return null;
}
@@ -293,11 +293,13 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
/**
* Aggregate all partial matches and expose methods checking across them.
*/
private static class PartialMatchHelper {
private static final class PartialMatchHelper {
private static final PartialMatchHelper EMPTY_HELPER = new PartialMatchHelper(Collections.emptySet(), null);
private final List<PartialMatch> partialMatches = new ArrayList<>();
public PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
private PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
for (RequestMappingInfo info : infos) {
if (info.getActivePatternsCondition().getMatchingCondition(request) != null) {
this.partialMatches.add(new PartialMatch(info, request));
@@ -305,6 +307,13 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
}
}
public static PartialMatchHelper from(Set<RequestMappingInfo> infos, HttpServletRequest request) {
if (CollectionUtils.isEmpty(infos)) {
return EMPTY_HELPER;
}
return new PartialMatchHelper(infos, request);
}
/**
* Whether there are any partial matches.
*/

View File

@@ -25,6 +25,7 @@ import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -397,6 +398,17 @@ class RequestMappingInfoHandlerMappingTests {
assertThat(uriVariables.get("cars")).isEqualTo("cars");
}
@PathPatternsParameterizedTest
void handleNoMatchEmptyRequestMappingInfo(TestRequestMappingInfoHandlerMapping mapping) throws ServletException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/cars;color=green");
HandlerMethod handlerMethod = mapping.handleNoMatch(new HashSet<>(), "/{cars}", request);
assertThat(handlerMethod).isNull();
handlerMethod = mapping.handleNoMatch(null, "/{cars}", request);
assertThat(handlerMethod).isNull();
}
private HandlerMethod getHandler(
TestRequestMappingInfoHandlerMapping mapping, MockHttpServletRequest request) throws Exception {