From eb50a6f4a0e8e4924f90aac14d0769eefe64923b Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 10 May 2022 16:58:27 +0200 Subject: [PATCH] Further polishing regarding JDK baseline upgrade See gh-28440 --- .../AspectJAwareAdvisorAutoProxyCreator.java | 6 ++-- .../aop/framework/ProxyFactoryBean.java | 15 +++++----- .../aop/scope/ScopedProxyUtils.java | 2 +- .../aop/support/AbstractPointcutAdvisor.java | 2 +- .../AbstractNestablePropertyAccessor.java | 28 ++++++++----------- .../beans/factory/BeanFactoryUtils.java | 20 ++++++------- .../mock/web/MockHttpServletResponse.java | 6 ++-- .../org/springframework/http/HttpMethod.java | 7 +++-- .../servlet/MockHttpServletResponse.java | 4 +-- .../RequestMappingHandlerAdapter.java | 4 +-- .../RequestMappingHandlerAdapter.java | 2 +- 11 files changed, 45 insertions(+), 51 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java index b95e07fb2c0..e721a38f00c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -100,8 +100,8 @@ public class AspectJAwareAdvisorAutoProxyCreator extends AbstractAdvisorAutoProx // TODO: Consider optimization by caching the list of the aspect names List candidateAdvisors = findCandidateAdvisors(); for (Advisor advisor : candidateAdvisors) { - if (advisor instanceof AspectJPointcutAdvisor && - ((AspectJPointcutAdvisor) advisor).getAspectName().equals(beanName)) { + if (advisor instanceof AspectJPointcutAdvisor pointcutAdvisor && + pointcutAdvisor.getAspectName().equals(beanName)) { return true; } } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java index b8a4712e618..64975e98aff 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -436,12 +436,11 @@ public class ProxyFactoryBean extends ProxyCreatorSupport // Materialize interceptor chain from bean names. for (String name : this.interceptorNames) { if (name.endsWith(GLOBAL_SUFFIX)) { - if (!(this.beanFactory instanceof ListableBeanFactory)) { + if (!(this.beanFactory instanceof ListableBeanFactory lbf)) { throw new AopConfigException( "Can only use global advisors or interceptors with a ListableBeanFactory"); } - addGlobalAdvisors((ListableBeanFactory) this.beanFactory, - name.substring(0, name.length() - GLOBAL_SUFFIX.length())); + addGlobalAdvisors(lbf, name.substring(0, name.length() - GLOBAL_SUFFIX.length())); } else { @@ -475,16 +474,16 @@ public class ProxyFactoryBean extends ProxyCreatorSupport Advisor[] advisors = getAdvisors(); List freshAdvisors = new ArrayList<>(advisors.length); for (Advisor advisor : advisors) { - if (advisor instanceof PrototypePlaceholderAdvisor pa) { + if (advisor instanceof PrototypePlaceholderAdvisor ppa) { if (logger.isDebugEnabled()) { - logger.debug("Refreshing bean named '" + pa.getBeanName() + "'"); + logger.debug("Refreshing bean named '" + ppa.getBeanName() + "'"); } // Replace the placeholder with a fresh prototype instance resulting from a getBean lookup if (this.beanFactory == null) { throw new IllegalStateException("No BeanFactory available anymore (probably due to " + - "serialization) - cannot resolve prototype advisor '" + pa.getBeanName() + "'"); + "serialization) - cannot resolve prototype advisor '" + ppa.getBeanName() + "'"); } - Object bean = this.beanFactory.getBean(pa.getBeanName()); + Object bean = this.beanFactory.getBean(ppa.getBeanName()); Advisor refreshedAdvisor = namedBeanToAdvisor(bean); freshAdvisors.add(refreshedAdvisor); } diff --git a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java index 646e41f15f8..968cea81833 100644 --- a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java index 360ecc458fa..6937e31ce27 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java index bcb00e6925e..5310bd4688e 100644 --- a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,6 +60,7 @@ import org.springframework.util.StringUtils; * @author Stephane Nicoll * @author Rod Johnson * @author Rob Harrop + * @author Sam Brannen * @since 4.2 * @see #registerCustomEditor * @see #setPropertyValues @@ -279,7 +280,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA } } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "rawtypes"}) private void processKeyedProperty(PropertyTokenHolder tokens, PropertyValue pv) { Object propValue = getPropertyHoldingValue(tokens); PropertyHandler ph = getLocalPropertyHandler(tokens.actualName); @@ -318,9 +319,8 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA } } - else if (propValue instanceof List) { + else if (propValue instanceof List list) { Class requiredType = ph.getCollectionType(tokens.keys.length); - List list = (List) propValue; int index = Integer.parseInt(lastKey); Object oldValue = null; if (isExtractOldValueForEditor() && index < list.size()) { @@ -354,10 +354,9 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA } } - else if (propValue instanceof Map) { + else if (propValue instanceof Map map) { Class mapKeyType = ph.getMapKeyType(tokens.keys.length); Class mapValueType = ph.getMapValueType(tokens.keys.length); - Map map = (Map) propValue; // IMPORTANT: Do not pass full property name in here - property editors // must not kick in for map keys but rather only for map values. TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(mapKeyType); @@ -446,8 +445,8 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA oldValue = ph.getValue(); } catch (Exception ex) { - if (ex instanceof PrivilegedActionException) { - ex = ((PrivilegedActionException) ex).getException(); + if (ex instanceof PrivilegedActionException pae) { + ex = pae.getException(); } if (logger.isDebugEnabled()) { logger.debug("Could not read previous value of property '" + @@ -617,7 +616,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA return nestedPa.getPropertyValue(tokens); } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "rawtypes"}) @Nullable protected Object getPropertyValue(PropertyTokenHolder tokens) throws BeansException { String propertyName = tokens.canonicalName; @@ -653,15 +652,13 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA value = growArrayIfNecessary(value, index, indexedPropertyName.toString()); value = Array.get(value, index); } - else if (value instanceof List) { + else if (value instanceof List list) { int index = Integer.parseInt(key); - List list = (List) value; growCollectionIfNecessary(list, index, indexedPropertyName.toString(), ph, i + 1); value = list.get(index); } - else if (value instanceof Set) { + else if (value instanceof Set set) { // Apply index to Iterator in case of a Set. - Set set = (Set) value; int index = Integer.parseInt(key); if (index < 0 || index >= set.size()) { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, @@ -677,8 +674,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA } } } - else if (value instanceof Map) { - Map map = (Map) value; + else if (value instanceof Map map) { Class mapKeyType = ph.getResolvableType().getNested(i + 1).asMap().resolveGeneric(0); // IMPORTANT: Do not pass full property name in here - property editors // must not kick in for map keys but rather only for map values. @@ -841,7 +837,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA PropertyTokenHolder tokens = getPropertyNameTokens(nestedProperty); String canonicalName = tokens.canonicalName; Object value = getPropertyValue(tokens); - if (value == null || (value instanceof Optional optional && optional.isEmpty()) { + if (value == null || (value instanceof Optional optional && optional.isEmpty())) { if (isAutoGrowNestedPaths()) { value = setDefaultValue(tokens); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java b/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java index adf9a4537b9..07976017703 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -198,9 +198,9 @@ public abstract class BeanFactoryUtils { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit); if (lbf instanceof HierarchicalBeanFactory hbf) { - if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { + if (hbf.getParentBeanFactory() instanceof ListableBeanFactory pbf) { String[] parentResult = beanNamesForTypeIncludingAncestors( - (ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit); + pbf, type, includeNonSingletons, allowEagerInit); result = mergeNamesWithParent(result, parentResult, hbf); } } @@ -259,9 +259,9 @@ public abstract class BeanFactoryUtils { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit); if (lbf instanceof HierarchicalBeanFactory hbf) { - if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { + if (hbf.getParentBeanFactory() instanceof ListableBeanFactory pbf) { String[] parentResult = beanNamesForTypeIncludingAncestors( - (ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit); + pbf, type, includeNonSingletons, allowEagerInit); result = mergeNamesWithParent(result, parentResult, hbf); } } @@ -284,9 +284,8 @@ public abstract class BeanFactoryUtils { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForAnnotation(annotationType); if (lbf instanceof HierarchicalBeanFactory hbf) { - if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { - String[] parentResult = beanNamesForAnnotationIncludingAncestors( - (ListableBeanFactory) hbf.getParentBeanFactory(), annotationType); + if (hbf.getParentBeanFactory() instanceof ListableBeanFactory pbf) { + String[] parentResult = beanNamesForAnnotationIncludingAncestors(pbf, annotationType); result = mergeNamesWithParent(result, parentResult, hbf); } } @@ -321,9 +320,8 @@ public abstract class BeanFactoryUtils { Map result = new LinkedHashMap<>(4); result.putAll(lbf.getBeansOfType(type)); if (lbf instanceof HierarchicalBeanFactory hbf) { - if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { - Map parentResult = beansOfTypeIncludingAncestors( - (ListableBeanFactory) hbf.getParentBeanFactory(), type); + if (hbf.getParentBeanFactory() instanceof ListableBeanFactory pbf) { + Map parentResult = beansOfTypeIncludingAncestors(pbf, type); parentResult.forEach((beanName, beanInstance) -> { if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) { result.put(beanName, beanInstance); diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 397cec96167..76ae3df57f8 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -423,7 +423,7 @@ public class MockHttpServletResponse implements HttpServletResponse { buf.append("; Domain=").append(cookie.getDomain()); } int maxAge = cookie.getMaxAge(); - ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null); + ZonedDateTime expires = (cookie instanceof MockCookie mockCookie? mockCookie.getExpires() : null); if (maxAge >= 0) { buf.append("; Max-Age=").append(maxAge); buf.append("; Expires="); @@ -692,7 +692,7 @@ public class MockHttpServletResponse implements HttpServletResponse { return true; } else if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { - setContentLength(value instanceof Number ? ((Number) value).intValue() : + setContentLength(value instanceof Number number ? number.intValue() : Integer.parseInt(value.toString())); return true; } diff --git a/spring-web/src/main/java/org/springframework/http/HttpMethod.java b/spring-web/src/main/java/org/springframework/http/HttpMethod.java index 327d3796789..c71d54cd81b 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpMethod.java +++ b/spring-web/src/main/java/org/springframework/http/HttpMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -177,8 +177,8 @@ public final class HttpMethod implements Comparable, Serializable { if (this == o) { return true; } - else if (o instanceof HttpMethod other) { - return this.name.equals(other.name); + else if (o instanceof HttpMethod otherMethod) { + return this.name.equals(otherMethod.name); } return false; } @@ -187,4 +187,5 @@ public final class HttpMethod implements Comparable, Serializable { public String toString() { return this.name; } + } diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java index 6f52b363103..6aa3ef3befa 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -423,7 +423,7 @@ public class MockHttpServletResponse implements HttpServletResponse { buf.append("; Domain=").append(cookie.getDomain()); } int maxAge = cookie.getMaxAge(); - ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null); + ZonedDateTime expires = (cookie instanceof MockCookie mockCookie? mockCookie.getExpires() : null); if (maxAge >= 0) { buf.append("; Max-Age=").append(maxAge); buf.append("; Expires="); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java index 7f970f670b6..ceb8085ed61 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java @@ -149,8 +149,8 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, Application */ @Override public void setApplicationContext(ApplicationContext applicationContext) { - if (applicationContext instanceof ConfigurableApplicationContext) { - this.applicationContext = (ConfigurableApplicationContext) applicationContext; + if (applicationContext instanceof ConfigurableApplicationContext cac) { + this.applicationContext = cac; } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index 0aad286c6cb..5de02d75617 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.