Skip to content

Commit ffe7ec4

Browse files
committed
Apply "instanceof pattern matching" in remainder of spring-webmvc module
See gh-30067
1 parent 9b811a0 commit ffe7ec4

File tree

46 files changed

+214
-232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+214
-232
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,8 @@ else if (logger.isDebugEnabled()) {
812812
@Deprecated
813813
@Nullable
814814
public final org.springframework.ui.context.ThemeSource getThemeSource() {
815-
return (getWebApplicationContext() instanceof org.springframework.ui.context.ThemeSource ?
816-
(org.springframework.ui.context.ThemeSource) getWebApplicationContext() : null);
815+
return (getWebApplicationContext() instanceof org.springframework.ui.context.ThemeSource themeSource ?
816+
themeSource : null);
817817
}
818818

819819
/**
@@ -1143,9 +1143,9 @@ private void processDispatchResult(HttpServletRequest request, HttpServletRespon
11431143
boolean errorView = false;
11441144

11451145
if (exception != null) {
1146-
if (exception instanceof ModelAndViewDefiningException) {
1146+
if (exception instanceof ModelAndViewDefiningException mavDefiningException) {
11471147
logger.debug("ModelAndViewDefiningException encountered", exception);
1148-
mv = ((ModelAndViewDefiningException) exception).getModelAndView();
1148+
mv = mavDefiningException.getModelAndView();
11491149
}
11501150
else {
11511151
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
@@ -1188,8 +1188,8 @@ private void processDispatchResult(HttpServletRequest request, HttpServletRespon
11881188
@Override
11891189
protected LocaleContext buildLocaleContext(final HttpServletRequest request) {
11901190
LocaleResolver lr = this.localeResolver;
1191-
if (lr instanceof LocaleContextResolver) {
1192-
return ((LocaleContextResolver) lr).resolveLocaleContext(request);
1191+
if (lr instanceof LocaleContextResolver localeContextResolver) {
1192+
return localeContextResolver.resolveLocaleContext(request);
11931193
}
11941194
else {
11951195
return () -> (lr != null ? lr.resolveLocale(request) : request.getLocale());

spring-webmvc/src/main/java/org/springframework/web/servlet/ModelAndView.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -195,7 +195,7 @@ public void setViewName(@Nullable String viewName) {
195195
*/
196196
@Nullable
197197
public String getViewName() {
198-
return (this.view instanceof String ? (String) this.view : null);
198+
return (this.view instanceof String name ? name : null);
199199
}
200200

201201
/**
@@ -212,7 +212,7 @@ public void setView(@Nullable View view) {
212212
*/
213213
@Nullable
214214
public View getView() {
215-
return (this.view instanceof View ? (View) this.view : null);
215+
return (this.view instanceof View v ? v : null);
216216
}
217217

218218
/**

spring-webmvc/src/main/java/org/springframework/web/servlet/config/MvcNamespaceUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -303,7 +303,7 @@ public static Object getContentNegotiationManager(ParserContext context) {
303303
*/
304304
private static boolean containsBeanInHierarchy(ParserContext context, String beanName) {
305305
BeanDefinitionRegistry registry = context.getRegistry();
306-
return (registry instanceof BeanFactory ? ((BeanFactory) registry).containsBean(beanName) :
306+
return (registry instanceof BeanFactory beanFactory ? beanFactory.containsBean(beanName) :
307307
registry.containsBeanDefinition(beanName));
308308
}
309309

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -76,8 +76,8 @@ protected List<Object> getInterceptors() {
7676

7777
private static final Comparator<Object> INTERCEPTOR_ORDER_COMPARATOR =
7878
OrderComparator.INSTANCE.withSourceProvider(object -> {
79-
if (object instanceof InterceptorRegistration) {
80-
return (Ordered) ((InterceptorRegistration) object)::getOrder;
79+
if (object instanceof InterceptorRegistration interceptorRegistration) {
80+
return (Ordered) interceptorRegistration::getOrder;
8181
}
8282
return null;
8383
});

spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultAsyncServerResponse.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -168,19 +168,18 @@ private DeferredResult<ServerResponse> createDeferredResult(HttpServletRequest r
168168
return result;
169169
}
170170

171-
@SuppressWarnings({"unchecked"})
172-
public static AsyncServerResponse create(Object o, @Nullable Duration timeout) {
173-
Assert.notNull(o, "Argument to async must not be null");
171+
@SuppressWarnings({"unchecked", "rawtypes"})
172+
public static AsyncServerResponse create(Object obj, @Nullable Duration timeout) {
173+
Assert.notNull(obj, "Argument to async must not be null");
174174

175-
if (o instanceof CompletableFuture) {
176-
CompletableFuture<ServerResponse> futureResponse = (CompletableFuture<ServerResponse>) o;
175+
if (obj instanceof CompletableFuture futureResponse) {
177176
return new DefaultAsyncServerResponse(futureResponse, timeout);
178177
}
179178
else if (reactiveStreamsPresent) {
180179
ReactiveAdapterRegistry registry = ReactiveAdapterRegistry.getSharedInstance();
181-
ReactiveAdapter publisherAdapter = registry.getAdapter(o.getClass());
180+
ReactiveAdapter publisherAdapter = registry.getAdapter(obj.getClass());
182181
if (publisherAdapter != null) {
183-
Publisher<ServerResponse> publisher = publisherAdapter.toPublisher(o);
182+
Publisher<ServerResponse> publisher = publisherAdapter.toPublisher(obj);
184183
ReactiveAdapter futureAdapter = registry.getAdapter(CompletableFuture.class);
185184
if (futureAdapter != null) {
186185
CompletableFuture<ServerResponse> futureResponse =
@@ -189,7 +188,7 @@ else if (reactiveStreamsPresent) {
189188
}
190189
}
191190
}
192-
throw new IllegalArgumentException("Asynchronous type not supported: " + o.getClass());
191+
throw new IllegalArgumentException("Asynchronous type not supported: " + obj.getClass());
193192
}
194193

195194

spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequestBuilder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -299,11 +299,9 @@ private <T> T bodyInternal(Type bodyType, Class<?> bodyClass) throws ServletExce
299299
MediaType contentType = headers().contentType().orElse(MediaType.APPLICATION_OCTET_STREAM);
300300

301301
for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
302-
if (messageConverter instanceof GenericHttpMessageConverter) {
303-
GenericHttpMessageConverter<T> genericMessageConverter =
304-
(GenericHttpMessageConverter<T>) messageConverter;
302+
if (messageConverter instanceof GenericHttpMessageConverter<?> genericMessageConverter) {
305303
if (genericMessageConverter.canRead(bodyType, bodyClass, contentType)) {
306-
return genericMessageConverter.read(bodyType, bodyClass, inputMessage);
304+
return (T) genericMessageConverter.read(bodyType, bodyClass, inputMessage);
307305
}
308306
}
309307
if (messageConverter.canRead(bodyClass, contentType)) {

spring-webmvc/src/main/java/org/springframework/web/servlet/function/ErrorHandlingServerResponse.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -60,11 +60,11 @@ protected final ModelAndView handleError(Throwable t, HttpServletRequest servlet
6060
if (serverResponse != null) {
6161
return serverResponse.writeTo(servletRequest, servletResponse, context);
6262
}
63-
else if (t instanceof ServletException) {
64-
throw (ServletException) t;
63+
else if (t instanceof ServletException servletException) {
64+
throw servletException;
6565
}
66-
else if (t instanceof IOException) {
67-
throw (IOException) t;
66+
else if (t instanceof IOException ioException ) {
67+
throw ioException;
6868
}
6969
else {
7070
throw new ServletException(t);

spring-webmvc/src/main/java/org/springframework/web/servlet/function/HandlerFilterFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -112,8 +112,8 @@ default HandlerFunction<R> apply(HandlerFunction<T> handler) {
112112
return (request, next) -> {
113113
try {
114114
T t = next.handle(request);
115-
if (t instanceof ErrorHandlingServerResponse) {
116-
((ErrorHandlingServerResponse) t).addErrorHandler(predicate, errorHandler);
115+
if (t instanceof ErrorHandlingServerResponse response) {
116+
response.addErrorHandler(predicate, errorHandler);
117117
}
118118
return t;
119119
}

spring-webmvc/src/main/java/org/springframework/web/servlet/function/PathResourceLookupFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -125,8 +125,8 @@ private boolean isResourceUnderLocation(Resource resource) throws IOException {
125125
resourcePath = resource.getURL().toExternalForm();
126126
locationPath = StringUtils.cleanPath(this.location.getURL().toString());
127127
}
128-
else if (resource instanceof ClassPathResource) {
129-
resourcePath = ((ClassPathResource) resource).getPath();
128+
else if (resource instanceof ClassPathResource classPathResource) {
129+
resourcePath = classPathResource.getPath();
130130
locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath());
131131
}
132132
else {

spring-webmvc/src/main/java/org/springframework/web/servlet/function/SseServerResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -168,8 +168,8 @@ private SseBuilder field(String name, String value) {
168168
public void data(Object object) throws IOException {
169169
Assert.notNull(object, "Object must not be null");
170170

171-
if (object instanceof String) {
172-
writeString((String) object);
171+
if (object instanceof String text) {
172+
writeString(text);
173173
}
174174
else {
175175
writeObject(object);

0 commit comments

Comments
 (0)