From f88c314e834d5d5150d1ee444b0a8d96608b1309 Mon Sep 17 00:00:00 2001 From: Patrick Strawderman Date: Tue, 3 Sep 2024 23:34:19 -0700 Subject: [PATCH] Avoid empty array allocations in ResolvableType Avoid allocating empty arrays in a few places in ResolvableType. Closes gh-33473 --- .../springframework/core/ResolvableType.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/ResolvableType.java b/spring-core/src/main/java/org/springframework/core/ResolvableType.java index ed541ebbda63..bc76795032eb 100644 --- a/spring-core/src/main/java/org/springframework/core/ResolvableType.java +++ b/spring-core/src/main/java/org/springframework/core/ResolvableType.java @@ -549,9 +549,14 @@ public ResolvableType[] getInterfaces() { ResolvableType[] interfaces = this.interfaces; if (interfaces == null) { Type[] genericIfcs = resolved.getGenericInterfaces(); - interfaces = new ResolvableType[genericIfcs.length]; - for (int i = 0; i < genericIfcs.length; i++) { - interfaces[i] = forType(genericIfcs[i], this); + if (genericIfcs.length > 0) { + interfaces = new ResolvableType[genericIfcs.length]; + for (int i = 0; i < genericIfcs.length; i++) { + interfaces[i] = forType(genericIfcs[i], this); + } + } + else { + interfaces = EMPTY_TYPES_ARRAY; } this.interfaces = interfaces; } @@ -789,16 +794,26 @@ public ResolvableType[] getGenerics() { if (generics == null) { if (this.type instanceof Class clazz) { Type[] typeParams = clazz.getTypeParameters(); - generics = new ResolvableType[typeParams.length]; - for (int i = 0; i < generics.length; i++) { - generics[i] = ResolvableType.forType(typeParams[i], this); + if (typeParams.length > 0) { + generics = new ResolvableType[typeParams.length]; + for (int i = 0; i < generics.length; i++) { + generics[i] = ResolvableType.forType(typeParams[i], this); + } + } + else { + generics = EMPTY_TYPES_ARRAY; } } else if (this.type instanceof ParameterizedType parameterizedType) { Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); - generics = new ResolvableType[actualTypeArguments.length]; - for (int i = 0; i < actualTypeArguments.length; i++) { - generics[i] = forType(actualTypeArguments[i], this.variableResolver); + if (actualTypeArguments.length > 0) { + generics = new ResolvableType[actualTypeArguments.length]; + for (int i = 0; i < actualTypeArguments.length; i++) { + generics[i] = forType(actualTypeArguments[i], this.variableResolver); + } + } + else { + generics = EMPTY_TYPES_ARRAY; } } else {