Skip to content

Commit 1174c4a

Browse files
committed
GH-1713 - Fix primitive type and array handling in FormattableType.
1 parent af1e50f commit 1174c4a

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

spring-modulith-core/src/main/java/org/springframework/modulith/core/FormattableType.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ private FormattableType(String type) {
6969
this.type = type;
7070
this.abbreviatedName = SingletonSupplier.of(() -> {
7171

72+
String packageName = ClassUtils.getPackageName(type);
73+
74+
if (!StringUtils.hasText(packageName)) {
75+
return ClassUtils.getShortName(type);
76+
}
77+
7278
String abbreviatedPackage = Stream //
73-
.of(ClassUtils.getPackageName(type).split("\\.")) //
79+
.of(packageName.split("\\.")) //
7480
.map(it -> it.substring(0, 1)) //
7581
.collect(Collectors.joining("."));
7682

@@ -89,7 +95,7 @@ public static FormattableType of(JavaClass type) {
8995

9096
Assert.notNull(type, "JavaClass must not be null!");
9197

92-
return CACHE.computeIfAbsent(type.getName(), FormattableType::new);
98+
return of(type.reflect());
9399
}
94100

95101
/**
@@ -99,7 +105,7 @@ public static FormattableType of(JavaClass type) {
99105
* @return will never be {@literal null}.
100106
*/
101107
public static FormattableType of(Class<?> type) {
102-
return CACHE.computeIfAbsent(type.getName(), FormattableType::new);
108+
return CACHE.computeIfAbsent(type.getTypeName(), FormattableType::new);
103109
}
104110

105111
/**
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.core;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.modulith.core.FormattableType.NonModuleTypeAbbreviation;
22+
23+
/**
24+
* Unit tests for {@link FormattableType}.
25+
*
26+
* @author Oliver Drotbohm
27+
*/
28+
class FormattableTypeUnitTests {
29+
30+
@Test // GH-1713
31+
void abbreviatesPackagedType() {
32+
33+
assertThat(FormattableType.of(String.class).getAbbreviatedFullName()).isEqualTo("j.l.String");
34+
}
35+
36+
@Test // GH-1713
37+
void handlesPrimitiveTypeWithoutPackage() {
38+
39+
assertThat(FormattableType.of(long.class).getAbbreviatedFullName()).isEqualTo("long");
40+
assertThat(FormattableType.of(long.class).getFullName()).isEqualTo("long");
41+
}
42+
43+
@Test // GH-1713
44+
void handlesVoidTypeWithoutPackage() {
45+
46+
assertThat(FormattableType.of(void.class).getAbbreviatedFullName()).isEqualTo("void");
47+
}
48+
49+
@Test // GH-1713
50+
void abbreviatesPrimitiveTypeForNonModuleType() {
51+
52+
var module = TestUtils.getApplicationModule("example.springbean");
53+
54+
var type = FormattableType.of(long.class);
55+
56+
assertThatNoException()
57+
.isThrownBy(() -> type.getAbbreviatedFullName(module, NonModuleTypeAbbreviation.ABBREVIATED));
58+
59+
assertThat(type.getAbbreviatedFullName(module, NonModuleTypeAbbreviation.ABBREVIATED))
60+
.isEqualTo("long");
61+
62+
assertThat(type.getAbbreviatedFullName(module, NonModuleTypeAbbreviation.FULL_NAME))
63+
.isEqualTo("long");
64+
}
65+
66+
@Test // GH-1713
67+
void handlesPrimitiveArrayType() {
68+
69+
assertThat(FormattableType.of(long[].class).getAbbreviatedFullName()).isEqualTo("long[]");
70+
assertThat(FormattableType.of(long[].class).getFullName()).isEqualTo("long[]");
71+
}
72+
73+
@Test // GH-1713
74+
void handlesReferenceArrayType() {
75+
76+
assertThat(FormattableType.of(String[].class).getAbbreviatedFullName()).isEqualTo("j.l.String[]");
77+
assertThat(FormattableType.of(String[].class).getFullName()).isEqualTo("java.lang.String[]");
78+
}
79+
80+
@Test // GH-1713
81+
void handlesMultiDimensionalArrayType() {
82+
83+
assertThat(FormattableType.of(int[][].class).getAbbreviatedFullName()).isEqualTo("int[][]");
84+
assertThat(FormattableType.of(String[][].class).getAbbreviatedFullName()).isEqualTo("j.l.String[][]");
85+
assertThat(FormattableType.of(String[][].class).getFullName()).isEqualTo("java.lang.String[][]");
86+
}
87+
}

0 commit comments

Comments
 (0)