From 68c321499a3c1b5fa42aa75191dec3bcc61be00f Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Mon, 28 Sep 2026 18:50:45 +0700 Subject: [PATCH 1/2] Use instanceof pattern matching to avoid an explicit cast See gh-51896 Signed-off-by: Tran Ngoc Nhan --- .../MetadataGenerationEnvironment.java | 4 ++-- .../configurationprocessor/TypeElementMembers.java | 4 ++-- .../boot/devtools/remote/server/DispatcherFilter.java | 4 ++-- .../boot/devtools/restart/Restarter.java | 10 +++++----- .../DispatcherServletsMappingDescriptionProvider.java | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java b/configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java index ae018ed094a..498ac7c0d8d 100644 --- a/configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java +++ b/configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java @@ -402,8 +402,8 @@ class MetadataGenerationEnvironment { // continue } Element superType = this.typeUtils.asElement(element.getSuperclass()); - if (superType instanceof TypeElement && superType.asType().getKind() != TypeKind.NONE) { - resolveFieldValuesFor(values, (TypeElement) superType); + if (superType instanceof TypeElement typeElement && superType.asType().getKind() != TypeKind.NONE) { + resolveFieldValuesFor(values, typeElement); } } diff --git a/configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java b/configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java index d4429f95e1c..a0eb675a674 100644 --- a/configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java +++ b/configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java @@ -80,9 +80,9 @@ class TypeElementMembers { processMethod(method); } Element superType = this.env.getTypeUtils().asElement(element.getSuperclass()); - if (superType instanceof TypeElement && !OBJECT_CLASS_NAME.equals(superType.toString()) + if (superType instanceof TypeElement typeElement && !OBJECT_CLASS_NAME.equals(superType.toString()) && !RECORD_CLASS_NAME.equals(superType.toString())) { - process((TypeElement) superType); + process(typeElement); } } diff --git a/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/server/DispatcherFilter.java b/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/server/DispatcherFilter.java index 844c8e6ad44..8466b44ad97 100644 --- a/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/server/DispatcherFilter.java +++ b/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/server/DispatcherFilter.java @@ -56,8 +56,8 @@ public class DispatcherFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { - doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain); + if (request instanceof HttpServletRequest req && response instanceof HttpServletResponse res) { + doFilter(req, res, chain); } else { chain.doFilter(request, response); diff --git a/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java b/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java index 8727c0ace9e..7d7efe053ee 100644 --- a/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java +++ b/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java @@ -357,11 +357,11 @@ public class Restarter { Field field = type.getDeclaredField(fieldName); field.setAccessible(true); Object instance = field.get(null); - if (instance instanceof Set) { - ((Set) instance).clear(); + if (instance instanceof Set set) { + set.clear(); } - if (instance instanceof Map) { - ((Map) instance).keySet().removeIf(this::isFromRestartClassLoader); + if (instance instanceof Map map) { + map.keySet().removeIf(this::isFromRestartClassLoader); } } catch (Exception ex) { @@ -372,7 +372,7 @@ public class Restarter { } private boolean isFromRestartClassLoader(Object object) { - return (object instanceof Class && ((Class) object).getClassLoader() instanceof RestartClassLoader); + return (object instanceof Class cls && cls.getClassLoader() instanceof RestartClassLoader); } /** diff --git a/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/actuate/web/mappings/DispatcherServletsMappingDescriptionProvider.java b/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/actuate/web/mappings/DispatcherServletsMappingDescriptionProvider.java index fd2d1f9d70b..e3f07dd073b 100644 --- a/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/actuate/web/mappings/DispatcherServletsMappingDescriptionProvider.java +++ b/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/actuate/web/mappings/DispatcherServletsMappingDescriptionProvider.java @@ -104,8 +104,8 @@ public class DispatcherServletsMappingDescriptionProvider implements MappingDesc Map dispatcherServlets = new LinkedHashMap<>(); context.getBeansOfType(ServletRegistrationBean.class).values().forEach((registration) -> { Servlet servlet = registration.getServlet(); - if (servlet instanceof DispatcherServlet && !dispatcherServlets.containsValue(servlet)) { - dispatcherServlets.put(registration.getServletName(), (DispatcherServlet) servlet); + if (servlet instanceof DispatcherServlet dispatcherServlet && !dispatcherServlets.containsValue(servlet)) { + dispatcherServlets.put(registration.getServletName(), dispatcherServlet); } }); context.getBeansOfType(DispatcherServlet.class).forEach((name, dispatcherServlet) -> { From 7a871ff117ed550cab6000eab53f7b3631a0f63a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 28 Sep 2026 14:12:14 -0700 Subject: [PATCH 2/2] Polish 'Use instanceof pattern matching to avoid an explicit cast' See gh-51896 --- .../boot/devtools/remote/server/DispatcherFilter.java | 5 +++-- .../org/springframework/boot/devtools/restart/Restarter.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/server/DispatcherFilter.java b/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/server/DispatcherFilter.java index 8466b44ad97..92fa59871a5 100644 --- a/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/server/DispatcherFilter.java +++ b/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/server/DispatcherFilter.java @@ -56,8 +56,9 @@ public class DispatcherFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - if (request instanceof HttpServletRequest req && response instanceof HttpServletResponse res) { - doFilter(req, res, chain); + if (request instanceof HttpServletRequest httpServletRequest + && response instanceof HttpServletResponse httpServletResponse) { + doFilter(httpServletRequest, httpServletResponse, chain); } else { chain.doFilter(request, response); diff --git a/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java b/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java index 7d7efe053ee..9507606aa90 100644 --- a/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java +++ b/module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java @@ -371,8 +371,9 @@ public class Restarter { } } - private boolean isFromRestartClassLoader(Object object) { - return (object instanceof Class cls && cls.getClassLoader() instanceof RestartClassLoader); + private boolean isFromRestartClassLoader(Object candidate) { + return (candidate instanceof Class candidateClass + && candidateClass.getClassLoader() instanceof RestartClassLoader); } /**