From d2246162c3a1b4f60bbec24395b83a067a20efc7 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 29 Apr 2025 19:52:06 -0700 Subject: [PATCH] Polish `GroupsMetadata.Registration` Unify `Registration` and `DefualtRegistration` into a single class since they are both package-private and the interface isn't really needed. --- .../web/service/registry/GroupsMetadata.java | 59 +++++++------------ .../registry/GroupsMetadataValueDelegate.java | 14 ++--- .../GroupsMetadataValueDelegateTests.java | 9 ++- 3 files changed, 31 insertions(+), 51 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadata.java b/spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadata.java index 2822d048dc..7df6a83568 100644 --- a/spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadata.java +++ b/spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadata.java @@ -41,13 +41,13 @@ import org.springframework.util.ClassUtils; */ final class GroupsMetadata { - private final Map groupMap; + private final Map groupMap; public GroupsMetadata() { this(Collections.emptyList()); } - GroupsMetadata(Iterable registrations) { + GroupsMetadata(Iterable registrations) { this.groupMap = new LinkedHashMap<>(); registrations.forEach(registration -> this.groupMap.put(registration.name(), registration)); } @@ -58,7 +58,7 @@ final class GroupsMetadata { * types after checking they don't conflict. */ public Registration getOrCreateGroup(String groupName, HttpServiceGroup.ClientType clientType) { - return this.groupMap.computeIfAbsent(groupName, name -> new DefaultRegistration(name, clientType)) + return this.groupMap.computeIfAbsent(groupName, name -> new Registration(name, clientType)) .clientType(clientType); } @@ -94,7 +94,7 @@ final class GroupsMetadata { /** * Return the raw {@link DefaultRegistration registrations}. */ - Stream registrations() { + Stream registrations() { return this.groupMap.values().stream(); } @@ -102,63 +102,43 @@ final class GroupsMetadata { /** * Registration metadata for an {@link HttpServiceGroup}. */ - interface Registration { - - String name(); - - HttpServiceGroup.ClientType clientType(); - - Set httpServiceTypeNames(); - } - - - /** - * Default implementation of {@link Registration}. - */ - static class DefaultRegistration implements Registration { + static class Registration { private final String name; private HttpServiceGroup.ClientType clientType; - private final Set typeNames; + private final Set httpServiceTypeNames; - DefaultRegistration(String name, HttpServiceGroup.ClientType clientType) { + Registration(String name, HttpServiceGroup.ClientType clientType) { this(name, clientType, new LinkedHashSet<>()); } - DefaultRegistration(String name, HttpServiceGroup.ClientType clientType, Set typeNames) { + Registration(String name, HttpServiceGroup.ClientType clientType, Set httpServiceTypeNames) { this.name = name; this.clientType = clientType; - this.typeNames = typeNames; + this.httpServiceTypeNames = httpServiceTypeNames; } - @Override - public String name() { + String name() { return this.name; } - @Override - public HttpServiceGroup.ClientType clientType() { + HttpServiceGroup.ClientType clientType() { return this.clientType; } - @Override - public Set httpServiceTypeNames() { - return this.typeNames; + Set httpServiceTypeNames() { + return this.httpServiceTypeNames; } /** * Update the client type if it does not conflict with the existing value. */ - public DefaultRegistration clientType(HttpServiceGroup.ClientType other) { - if (this.clientType.isUnspecified()) { - this.clientType = other; - } - else { - Assert.isTrue(this.clientType == other || other.isUnspecified(), - "ClientType conflict for HttpServiceGroup '" + this.name + "'"); - } + public Registration clientType(HttpServiceGroup.ClientType other) { + this.clientType = (this.clientType.isUnspecified() ? other : this.clientType); + Assert.isTrue(this.clientType == other || other.isUnspecified(), + "ClientType conflict for HttpServiceGroup '" + this.name + "'"); return this; } @@ -168,15 +148,16 @@ final class GroupsMetadata { public HttpServiceGroup toHttpServiceGroup(@Nullable ClassLoader classLoader) { return new RegisteredGroup(this.name, (this.clientType.isUnspecified() ? HttpServiceGroup.ClientType.REST_CLIENT : this.clientType), - this.typeNames.stream() + this.httpServiceTypeNames.stream() .map(typeName -> ClassUtils.resolveClassName(typeName, classLoader)) .collect(Collectors.toSet())); } @Override public String toString() { - return "Group '" + this.name + "', ClientType." + this.clientType + ", " + this.typeNames; + return "Group '" + this.name + "', ClientType." + this.clientType + ", " + this.httpServiceTypeNames; } + } diff --git a/spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadataValueDelegate.java b/spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadataValueDelegate.java index 12c5546da4..cdb8447313 100644 --- a/spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadataValueDelegate.java +++ b/spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadataValueDelegate.java @@ -29,7 +29,7 @@ import org.jspecify.annotations.Nullable; import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator; import org.springframework.aot.generate.ValueCodeGenerator; import org.springframework.javapoet.CodeBlock; -import org.springframework.web.service.registry.GroupsMetadata.DefaultRegistration; +import org.springframework.web.service.registry.GroupsMetadata.Registration; /** * {@link ValueCodeGenerator.Delegate} for {@link GroupsMetadata}. @@ -41,7 +41,7 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate { @Override public @Nullable CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) { - if (value instanceof DefaultRegistration registration) { + if (value instanceof Registration registration) { return generateRegistrationCode(valueCodeGenerator, registration); } if (value instanceof GroupsMetadata groupsMetadata) { @@ -51,9 +51,9 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate { } public CodeBlock generateRegistrationCode(ValueCodeGenerator - valueCodeGenerator, DefaultRegistration value) { + valueCodeGenerator, Registration value) { CodeBlock.Builder code = CodeBlock.builder(); - code.add("new $T($S, $L, $L)", DefaultRegistration.class, value.name(), + code.add("new $T($S, $L, $L)", Registration.class, value.name(), valueCodeGenerator.generateCode(value.clientType()), !value.httpServiceTypeNames().isEmpty() ? valueCodeGenerator.generateCode(value.httpServiceTypeNames()) : @@ -62,7 +62,7 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate { } private CodeBlock generateGroupsMetadataCode(ValueCodeGenerator valueCodeGenerator, GroupsMetadata groupsMetadata) { - Collection registrations = groupsMetadata.registrations() + Collection registrations = groupsMetadata.registrations() .collect(Collectors.toCollection(ArrayList::new)); if (valueCodeGenerator.getGeneratedMethods() != null) { return valueCodeGenerator.getGeneratedMethods().add("getGroupsMetadata", method -> method @@ -77,11 +77,11 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate { } private CodeBlock generateGroupsMetadataMethod( - ValueCodeGenerator valueCodeGenerator, Collection registrations) { + ValueCodeGenerator valueCodeGenerator, Collection registrations) { CodeBlock.Builder code = CodeBlock.builder(); String registrationsVariable = "registrations"; - code.addStatement("$T<$T> $L = new $T<>()", List.class, DefaultRegistration.class, + code.addStatement("$T<$T> $L = new $T<>()", List.class, Registration.class, registrationsVariable, ArrayList.class); registrations.forEach(registration -> code.addStatement("$L.add($L)", registrationsVariable, diff --git a/spring-web/src/test/java/org/springframework/web/service/registry/GroupsMetadataValueDelegateTests.java b/spring-web/src/test/java/org/springframework/web/service/registry/GroupsMetadataValueDelegateTests.java index cca4c5bee9..0bc368a735 100644 --- a/spring-web/src/test/java/org/springframework/web/service/registry/GroupsMetadataValueDelegateTests.java +++ b/spring-web/src/test/java/org/springframework/web/service/registry/GroupsMetadataValueDelegateTests.java @@ -39,7 +39,6 @@ import org.springframework.core.test.tools.TestCompiler; import org.springframework.javapoet.CodeBlock; import org.springframework.javapoet.MethodSpec; import org.springframework.util.ReflectionUtils; -import org.springframework.web.service.registry.GroupsMetadata.DefaultRegistration; import org.springframework.web.service.registry.GroupsMetadata.Registration; import org.springframework.web.service.registry.HttpServiceGroup.ClientType; import org.springframework.web.service.registry.echo.EchoA; @@ -59,21 +58,21 @@ class GroupsMetadataValueDelegateTests { @Test void generateRegistrationWithOnlyName() { - DefaultRegistration registration = new DefaultRegistration("test", ClientType.UNSPECIFIED); + Registration registration = new Registration("test", ClientType.UNSPECIFIED); compile(registration, (instance, compiled) -> assertThat(instance) .isInstanceOfSatisfying(Registration.class, hasRegistration("test", ClientType.UNSPECIFIED))); } @Test void generateRegistrationWitNoHttpServiceTypeName() { - DefaultRegistration registration = new DefaultRegistration("test", ClientType.REST_CLIENT); + Registration registration = new Registration("test", ClientType.REST_CLIENT); compile(registration, (instance, compiled) -> assertThat(instance) .isInstanceOfSatisfying(Registration.class, hasRegistration("test", ClientType.REST_CLIENT))); } @Test void generateRegistrationWitOneHttpServiceTypeName() { - DefaultRegistration registration = new DefaultRegistration("test", ClientType.WEB_CLIENT, + Registration registration = new Registration("test", ClientType.WEB_CLIENT, httpServiceTypeNames("com.example.MyClient")); compile(registration, (instance, compiled) -> assertThat(instance) .isInstanceOfSatisfying(Registration.class, hasRegistration( @@ -82,7 +81,7 @@ class GroupsMetadataValueDelegateTests { @Test void generateRegistrationWitHttpServiceTypeNames() { - DefaultRegistration registration = new DefaultRegistration("test", ClientType.WEB_CLIENT, + Registration registration = new Registration("test", ClientType.WEB_CLIENT, httpServiceTypeNames("com.example.MyClient", "com.example.another.TestClient")); compile(registration, (instance, compiled) -> assertThat(instance) .isInstanceOfSatisfying(Registration.class, hasRegistration(