diff --git a/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java b/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java index 5f07b9e4f93..f140b60c88d 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java @@ -404,24 +404,14 @@ class ConfigBeanDefinitionParser implements BeanDefinitionParser { */ private Class getAdviceClass(Element adviceElement, ParserContext parserContext) { String elementName = parserContext.getDelegate().getLocalName(adviceElement); - if (BEFORE.equals(elementName)) { - return AspectJMethodBeforeAdvice.class; - } - else if (AFTER.equals(elementName)) { - return AspectJAfterAdvice.class; - } - else if (AFTER_RETURNING_ELEMENT.equals(elementName)) { - return AspectJAfterReturningAdvice.class; - } - else if (AFTER_THROWING_ELEMENT.equals(elementName)) { - return AspectJAfterThrowingAdvice.class; - } - else if (AROUND.equals(elementName)) { - return AspectJAroundAdvice.class; - } - else { - throw new IllegalArgumentException("Unknown advice kind [" + elementName + "]."); - } + return switch (elementName) { + case BEFORE -> AspectJMethodBeforeAdvice.class; + case AFTER -> AspectJAfterAdvice.class; + case AFTER_RETURNING_ELEMENT -> AspectJAfterReturningAdvice.class; + case AFTER_THROWING_ELEMENT -> AspectJAfterThrowingAdvice.class; + case AROUND -> AspectJAroundAdvice.class; + default -> throw new IllegalArgumentException("Unknown advice kind [" + elementName + "]."); + }; } /** diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java index 535717ed400..321b8bb5b05 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java @@ -182,17 +182,12 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser { if (element.hasAttribute(SCOPED_PROXY_ATTRIBUTE)) { String mode = element.getAttribute(SCOPED_PROXY_ATTRIBUTE); - if ("targetClass".equals(mode)) { - scanner.setScopedProxyMode(ScopedProxyMode.TARGET_CLASS); - } - else if ("interfaces".equals(mode)) { - scanner.setScopedProxyMode(ScopedProxyMode.INTERFACES); - } - else if ("no".equals(mode)) { - scanner.setScopedProxyMode(ScopedProxyMode.NO); - } - else { - throw new IllegalArgumentException("scoped-proxy only supports 'no', 'interfaces' and 'targetClass'"); + switch (mode) { + case "targetClass" -> scanner.setScopedProxyMode(ScopedProxyMode.TARGET_CLASS); + case "interfaces" -> scanner.setScopedProxyMode(ScopedProxyMode.INTERFACES); + case "no" -> scanner.setScopedProxyMode(ScopedProxyMode.NO); + default -> + throw new IllegalArgumentException("scoped-proxy only supports 'no', 'interfaces' and 'targetClass'"); } } } @@ -234,28 +229,28 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser { String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE); String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE); expression = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(expression); - if ("annotation".equals(filterType)) { - return new AnnotationTypeFilter((Class) ClassUtils.forName(expression, classLoader)); - } - else if ("assignable".equals(filterType)) { - return new AssignableTypeFilter(ClassUtils.forName(expression, classLoader)); - } - else if ("aspectj".equals(filterType)) { - return new AspectJTypeFilter(expression, classLoader); - } - else if ("regex".equals(filterType)) { - return new RegexPatternTypeFilter(Pattern.compile(expression)); - } - else if ("custom".equals(filterType)) { - Class filterClass = ClassUtils.forName(expression, classLoader); - if (!TypeFilter.class.isAssignableFrom(filterClass)) { - throw new IllegalArgumentException( - "Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression); + switch (filterType) { + case "annotation" -> { + return new AnnotationTypeFilter((Class) ClassUtils.forName(expression, classLoader)); } - return (TypeFilter) BeanUtils.instantiateClass(filterClass); - } - else { - throw new IllegalArgumentException("Unsupported filter type: " + filterType); + case "assignable" -> { + return new AssignableTypeFilter(ClassUtils.forName(expression, classLoader)); + } + case "aspectj" -> { + return new AspectJTypeFilter(expression, classLoader); + } + case "regex" -> { + return new RegexPatternTypeFilter(Pattern.compile(expression)); + } + case "custom" -> { + Class filterClass = ClassUtils.forName(expression, classLoader); + if (!TypeFilter.class.isAssignableFrom(filterClass)) { + throw new IllegalArgumentException( + "Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression); + } + return (TypeFilter) BeanUtils.instantiateClass(filterClass); + } + default -> throw new IllegalArgumentException("Unsupported filter type: " + filterType); } } diff --git a/spring-context/src/main/java/org/springframework/scheduling/config/ExecutorBeanDefinitionParser.java b/spring-context/src/main/java/org/springframework/scheduling/config/ExecutorBeanDefinitionParser.java index 7c53292786a..875ba05641e 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/config/ExecutorBeanDefinitionParser.java +++ b/spring-context/src/main/java/org/springframework/scheduling/config/ExecutorBeanDefinitionParser.java @@ -61,22 +61,13 @@ public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionPa return; } String prefix = "java.util.concurrent.ThreadPoolExecutor."; - String policyClassName; - if (rejectionPolicy.equals("ABORT")) { - policyClassName = prefix + "AbortPolicy"; - } - else if (rejectionPolicy.equals("CALLER_RUNS")) { - policyClassName = prefix + "CallerRunsPolicy"; - } - else if (rejectionPolicy.equals("DISCARD")) { - policyClassName = prefix + "DiscardPolicy"; - } - else if (rejectionPolicy.equals("DISCARD_OLDEST")) { - policyClassName = prefix + "DiscardOldestPolicy"; - } - else { - policyClassName = rejectionPolicy; - } + String policyClassName = switch (rejectionPolicy) { + case "ABORT" -> prefix + "AbortPolicy"; + case "CALLER_RUNS" -> prefix + "CallerRunsPolicy"; + case "DISCARD" -> prefix + "DiscardPolicy"; + case "DISCARD_OLDEST" -> prefix + "DiscardOldestPolicy"; + default -> rejectionPolicy; + }; builder.addPropertyValue("rejectedExecutionHandler", new RootBeanDefinition(policyClassName)); } diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java index 3f49f0ff032..c5c41a2b426 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java @@ -1778,19 +1778,13 @@ class SpelReproTests extends AbstractExpressionTests { @Override public Object resolve(EvaluationContext context, String beanName) throws AccessException { - if (beanName.equals("foo")) { - return "custard"; - } - else if (beanName.equals("foo.bar")) { - return "trouble"; - } - else if (beanName.equals("&foo")) { - return "foo factory"; - } - else if (beanName.equals("goo")) { - throw new AccessException("DONT ASK ME ABOUT GOO"); - } - return null; + return switch (beanName) { + case "foo" -> "custard"; + case "foo.bar" -> "trouble"; + case "&foo" -> "foo factory"; + case "goo" -> throw new AccessException("DONT ASK ME ABOUT GOO"); + default -> null; + }; } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java index 8abbe8f9153..3ab884013d6 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java @@ -242,19 +242,17 @@ public abstract class JdbcUtils { // Corresponding SQL types for JSR-310 / Joda-Time types, left up // to the caller to convert them (e.g. through a ConversionService). String typeName = requiredType.getSimpleName(); - if ("LocalDate".equals(typeName)) { - return rs.getDate(index); - } - else if ("LocalTime".equals(typeName)) { - return rs.getTime(index); - } - else if ("LocalDateTime".equals(typeName)) { - return rs.getTimestamp(index); - } + return switch (typeName) { + case "LocalDate" -> rs.getDate(index); + case "LocalTime" -> rs.getTime(index); + case "LocalDateTime" -> rs.getTimestamp(index); + default -> + + // Fall back to getObject without type specification, again + // left up to the caller to convert the value if necessary. + getResultSetValue(rs, index); + }; - // Fall back to getObject without type specification, again - // left up to the caller to convert the value if necessary. - return getResultSetValue(rs, index); } // Perform was-null check if necessary (for results that the JDBC driver returns as primitives). diff --git a/spring-web/src/main/java/org/springframework/web/context/request/FacesRequestAttributes.java b/spring-web/src/main/java/org/springframework/web/context/request/FacesRequestAttributes.java index 14df22d3b0e..f68599e6cf3 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/FacesRequestAttributes.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/FacesRequestAttributes.java @@ -131,60 +131,26 @@ public class FacesRequestAttributes implements RequestAttributes { @Override public Object resolveReference(String key) { - if (REFERENCE_REQUEST.equals(key)) { - return getExternalContext().getRequest(); - } - else if (REFERENCE_SESSION.equals(key)) { - return getExternalContext().getSession(true); - } - else if ("application".equals(key)) { - return getExternalContext().getContext(); - } - else if ("requestScope".equals(key)) { - return getExternalContext().getRequestMap(); - } - else if ("sessionScope".equals(key)) { - return getExternalContext().getSessionMap(); - } - else if ("applicationScope".equals(key)) { - return getExternalContext().getApplicationMap(); - } - else if ("facesContext".equals(key)) { - return getFacesContext(); - } - else if ("cookie".equals(key)) { - return getExternalContext().getRequestCookieMap(); - } - else if ("header".equals(key)) { - return getExternalContext().getRequestHeaderMap(); - } - else if ("headerValues".equals(key)) { - return getExternalContext().getRequestHeaderValuesMap(); - } - else if ("param".equals(key)) { - return getExternalContext().getRequestParameterMap(); - } - else if ("paramValues".equals(key)) { - return getExternalContext().getRequestParameterValuesMap(); - } - else if ("initParam".equals(key)) { - return getExternalContext().getInitParameterMap(); - } - else if ("view".equals(key)) { - return getFacesContext().getViewRoot(); - } - else if ("viewScope".equals(key)) { - return getFacesContext().getViewRoot().getViewMap(); - } - else if ("flash".equals(key)) { - return getExternalContext().getFlash(); - } - else if ("resource".equals(key)) { - return getFacesContext().getApplication().getResourceHandler(); - } - else { - return null; - } + return switch (key) { + case REFERENCE_REQUEST -> getExternalContext().getRequest(); + case REFERENCE_SESSION -> getExternalContext().getSession(true); + case "application" -> getExternalContext().getContext(); + case "requestScope" -> getExternalContext().getRequestMap(); + case "sessionScope" -> getExternalContext().getSessionMap(); + case "applicationScope" -> getExternalContext().getApplicationMap(); + case "facesContext" -> getFacesContext(); + case "cookie" -> getExternalContext().getRequestCookieMap(); + case "header" -> getExternalContext().getRequestHeaderMap(); + case "headerValues" -> getExternalContext().getRequestHeaderValuesMap(); + case "param" -> getExternalContext().getRequestParameterMap(); + case "paramValues" -> getExternalContext().getRequestParameterValuesMap(); + case "initParam" -> getExternalContext().getInitParameterMap(); + case "view" -> getFacesContext().getViewRoot(); + case "viewScope" -> getFacesContext().getViewRoot().getViewMap(); + case "flash" -> getExternalContext().getFlash(); + case "resource" -> getFacesContext().getApplication().getResourceHandler(); + default -> null; + }; } @Override diff --git a/spring-web/src/main/java/org/springframework/web/util/TagUtils.java b/spring-web/src/main/java/org/springframework/web/util/TagUtils.java index d299c11de56..6f9eb3a930a 100644 --- a/spring-web/src/main/java/org/springframework/web/util/TagUtils.java +++ b/spring-web/src/main/java/org/springframework/web/util/TagUtils.java @@ -67,18 +67,12 @@ public abstract class TagUtils { */ public static int getScope(String scope) { Assert.notNull(scope, "Scope to search for cannot be null"); - if (scope.equals(SCOPE_REQUEST)) { - return PageContext.REQUEST_SCOPE; - } - else if (scope.equals(SCOPE_SESSION)) { - return PageContext.SESSION_SCOPE; - } - else if (scope.equals(SCOPE_APPLICATION)) { - return PageContext.APPLICATION_SCOPE; - } - else { - return PageContext.PAGE_SCOPE; - } + return switch (scope) { + case SCOPE_REQUEST -> PageContext.REQUEST_SCOPE; + case SCOPE_SESSION -> PageContext.SESSION_SCOPE; + case SCOPE_APPLICATION -> PageContext.APPLICATION_SCOPE; + default -> PageContext.PAGE_SCOPE; + }; } /**