From 2494ecb47bedb9c82c2a4c906005eb1cff8d5c22 Mon Sep 17 00:00:00 2001 From: KNU-K Date: Sun, 17 Nov 2024 20:40:11 +0900 Subject: [PATCH 1/2] Simplify utility implementations in spring-core Closes gh-33903 --- .../util/FastByteArrayOutputStream.java | 32 ++++++++----------- .../springframework/util/MethodInvoker.java | 14 ++++---- .../org/springframework/util/MimeType.java | 4 +-- .../org/springframework/util/NumberUtils.java | 6 ++-- .../org/springframework/util/ObjectUtils.java | 18 ++++------- 5 files changed, 29 insertions(+), 45 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java index 7e7f1430c7f0..713dff9fb425 100644 --- a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java +++ b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java @@ -98,13 +98,11 @@ public void write(int datum) throws IOException { if (this.closed) { throw new IOException("Stream closed"); } - else { - if (this.buffers.peekLast() == null || this.buffers.getLast().length == this.index) { - addBuffer(1); - } - // store the byte - this.buffers.getLast()[this.index++] = (byte) datum; + if (this.buffers.peekLast() == null || this.buffers.getLast().length == this.index) { + addBuffer(1); } + // store the byte + this.buffers.getLast()[this.index++] = (byte) datum; } @Override @@ -384,22 +382,20 @@ public int read() { // This stream doesn't have any data in it... return -1; } + if (this.nextIndexInCurrentBuffer < this.currentBufferLength) { + this.totalBytesRead++; + return this.currentBuffer[this.nextIndexInCurrentBuffer++] & 0xFF; + } else { - if (this.nextIndexInCurrentBuffer < this.currentBufferLength) { - this.totalBytesRead++; - return this.currentBuffer[this.nextIndexInCurrentBuffer++] & 0xFF; + if (this.buffersIterator.hasNext()) { + this.currentBuffer = this.buffersIterator.next(); + updateCurrentBufferLength(); + this.nextIndexInCurrentBuffer = 0; } else { - if (this.buffersIterator.hasNext()) { - this.currentBuffer = this.buffersIterator.next(); - updateCurrentBufferLength(); - this.nextIndexInCurrentBuffer = 0; - } - else { - this.currentBuffer = null; - } - return read(); + this.currentBuffer = null; } + return read(); } } diff --git a/spring-core/src/main/java/org/springframework/util/MethodInvoker.java b/spring-core/src/main/java/org/springframework/util/MethodInvoker.java index 0725b5f7466f..e2f4a9cfa970 100644 --- a/spring-core/src/main/java/org/springframework/util/MethodInvoker.java +++ b/spring-core/src/main/java/org/springframework/util/MethodInvoker.java @@ -226,14 +226,12 @@ protected Method findMatchingMethod() { Method matchingMethod = null; for (Method candidate : candidates) { - if (candidate.getName().equals(targetMethod)) { - if (candidate.getParameterCount() == argCount) { - Class[] paramTypes = candidate.getParameterTypes(); - int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments); - if (typeDiffWeight < minTypeDiffWeight) { - minTypeDiffWeight = typeDiffWeight; - matchingMethod = candidate; - } + if (candidate.getName().equals(targetMethod) && candidate.getParameterCount() == argCount) { + Class[] paramTypes = candidate.getParameterTypes(); + int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments); + if (typeDiffWeight < minTypeDiffWeight) { + minTypeDiffWeight = typeDiffWeight; + matchingMethod = candidate; } } } diff --git a/spring-core/src/main/java/org/springframework/util/MimeType.java b/spring-core/src/main/java/org/springframework/util/MimeType.java index 1217c3bbc3df..d4b670718c7f 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeType.java +++ b/spring-core/src/main/java/org/springframework/util/MimeType.java @@ -243,9 +243,7 @@ private boolean isQuotedString(String s) { if (s.length() < 2) { return false; } - else { - return ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'"))); - } + return ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'"))); } protected String unquote(String s) { diff --git a/spring-core/src/main/java/org/springframework/util/NumberUtils.java b/spring-core/src/main/java/org/springframework/util/NumberUtils.java index 505088913a72..9b37793de624 100644 --- a/spring-core/src/main/java/org/springframework/util/NumberUtils.java +++ b/spring-core/src/main/java/org/springframework/util/NumberUtils.java @@ -110,10 +110,8 @@ else if (BigInteger.class == targetClass) { // do not lose precision - use BigDecimal's own conversion return (T) bigDecimal.toBigInteger(); } - else { - // original value is not a Big* number - use standard long conversion - return (T) BigInteger.valueOf(number.longValue()); - } + // original value is not a Big* number - use standard long conversion + return (T) BigInteger.valueOf(number.longValue()); } else if (Float.class == targetClass) { return (T) Float.valueOf(number.floatValue()); diff --git a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java index a4b5822351a6..6e4c35924a95 100644 --- a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java @@ -702,8 +702,7 @@ public static String nullSafeToString(@Nullable byte[] array) { if (array == null) { return NULL_STRING; } - int length = array.length; - if (length == 0) { + if (array.length == 0) { return EMPTY_ARRAY; } StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); @@ -726,8 +725,7 @@ public static String nullSafeToString(@Nullable char[] array) { if (array == null) { return NULL_STRING; } - int length = array.length; - if (length == 0) { + if (array.length == 0) { return EMPTY_ARRAY; } StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); @@ -750,8 +748,7 @@ public static String nullSafeToString(@Nullable double[] array) { if (array == null) { return NULL_STRING; } - int length = array.length; - if (length == 0) { + if (array.length == 0) { return EMPTY_ARRAY; } StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); @@ -774,8 +771,7 @@ public static String nullSafeToString(@Nullable float[] array) { if (array == null) { return NULL_STRING; } - int length = array.length; - if (length == 0) { + if (array.length == 0) { return EMPTY_ARRAY; } StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); @@ -798,8 +794,7 @@ public static String nullSafeToString(@Nullable int[] array) { if (array == null) { return NULL_STRING; } - int length = array.length; - if (length == 0) { + if (array.length == 0) { return EMPTY_ARRAY; } StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); @@ -846,8 +841,7 @@ public static String nullSafeToString(@Nullable short[] array) { if (array == null) { return NULL_STRING; } - int length = array.length; - if (length == 0) { + if (array.length == 0) { return EMPTY_ARRAY; } StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); From 7f7819329c952a98a6e4ed81d376b5e8ca610e79 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sun, 17 Nov 2024 15:14:16 +0100 Subject: [PATCH 2/2] Update copyright headers See gh-33903 --- .../org/springframework/util/FastByteArrayOutputStream.java | 2 +- .../src/main/java/org/springframework/util/MethodInvoker.java | 2 +- .../src/main/java/org/springframework/util/NumberUtils.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java index 713dff9fb425..e91eda569920 100644 --- a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java +++ b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.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. diff --git a/spring-core/src/main/java/org/springframework/util/MethodInvoker.java b/spring-core/src/main/java/org/springframework/util/MethodInvoker.java index e2f4a9cfa970..73171aec1de7 100644 --- a/spring-core/src/main/java/org/springframework/util/MethodInvoker.java +++ b/spring-core/src/main/java/org/springframework/util/MethodInvoker.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. diff --git a/spring-core/src/main/java/org/springframework/util/NumberUtils.java b/spring-core/src/main/java/org/springframework/util/NumberUtils.java index 9b37793de624..27bb5db53b90 100644 --- a/spring-core/src/main/java/org/springframework/util/NumberUtils.java +++ b/spring-core/src/main/java/org/springframework/util/NumberUtils.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.