From feb6a5f52d4d4d9ab8651af1efb3b9aa220cb4be Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 16 Oct 2024 11:35:23 +0200 Subject: [PATCH] Polishing --- .../beans/factory/InjectionPoint.java | 10 +++-- .../DefaultBeanRegistrationCodeFragments.java | 4 +- .../factory/config/DependencyDescriptor.java | 4 +- .../support/DefaultListableBeanFactory.java | 4 +- .../aot/generate/AccessControl.java | 14 +++--- .../aot/hint/JavaSerializationHint.java | 31 +++++-------- .../aot/hint/JdkProxyHint.java | 25 ++++------- .../aot/hint/ResourceBundleHint.java | 28 ++++-------- .../aot/hint/ResourcePatternHint.java | 17 ++----- .../springframework/aot/hint/TypeHint.java | 44 +++++++------------ .../DefaultServerHttpRequestBuilder.java | 2 - .../reactive/ServerHttpRequestDecorator.java | 16 +++---- 12 files changed, 75 insertions(+), 124 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/InjectionPoint.java b/spring-beans/src/main/java/org/springframework/beans/factory/InjectionPoint.java index 0a4731f904c5..f83a5da492f7 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/InjectionPoint.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/InjectionPoint.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. @@ -29,9 +29,11 @@ /** * A simple descriptor for an injection point, pointing to a method/constructor - * parameter or a field. Exposed by {@link UnsatisfiedDependencyException}. - * Also available as an argument for factory methods, reacting to the - * requesting injection point for building a customized bean instance. + * parameter or a field. + * + *

Exposed by {@link UnsatisfiedDependencyException}. Also available as an + * argument for factory methods, reacting to the requesting injection point + * for building a customized bean instance. * * @author Juergen Hoeller * @since 4.3 diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java index 57c792af1768..1c115796a8ce 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java @@ -64,8 +64,8 @@ class DefaultBeanRegistrationCodeFragments implements BeanRegistrationCodeFragme private final Supplier instantiationDescriptor; - DefaultBeanRegistrationCodeFragments(BeanRegistrationsCode beanRegistrationsCode, - RegisteredBean registeredBean, + DefaultBeanRegistrationCodeFragments( + BeanRegistrationsCode beanRegistrationsCode, RegisteredBean registeredBean, BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory) { this.beanRegistrationsCode = beanRegistrationsCode; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java index 499628a11386..96d89e3886b9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.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. @@ -148,10 +148,10 @@ public DependencyDescriptor(DependencyDescriptor original) { this.parameterTypes = original.parameterTypes; this.parameterIndex = original.parameterIndex; this.fieldName = original.fieldName; - this.containingClass = original.containingClass; this.required = original.required; this.eager = original.eager; this.nestingLevel = original.nestingLevel; + this.containingClass = original.containingClass; } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index d119caefd3be..17f0fc164dc9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -488,8 +488,8 @@ private T resolveBean(ResolvableType requiredType, @Nullable Object[] args, return namedBean.getBeanInstance(); } BeanFactory parent = getParentBeanFactory(); - if (parent instanceof DefaultListableBeanFactory dlfb) { - return dlfb.resolveBean(requiredType, args, nonUniqueAsNull); + if (parent instanceof DefaultListableBeanFactory dlbf) { + return dlbf.resolveBean(requiredType, args, nonUniqueAsNull); } else if (parent != null) { ObjectProvider parentProvider = parent.getBeanProvider(requiredType); diff --git a/spring-core/src/main/java/org/springframework/aot/generate/AccessControl.java b/spring-core/src/main/java/org/springframework/aot/generate/AccessControl.java index cb9cbd4ec195..72d532513bca 100644 --- a/spring-core/src/main/java/org/springframework/aot/generate/AccessControl.java +++ b/spring-core/src/main/java/org/springframework/aot/generate/AccessControl.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. @@ -45,11 +45,13 @@ public final class AccessControl { private final Visibility visibility; + AccessControl(Class target, Visibility visibility) { this.target = target; this.visibility = visibility; } + /** * Create an {@link AccessControl} for the given member. This considers the * member modifier, parameter types, return types and any enclosing classes. @@ -68,8 +70,7 @@ public static AccessControl forMember(Member member) { * @return the {@link AccessControl} for the type */ public static AccessControl forResolvableType(ResolvableType resolvableType) { - return new AccessControl(resolvableType.toClass(), - Visibility.forResolvableType(resolvableType)); + return new AccessControl(resolvableType.toClass(), Visibility.forResolvableType(resolvableType)); } /** @@ -87,8 +88,8 @@ public static AccessControl forClass(Class type) { * @return the lowest {@link AccessControl} from the candidates */ public static AccessControl lowest(AccessControl... candidates) { - int index = Visibility.lowestIndex(Arrays.stream(candidates) - .map(AccessControl::getVisibility).toArray(Visibility[]::new)); + int index = Visibility.lowestIndex( + Arrays.stream(candidates).map(AccessControl::getVisibility).toArray(Visibility[]::new)); return candidates[index]; } @@ -125,6 +126,7 @@ public boolean isAccessibleFrom(ClassName type) { return this.target.getPackageName().equals(type.packageName()); } + /** * Access visibility types as determined by the modifiers @@ -270,6 +272,6 @@ static int lowestIndex(Visibility... candidates) { } return index; } - } + } diff --git a/spring-core/src/main/java/org/springframework/aot/hint/JavaSerializationHint.java b/spring-core/src/main/java/org/springframework/aot/hint/JavaSerializationHint.java index ef5213895303..8f41d18f51bc 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/JavaSerializationHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/JavaSerializationHint.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. @@ -28,21 +28,22 @@ * @author Brian Clozel * @since 6.0 */ -public class JavaSerializationHint implements ConditionalHint { +public final class JavaSerializationHint implements ConditionalHint { private final TypeReference type; @Nullable private final TypeReference reachableType; + JavaSerializationHint(Builder builder) { this.type = builder.type; this.reachableType = builder.reachableType; } + /** - * Return the {@link TypeReference type} that needs to be serialized using - * Java serialization at runtime. + * Return the {@link TypeReference type} that needs to be serialized using Java serialization at runtime. * @return a {@link Serializable} type */ public TypeReference getType() { @@ -56,16 +57,9 @@ public TypeReference getReachableType() { } @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - JavaSerializationHint that = (JavaSerializationHint) o; - return this.type.equals(that.type) - && Objects.equals(this.reachableType, that.reachableType); + public boolean equals(@Nullable Object other) { + return (this == other || (other instanceof JavaSerializationHint that && + this.type.equals(that.type) && Objects.equals(this.reachableType, that.reachableType))); } @Override @@ -84,16 +78,13 @@ public static class Builder { @Nullable private TypeReference reachableType; - Builder(TypeReference type) { this.type = type; } /** - * Make this hint conditional on the fact that the specified type - * can be resolved. - * @param reachableType the type that should be reachable for this - * hint to apply + * Make this hint conditional on the fact that the specified type can be resolved. + * @param reachableType the type that should be reachable for this hint to apply * @return {@code this}, to facilitate method chaining */ public Builder onReachableType(TypeReference reachableType) { @@ -108,6 +99,6 @@ public Builder onReachableType(TypeReference reachableType) { JavaSerializationHint build() { return new JavaSerializationHint(this); } - } + } diff --git a/spring-core/src/main/java/org/springframework/aot/hint/JdkProxyHint.java b/spring-core/src/main/java/org/springframework/aot/hint/JdkProxyHint.java index dacc51e335da..4310ab5a4f0b 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/JdkProxyHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/JdkProxyHint.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. @@ -62,6 +62,7 @@ public static Builder of(Class... proxiedInterfaces) { return new Builder().proxiedInterfaces(proxiedInterfaces); } + /** * Return the interfaces to be proxied. * @return the interfaces that the proxy should implement @@ -77,16 +78,10 @@ public TypeReference getReachableType() { } @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - JdkProxyHint that = (JdkProxyHint) o; - return this.proxiedInterfaces.equals(that.proxiedInterfaces) - && Objects.equals(this.reachableType, that.reachableType); + public boolean equals(@Nullable Object other) { + return (this == other || (other instanceof JdkProxyHint that && + this.proxiedInterfaces.equals(that.proxiedInterfaces) && + Objects.equals(this.reachableType, that.reachableType))); } @Override @@ -105,7 +100,6 @@ public static class Builder { @Nullable private TypeReference reachableType; - Builder() { this.proxiedInterfaces = new LinkedList<>(); } @@ -131,10 +125,8 @@ public Builder proxiedInterfaces(Class... proxiedInterfaces) { } /** - * Make this hint conditional on the fact that the specified type - * can be resolved. - * @param reachableType the type that should be reachable for this - * hint to apply + * Make this hint conditional on the fact that the specified type can be resolved. + * @param reachableType the type that should be reachable for this hint to apply * @return {@code this}, to facilitate method chaining */ public Builder onReachableType(TypeReference reachableType) { @@ -160,7 +152,6 @@ private static List toTypeReferences(Class... proxiedInterface } return TypeReference.listOf(proxiedInterfaces); } - } } diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ResourceBundleHint.java b/spring-core/src/main/java/org/springframework/aot/hint/ResourceBundleHint.java index d462b1a3b523..c6a652260d21 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ResourceBundleHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ResourceBundleHint.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. @@ -41,9 +41,9 @@ public final class ResourceBundleHint implements ConditionalHint { this.reachableType = builder.reachableType; } + /** * Return the {@code baseName} of the resource bundle. - * @return the base name */ public String getBaseName() { return this.baseName; @@ -56,16 +56,9 @@ public TypeReference getReachableType() { } @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ResourceBundleHint that = (ResourceBundleHint) o; - return this.baseName.equals(that.baseName) - && Objects.equals(this.reachableType, that.reachableType); + public boolean equals(@Nullable Object other) { + return (this == other || (other instanceof ResourceBundleHint that && + this.baseName.equals(that.baseName) && Objects.equals(this.reachableType, that.reachableType))); } @Override @@ -73,6 +66,7 @@ public int hashCode() { return Objects.hash(this.baseName, this.reachableType); } + /** * Builder for {@link ResourceBundleHint}. */ @@ -88,10 +82,8 @@ public static class Builder { } /** - * Make this hint conditional on the fact that the specified type - * can be resolved. - * @param reachableType the type that should be reachable for this - * hint to apply + * Make this hint conditional on the fact that the specified type can be resolved. + * @param reachableType the type that should be reachable for this hint to apply * @return {@code this}, to facilitate method chaining */ public Builder onReachableType(TypeReference reachableType) { @@ -109,14 +101,12 @@ public Builder baseName(String baseName) { } /** - * Creates a {@link ResourceBundleHint} based on the state of this - * builder. + * Create a {@link ResourceBundleHint} based on the state of this builder. * @return a resource bundle hint */ ResourceBundleHint build() { return new ResourceBundleHint(this); } - } } diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java b/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java index 92cc0a690b95..e686a86a3a1b 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.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. @@ -71,7 +71,6 @@ public final class ResourcePatternHint implements ConditionalHint { /** * Return the pattern to use for identifying the resources to match. - * @return the pattern */ public String getPattern() { return this.pattern; @@ -79,7 +78,6 @@ public String getPattern() { /** * Return the regex {@link Pattern} to use for identifying the resources to match. - * @return the regex pattern */ public Pattern toRegex() { String prefix = (this.pattern.startsWith("*") ? ".*" : ""); @@ -98,16 +96,9 @@ public TypeReference getReachableType() { } @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ResourcePatternHint that = (ResourcePatternHint) o; - return this.pattern.equals(that.pattern) - && Objects.equals(this.reachableType, that.reachableType); + public boolean equals(@Nullable Object other) { + return (this == other || (other instanceof ResourcePatternHint that && + this.pattern.equals(that.pattern) && Objects.equals(this.reachableType, that.reachableType))); } @Override diff --git a/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java b/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java index a4d927c73c3e..70b7b7ea0eee 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.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. @@ -23,7 +23,6 @@ import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.StringJoiner; import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -75,6 +74,7 @@ static Builder of(TypeReference type) { return new Builder(type); } + /** * Return the type that this hint handles. * @return the type @@ -123,9 +123,7 @@ public Set getMemberCategories() { @Override public String toString() { - return new StringJoiner(", ", TypeHint.class.getSimpleName() + "[", "]") - .add("type=" + this.type) - .toString(); + return TypeHint.class.getSimpleName() + "[type=" + this.type + "]"; } /** @@ -157,16 +155,14 @@ public static class Builder { private final Set memberCategories = new HashSet<>(); - Builder(TypeReference type) { this.type = type; } /** - * Make this hint conditional on the fact that the specified type - * is in a reachable code path from a static analysis point of view. - * @param reachableType the type that should be reachable for this - * hint to apply + * Make this hint conditional on the fact that the specified type is in a + * reachable code path from a static analysis point of view. + * @param reachableType the type that should be reachable for this hint to apply * @return {@code this}, to facilitate method chaining */ public Builder onReachableType(TypeReference reachableType) { @@ -175,10 +171,9 @@ public Builder onReachableType(TypeReference reachableType) { } /** - * Make this hint conditional on the fact that the specified type - * is in a reachable code path from a static analysis point of view. - * @param reachableType the type that should be reachable for this - * hint to apply + * Make this hint conditional on the fact that the specified type is in a + * reachable code path from a static analysis point of view. + * @param reachableType the type that should be reachable for this hint to apply * @return {@code this}, to facilitate method chaining */ public Builder onReachableType(Class reachableType) { @@ -215,8 +210,9 @@ public Builder withConstructor(List parameterTypes, ExecutableMod * constructor * @return {@code this}, to facilitate method chaining */ - private Builder withConstructor(List parameterTypes, - Consumer constructorHint) { + private Builder withConstructor( + List parameterTypes, Consumer constructorHint) { + ExecutableKey key = new ExecutableKey("", parameterTypes); ExecutableHint.Builder builder = this.constructors.computeIfAbsent(key, k -> ExecutableHint.ofConstructor(parameterTypes)); @@ -271,38 +267,30 @@ public Builder withMembers(MemberCategory... memberCategories) { TypeHint build() { return new TypeHint(this); } - } + private static final class ExecutableKey { private final String name; private final List parameterTypes; - private ExecutableKey(String name, List parameterTypes) { this.name = name; this.parameterTypes = parameterTypes.stream().map(TypeReference::getCanonicalName).toList(); } @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ExecutableKey that = (ExecutableKey) o; - return this.name.equals(that.name) && this.parameterTypes.equals(that.parameterTypes); + public boolean equals(@Nullable Object other) { + return (this == other || (other instanceof ExecutableKey that && + this.name.equals(that.name) && this.parameterTypes.equals(that.parameterTypes))); } @Override public int hashCode() { return Objects.hash(this.name, this.parameterTypes); } - } } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java index 54e2b5892649..2762104246fb 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java @@ -187,7 +187,6 @@ private static class MutatedServerHttpRequest extends AbstractServerHttpRequest private final ServerHttpRequest originalRequest; - public MutatedServerHttpRequest(URI uri, @Nullable String contextPath, HttpMethod method, @Nullable SslInfo sslInfo, @Nullable InetSocketAddress remoteAddress, HttpHeaders headers, Flux body, ServerHttpRequest originalRequest) { @@ -227,7 +226,6 @@ public Flux getBody() { return this.body; } - @SuppressWarnings("unchecked") @Override public T getNativeRequest() { return ServerHttpRequestDecorator.getNativeRequest(this.originalRequest); diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java index fc6143bfdaf0..56592303badb 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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. @@ -113,6 +113,11 @@ public Flux getBody() { return getDelegate().getBody(); } + @Override + public String toString() { + return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]"; + } + /** * Return the native request of the underlying server API, if possible, @@ -130,15 +135,8 @@ else if (request instanceof ServerHttpRequestDecorator serverHttpRequestDecorato return getNativeRequest(serverHttpRequestDecorator.getDelegate()); } else { - throw new IllegalArgumentException( - "Can't find native request in " + request.getClass().getName()); + throw new IllegalArgumentException("Cannot find native request in " + request.getClass().getName()); } } - - @Override - public String toString() { - return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]"; - } - }