From ee3e1591de7552046f8eec079cf02442fe63f949 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 14 May 2024 13:03:35 +0200 Subject: [PATCH] Polishing --- .../aop/aspectj/annotation/AspectMetadata.java | 16 +++++++++++----- .../util/MultiValueMapAdapter.java | 6 +++--- .../org/springframework/util/StringUtils.java | 11 ++++++----- .../rowset/ResultSetWrappingSqlRowSet.java | 4 ++-- .../jms/core/JmsMessagingTemplate.java | 7 +------ .../springframework/jms/core/JmsOperations.java | 14 +++++++------- .../core/AbstractMessageSendingTemplate.java | 5 ++--- .../org/springframework/http/ResponseEntity.java | 3 +-- .../HttpComponentsClientHttpRequest.java | 5 +---- .../servlet/support/AbstractFlashMapManager.java | 4 ++-- 10 files changed, 36 insertions(+), 39 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java index 969ec866eeb..a70aed625cc 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 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. @@ -124,10 +124,16 @@ public class AspectMetadata implements Serializable { * Extract contents from String of form {@code pertarget(contents)}. */ private String findPerClause(Class aspectClass) { - String str = aspectClass.getAnnotation(Aspect.class).value(); - int beginIndex = str.indexOf('(') + 1; - int endIndex = str.length() - 1; - return str.substring(beginIndex, endIndex); + Aspect ann = aspectClass.getAnnotation(Aspect.class); + if (ann == null) { + return ""; + } + String value = ann.value(); + int beginIndex = value.indexOf('('); + if (beginIndex < 0) { + return ""; + } + return value.substring(beginIndex + 1, value.length() - 1); } diff --git a/spring-core/src/main/java/org/springframework/util/MultiValueMapAdapter.java b/spring-core/src/main/java/org/springframework/util/MultiValueMapAdapter.java index 8c158ecf0fc..4c7c2f1d133 100644 --- a/spring-core/src/main/java/org/springframework/util/MultiValueMapAdapter.java +++ b/spring-core/src/main/java/org/springframework/util/MultiValueMapAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -59,7 +59,7 @@ public class MultiValueMapAdapter implements MultiValueMap, Serializ @Nullable public V getFirst(K key) { List values = this.targetMap.get(key); - return (values != null && !values.isEmpty() ? values.get(0) : null); + return (!CollectionUtils.isEmpty(values) ? values.get(0) : null); } @Override @@ -95,7 +95,7 @@ public class MultiValueMapAdapter implements MultiValueMap, Serializ public Map toSingleValueMap() { Map singleValueMap = CollectionUtils.newLinkedHashMap(this.targetMap.size()); this.targetMap.forEach((key, values) -> { - if (values != null && !values.isEmpty()) { + if (!CollectionUtils.isEmpty(values)) { singleValueMap.put(key, values.get(0)); } }); diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 8936c605bab..93c6dc21616 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -123,7 +123,7 @@ public abstract class StringUtils { * @see #hasText(CharSequence) */ public static boolean hasLength(@Nullable CharSequence str) { - return (str != null && str.length() > 0); + return (str != null && !str.isEmpty()); // as of JDK 15 } /** @@ -791,6 +791,7 @@ public abstract class StringUtils { * and {@code "0"} through {@code "9"} stay the same. *
  • Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.
  • *
  • A sequence "{@code %xy}" is interpreted as a hexadecimal representation of the character.
  • + *
  • For all other characters (including those already decoded), the output is undefined.
  • * * @param source the encoded String * @param charset the character set @@ -839,7 +840,7 @@ public abstract class StringUtils { * the {@link Locale#toString} format as well as BCP 47 language tags as * specified by {@link Locale#forLanguageTag}. * @param localeValue the locale value: following either {@code Locale's} - * {@code toString()} format ("en", "en_UK", etc), also accepting spaces as + * {@code toString()} format ("en", "en_UK", etc.), also accepting spaces as * separators (as an alternative to underscores), or BCP 47 (e.g. "en-UK") * @return a corresponding {@code Locale} instance, or {@code null} if none * @throws IllegalArgumentException in case of an invalid locale specification @@ -868,7 +869,7 @@ public abstract class StringUtils { *

    Note: This delegate does not accept the BCP 47 language tag format. * Please use {@link #parseLocale} for lenient parsing of both formats. * @param localeString the locale {@code String}: following {@code Locale's} - * {@code toString()} format ("en", "en_UK", etc), also accepting spaces as + * {@code toString()} format ("en", "en_UK", etc.), also accepting spaces as * separators (as an alternative to underscores) * @return a corresponding {@code Locale} instance, or {@code null} if none * @throws IllegalArgumentException in case of an invalid locale specification @@ -876,7 +877,7 @@ public abstract class StringUtils { @SuppressWarnings("deprecation") // for Locale constructors on JDK 19 @Nullable public static Locale parseLocaleString(String localeString) { - if (localeString.equals("")) { + if (localeString.isEmpty()) { return null; } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/rowset/ResultSetWrappingSqlRowSet.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/rowset/ResultSetWrappingSqlRowSet.java index d9b68de09bc..56d3accb10d 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/rowset/ResultSetWrappingSqlRowSet.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/rowset/ResultSetWrappingSqlRowSet.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -32,7 +32,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.CollectionUtils; /** - * The default implementation of Spring's {@link SqlRowSet} interface, wrapping a + * The common implementation of Spring's {@link SqlRowSet} interface, wrapping a * {@link java.sql.ResultSet}, catching any {@link SQLException SQLExceptions} and * translating them to a corresponding Spring {@link InvalidResultSetAccessException}. * diff --git a/spring-jms/src/main/java/org/springframework/jms/core/JmsMessagingTemplate.java b/spring-jms/src/main/java/org/springframework/jms/core/JmsMessagingTemplate.java index af7e0e5e24e..5c0aeb27b95 100644 --- a/spring-jms/src/main/java/org/springframework/jms/core/JmsMessagingTemplate.java +++ b/spring-jms/src/main/java/org/springframework/jms/core/JmsMessagingTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 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. @@ -189,11 +189,6 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate } } - @Override - public void convertAndSend(Object payload) throws MessagingException { - convertAndSend(payload, null); - } - @Override public void convertAndSend(Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException { Destination defaultDestination = getDefaultDestination(); diff --git a/spring-jms/src/main/java/org/springframework/jms/core/JmsOperations.java b/spring-jms/src/main/java/org/springframework/jms/core/JmsOperations.java index 845be02a12e..0aa7888819a 100644 --- a/spring-jms/src/main/java/org/springframework/jms/core/JmsOperations.java +++ b/spring-jms/src/main/java/org/springframework/jms/core/JmsOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2024 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. @@ -290,7 +290,7 @@ public interface JmsOperations { *

    This method should be used carefully, since it will block the thread * until the message becomes available or until the timeout value is exceeded. *

    This will only work with a default destination specified! - * @return the message produced for the consumer or {@code null} if the timeout expires. + * @return the message produced for the consumer, or {@code null} if the timeout expires * @throws JmsException checked JMSException converted to unchecked */ @Nullable @@ -303,7 +303,7 @@ public interface JmsOperations { *

    This method should be used carefully, since it will block the thread * until the message becomes available or until the timeout value is exceeded. * @param destination the destination to receive a message from - * @return the message produced for the consumer or {@code null} if the timeout expires. + * @return the message produced for the consumer, or {@code null} if the timeout expires * @throws JmsException checked JMSException converted to unchecked */ @Nullable @@ -317,7 +317,7 @@ public interface JmsOperations { * until the message becomes available or until the timeout value is exceeded. * @param destinationName the name of the destination to send this message to * (to be resolved to an actual destination by a DestinationResolver) - * @return the message produced for the consumer or {@code null} if the timeout expires. + * @return the message produced for the consumer, or {@code null} if the timeout expires * @throws JmsException checked JMSException converted to unchecked */ @Nullable @@ -332,7 +332,7 @@ public interface JmsOperations { *

    This will only work with a default destination specified! * @param messageSelector the JMS message selector expression (or {@code null} if none). * See the JMS specification for a detailed definition of selector expressions. - * @return the message produced for the consumer or {@code null} if the timeout expires. + * @return the message produced for the consumer, or {@code null} if the timeout expires * @throws JmsException checked JMSException converted to unchecked */ @Nullable @@ -347,7 +347,7 @@ public interface JmsOperations { * @param destination the destination to receive a message from * @param messageSelector the JMS message selector expression (or {@code null} if none). * See the JMS specification for a detailed definition of selector expressions. - * @return the message produced for the consumer or {@code null} if the timeout expires. + * @return the message produced for the consumer, or {@code null} if the timeout expires * @throws JmsException checked JMSException converted to unchecked */ @Nullable @@ -363,7 +363,7 @@ public interface JmsOperations { * (to be resolved to an actual destination by a DestinationResolver) * @param messageSelector the JMS message selector expression (or {@code null} if none). * See the JMS specification for a detailed definition of selector expressions. - * @return the message produced for the consumer or {@code null} if the timeout expires. + * @return the message produced for the consumer, or {@code null} if the timeout expires * @throws JmsException checked JMSException converted to unchecked */ @Nullable diff --git a/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java b/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java index b4cf36ae009..f355b343216 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -168,8 +168,7 @@ public abstract class AbstractMessageSendingTemplate implements MessageSendin Map headersToUse = processHeadersToSend(headers); if (headersToUse != null) { - messageHeaders = (headersToUse instanceof MessageHeaders _messageHeaders ? - _messageHeaders : new MessageHeaders(headersToUse)); + messageHeaders = (headersToUse instanceof MessageHeaders mh ? mh : new MessageHeaders(headersToUse)); } MessageConverter converter = getMessageConverter(); diff --git a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java index 360f0321c33..c96065ab72c 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -277,7 +277,6 @@ public class ResponseEntity extends HttpEntity { */ public static HeadersBuilder of(ProblemDetail body) { return new DefaultBuilder(body.getStatus()) { - @SuppressWarnings("unchecked") @Override public ResponseEntity build() { diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java index 9a87c252abf..596e649202e 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -124,7 +124,6 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest { @Override protected void applyHeaders() { HttpHeaders headers = getHeaders(); - headers.entrySet() .stream() .filter(entry -> !HttpHeaders.CONTENT_LENGTH.equals(entry.getKey())) @@ -133,7 +132,6 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest { if (!this.httpRequest.containsHeader(HttpHeaders.ACCEPT)) { this.httpRequest.addHeader(HttpHeaders.ACCEPT, MediaType.ALL_VALUE); } - this.contentLength = headers.getContentLength(); } @@ -144,7 +142,6 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest { } CookieStore cookieStore = this.context.getCookieStore(); - getCookies().values() .stream() .flatMap(Collection::stream) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java index 10b46f1923b..38ee6c19df1 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2024 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. @@ -220,7 +220,7 @@ public abstract class AbstractFlashMapManager implements FlashMapManager { @Nullable private String decodeAndNormalizePath(@Nullable String path, HttpServletRequest request) { - if (path != null && !path.isEmpty()) { + if (StringUtils.hasLength(path)) { path = getUrlPathHelper().decodeRequestString(request, path); if (path.charAt(0) != '/') { String requestUri = getUrlPathHelper().getRequestUri(request);