From 633fb6e0225c922181b0041df108ec841ec0ae1c Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Mon, 9 May 2022 21:39:05 +0200 Subject: [PATCH 1/2] Make inner classes static when it's possible and makes sense A static inner class does not keep an implicit reference to its enclosing instance. This prevents a common cause of memory leaks and uses less memory per instance of the class. --- .../AutowiredAnnotationBeanPostProcessor.java | 2 +- .../BeanDefinitionPropertyValueCodeGenerator.java | 12 ++++++------ .../generator/InnerComponentConfiguration.java | 4 ++-- .../annotation/ConfigurationClassPostProcessor.java | 2 +- .../server/reactive/UndertowHttpHandlerAdapter.java | 2 +- .../reactive/resource/VersionResourceResolver.java | 2 +- .../upgrade/UndertowRequestUpgradeStrategy.java | 2 +- .../servlet/resource/VersionResourceResolver.java | 2 +- .../handler/XhrStreamingTransportHandler.java | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 770a447aefb8..6407e3bd8d17 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -654,7 +654,7 @@ private Object resolvedCachedArgument(@Nullable String beanName, @Nullable Objec /** * Base class representing injection information. */ - private abstract class AutowiredElement extends InjectionMetadata.InjectedElement { + private abstract static class AutowiredElement extends InjectionMetadata.InjectedElement { protected final boolean required; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java index b8ea68b23c4b..c87bfce918e9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java @@ -119,7 +119,7 @@ private interface Delegate { /** * {@link Delegate} for {@code primitive} types. */ - private class PrimitiveDelegate implements Delegate { + private static class PrimitiveDelegate implements Delegate { private static final Map CHAR_ESCAPES; @@ -177,7 +177,7 @@ private String escape(char ch) { /** * {@link Delegate} for {@link String} types. */ - private class StringDelegate implements Delegate { + private static class StringDelegate implements Delegate { @Override @Nullable @@ -194,7 +194,7 @@ public CodeBlock generateCode(Object value, ResolvableType type) { /** * {@link Delegate} for {@link Enum} types. */ - private class EnumDelegate implements Delegate { + private static class EnumDelegate implements Delegate { @Override @Nullable @@ -212,7 +212,7 @@ public CodeBlock generateCode(Object value, ResolvableType type) { /** * {@link Delegate} for {@link Class} types. */ - private class ClassDelegate implements Delegate { + private static class ClassDelegate implements Delegate { @Override @Nullable @@ -229,7 +229,7 @@ public CodeBlock generateCode(Object value, ResolvableType type) { /** * {@link Delegate} for {@link ResolvableType} types. */ - private class ResolvableTypeDelegate implements Delegate { + private static class ResolvableTypeDelegate implements Delegate { @Override @Nullable @@ -512,7 +512,7 @@ private CodeBlock generateLinkedHashMapCode(Map map, /** * {@link Delegate} for {@link BeanReference} types. */ - private class BeanReferenceDelegate implements Delegate { + private static class BeanReferenceDelegate implements Delegate { @Override @Nullable diff --git a/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java index 28188b2a52a0..0824e010c478 100644 --- a/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java @@ -20,14 +20,14 @@ public class InnerComponentConfiguration { - public class NoDependencyComponent { + public static class NoDependencyComponent { public NoDependencyComponent() { } } - public class EnvironmentAwareComponent { + public static class EnvironmentAwareComponent { public EnvironmentAwareComponent(Environment environment) { diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index e883195a886f..aa8b9e51c809 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -509,7 +509,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) { } - private class AotContribution implements BeanFactoryInitializationAotContribution { + private static class AotContribution implements BeanFactoryInitializationAotContribution { private static final String BEAN_FACTORY_VARIABLE = BeanFactoryInitializationCode.BEAN_FACTORY_VARIABLE; diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowHttpHandlerAdapter.java b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowHttpHandlerAdapter.java index 3364a95493db..09037ecc158c 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowHttpHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowHttpHandlerAdapter.java @@ -88,7 +88,7 @@ public void handleRequest(HttpServerExchange exchange) { } - private class HandlerResultSubscriber implements Subscriber { + private static class HandlerResultSubscriber implements Subscriber { private final HttpServerExchange exchange; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java index d923e96024c8..10ead4bcfbef 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java @@ -236,7 +236,7 @@ protected VersionStrategy getStrategyForPath(String requestPath) { } - private class FileNameVersionedResource extends AbstractResource implements HttpResource { + private static class FileNameVersionedResource extends AbstractResource implements HttpResource { private final Resource original; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/UndertowRequestUpgradeStrategy.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/UndertowRequestUpgradeStrategy.java index 973bbf4fb2e9..4a0e26589fc8 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/UndertowRequestUpgradeStrategy.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/UndertowRequestUpgradeStrategy.java @@ -82,7 +82,7 @@ public Mono upgrade(ServerWebExchange exchange, WebSocketHandler handler, } - private class DefaultCallback implements WebSocketConnectionCallback { + private static class DefaultCallback implements WebSocketConnectionCallback { private final HandshakeInfo handshakeInfo; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java index f2469483abc3..91ca7e77b9ef 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java @@ -232,7 +232,7 @@ protected VersionStrategy getStrategyForPath(String requestPath) { } - private class FileNameVersionedResource extends AbstractResource implements HttpResource { + private static class FileNameVersionedResource extends AbstractResource implements HttpResource { private final Resource original; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java index f947643205ce..08682e3c4871 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java @@ -75,7 +75,7 @@ protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) { } - private class XhrStreamingSockJsSession extends StreamingSockJsSession { + private static class XhrStreamingSockJsSession extends StreamingSockJsSession { public XhrStreamingSockJsSession(String sessionId, SockJsServiceConfig config, WebSocketHandler wsHandler, Map attributes) { From 54a66337de160c4ca0d1278a69f00bf9cbc80aa3 Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Mon, 9 May 2022 21:52:45 +0200 Subject: [PATCH 2/2] Remove static from test fixture --- .../beans/factory/generator/InnerComponentConfiguration.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java index 0824e010c478..28188b2a52a0 100644 --- a/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java @@ -20,14 +20,14 @@ public class InnerComponentConfiguration { - public static class NoDependencyComponent { + public class NoDependencyComponent { public NoDependencyComponent() { } } - public static class EnvironmentAwareComponent { + public class EnvironmentAwareComponent { public EnvironmentAwareComponent(Environment environment) {