Skip to content

Commit 6505c4b

Browse files
committed
Refine use of isArray() and componentType()
1 parent cc9b39b commit 6505c4b

File tree

6 files changed

+18
-22
lines changed

6 files changed

+18
-22
lines changed

spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,17 @@ private void processKeyedProperty(PropertyTokenHolder tokens, PropertyValue pv)
288288
String lastKey = tokens.keys[tokens.keys.length - 1];
289289

290290
if (propValue.getClass().isArray()) {
291-
Class<?> requiredType = propValue.getClass().componentType();
291+
Class<?> componentType = propValue.getClass().componentType();
292292
int arrayIndex = Integer.parseInt(lastKey);
293293
Object oldValue = null;
294294
try {
295295
if (isExtractOldValueForEditor() && arrayIndex < Array.getLength(propValue)) {
296296
oldValue = Array.get(propValue, arrayIndex);
297297
}
298298
Object convertedValue = convertIfNecessary(tokens.canonicalName, oldValue, pv.getValue(),
299-
requiredType, ph.nested(tokens.keys.length));
299+
componentType, ph.nested(tokens.keys.length));
300300
int length = Array.getLength(propValue);
301301
if (arrayIndex >= length && arrayIndex < this.autoGrowCollectionLimit) {
302-
Class<?> componentType = propValue.getClass().componentType();
303302
Object newArray = Array.newInstance(componentType, arrayIndex + 1);
304303
System.arraycopy(propValue, 0, newArray, 0, length);
305304
int lastKeyIndex = tokens.canonicalName.lastIndexOf('[');

spring-core/src/main/java/org/springframework/core/annotation/RepeatableContainers.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -211,7 +211,7 @@ private static class ExplicitRepeatableContainer extends RepeatableContainers {
211211
throw new NoSuchMethodException("No value method found");
212212
}
213213
Class<?> returnType = valueMethod.getReturnType();
214-
if (!returnType.isArray() || returnType.componentType() != repeatable) {
214+
if (returnType.componentType() != repeatable) {
215215
throw new AnnotationConfigurationException(
216216
"Container type [%s] must declare a 'value' attribute for an array of type [%s]"
217217
.formatted(container.getName(), repeatable.getName()));

spring-expression/src/main/java/org/springframework/expression/spel/CodeFlow.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -516,11 +516,9 @@ public static String createSignatureDescriptor(Constructor<?> ctor) {
516516
*/
517517
public static String toJvmDescriptor(Class<?> clazz) {
518518
StringBuilder sb = new StringBuilder();
519-
if (clazz.isArray()) {
520-
while (clazz.isArray()) {
521-
sb.append('[');
522-
clazz = clazz.componentType();
523-
}
519+
while (clazz.isArray()) {
520+
sb.append('[');
521+
clazz = clazz.componentType();
524522
}
525523
if (clazz.isPrimitive()) {
526524
if (clazz == boolean.class) {

spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/DestinationVariableArgumentResolver.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,8 +49,8 @@ public boolean resolve(
4949
collection.forEach(requestValues::addRouteVariable);
5050
return true;
5151
}
52-
else if (argument.getClass().isArray()) {
53-
for (Object variable : (Object[]) argument) {
52+
else if (argument instanceof Object[] arguments) {
53+
for (Object variable : arguments) {
5454
requestValues.addRouteVariable(variable);
5555
}
5656
return true;

spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -623,7 +623,7 @@ public boolean supports(Type genericType) {
623623
parameterizedType.getActualTypeArguments().length == 1) {
624624
Type typeArgument = parameterizedType.getActualTypeArguments()[0];
625625
if (typeArgument instanceof Class<?> classArgument) {
626-
return ((classArgument.isArray() && byte.class == classArgument.componentType()) ||
626+
return ((byte.class == classArgument.componentType()) ||
627627
isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) ||
628628
supportsInternal(classArgument, false));
629629
}

spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/AbstractMultiCheckedElementTag.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -208,11 +208,10 @@ protected int writeTagContent(TagWriter tagWriter) throws JspException {
208208
throw new IllegalArgumentException("Attribute 'items' is required and must be a Collection, an Array or a Map");
209209
}
210210

211-
if (itemsObject.getClass().isArray()) {
212-
Object[] itemsArray = (Object[]) itemsObject;
213-
for (int i = 0; i < itemsArray.length; i++) {
214-
Object item = itemsArray[i];
215-
writeObjectEntry(tagWriter, valueProperty, labelProperty, item, i);
211+
if (itemsObject instanceof Object[] itemsArray) {
212+
for (int itemIndex = 0; itemIndex < itemsArray.length; itemIndex++) {
213+
Object item = itemsArray[itemIndex];
214+
writeObjectEntry(tagWriter, valueProperty, labelProperty, item, itemIndex);
216215
}
217216
}
218217
else if (itemsObject instanceof Collection<?> optionCollection) {

0 commit comments

Comments
 (0)