From 8468cce734170f92900e5bf07515e5201690bccc Mon Sep 17 00:00:00 2001 From: arefbehboudi Date: Mon, 9 Sep 2024 16:13:35 +0330 Subject: [PATCH 1/2] Refactor code and update documentation --- .../boot/jdbc/DataSourceBuilder.java | 1 - .../boot/json/JsonValueWriter.java | 4 +- .../logging/log4j2/Log4J2LoggingSystem.java | 4 +- ...PortInfoApplicationContextInitializer.java | 58 +++++++++---------- ...cationContextServerWebExchangeMatcher.java | 2 +- .../ApplicationContextRequestMatcher.java | 2 +- 6 files changed, 31 insertions(+), 40 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java index cea962a7fa74..27526d92d0ad 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java @@ -698,7 +698,6 @@ private void setDriverClass(ComboPooledDataSource dataSource, String driverClass */ private static class SimpleDataSourceProperties extends MappedDataSourceProperties { - @SuppressWarnings("unchecked") SimpleDataSourceProperties() { add(DataSourceProperty.URL, SimpleDriverDataSource::getUrl, SimpleDriverDataSource::setUrl); add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, (dataSource) -> dataSource.getDriver().getClass(), diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java index be0535f7a9a5..861f38a2b31f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java @@ -106,8 +106,8 @@ else if (value instanceof Map map) { else if (value instanceof Number) { append(value.toString()); } - else if (value instanceof Boolean) { - append(Boolean.TRUE.equals(value) ? "true" : "false"); + else if (value instanceof Boolean bool) { + append(bool ? "true" : "false"); } else { writeString(value); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index 21d4d47a3971..694c8f5cba40 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -238,9 +238,7 @@ protected void loadConfiguration(LoggingInitializationContext initializationCont private void load(LoggingInitializationContext initializationContext, String location, LogFile logFile) { List overrides = getOverrides(initializationContext); - if (initializationContext != null) { - applySystemProperties(initializationContext.getEnvironment(), logFile); - } + applySystemProperties(initializationContext.getEnvironment(), logFile); loadConfiguration(location, logFile, overrides); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java index dd94ba58204a..e4b6a9c6c1d4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java @@ -51,49 +51,43 @@ public void initialize(ConfigurableApplicationContext applicationContext) { applicationContext.addApplicationListener(new Listener(applicationContext)); } - private static class Listener implements ApplicationListener { + private record Listener(ConfigurableApplicationContext applicationContext) implements ApplicationListener { - private static final String PROPERTY_NAME = "local.rsocket.server.port"; + private static final String PROPERTY_NAME = "local.rsocket.server.port"; - private static final String PROPERTY_SOURCE_NAME = "server.ports"; - - private final ConfigurableApplicationContext applicationContext; - - Listener(ConfigurableApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } + private static final String PROPERTY_SOURCE_NAME = "server.ports"; @Override - public void onApplicationEvent(RSocketServerInitializedEvent event) { - if (event.getServer().address() != null) { - setPortProperty(this.applicationContext, event.getServer().address().getPort()); + public void onApplicationEvent(RSocketServerInitializedEvent event) { + if (event.getServer().address() != null) { + setPortProperty(this.applicationContext, event.getServer().address().getPort()); + } } - } - private void setPortProperty(ApplicationContext context, int port) { - if (context instanceof ConfigurableApplicationContext configurableContext) { - setPortProperty(configurableContext.getEnvironment(), port); + private void setPortProperty(ApplicationContext context, int port) { + if (context instanceof ConfigurableApplicationContext configurableContext) { + setPortProperty(configurableContext.getEnvironment(), port); + } + if (context.getParent() != null) { + setPortProperty(context.getParent(), port); + } } - if (context.getParent() != null) { - setPortProperty(context.getParent(), port); + + private void setPortProperty(ConfigurableEnvironment environment, int port) { + MutablePropertySources sources = environment.getPropertySources(); + PropertySource source = sources.get(PROPERTY_SOURCE_NAME); + if (source == null) { + source = new MapPropertySource(PROPERTY_SOURCE_NAME, new HashMap<>()); + sources.addFirst(source); + } + setPortProperty(port, source); } - } - private void setPortProperty(ConfigurableEnvironment environment, int port) { - MutablePropertySources sources = environment.getPropertySources(); - PropertySource source = sources.get(PROPERTY_SOURCE_NAME); - if (source == null) { - source = new MapPropertySource(PROPERTY_SOURCE_NAME, new HashMap<>()); - sources.addFirst(source); + @SuppressWarnings("unchecked") + private void setPortProperty(int port, PropertySource source) { + ((Map) source.getSource()).put(PROPERTY_NAME, port); } - setPortProperty(port, source); - } - @SuppressWarnings("unchecked") - private void setPortProperty(int port, PropertySource source) { - ((Map) source.getSource()).put(PROPERTY_NAME, port); } - } - } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java index 88ea38ca5614..11dda128d247 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java @@ -29,7 +29,7 @@ /** * {@link ApplicationContext} backed {@link ServerWebExchangeMatcher}. Can work directly * with the {@link ApplicationContext}, obtain an existing bean or - * {@link AutowireCapableBeanFactory#createBean(Class, int, boolean) create a new bean} + * {@link AutowireCapableBeanFactory#createBean(Class) create a new bean} * that is autowired in the usual way. * * @param the type of the context that the match method actually needs to use. Can be diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/servlet/ApplicationContextRequestMatcher.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/servlet/ApplicationContextRequestMatcher.java index 0f2d9e3858ec..d96253c707d7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/servlet/ApplicationContextRequestMatcher.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/servlet/ApplicationContextRequestMatcher.java @@ -30,7 +30,7 @@ /** * {@link ApplicationContext} backed {@link RequestMatcher}. Can work directly with the * {@link ApplicationContext}, obtain an existing bean or - * {@link AutowireCapableBeanFactory#createBean(Class, int, boolean) create a new bean} + * {@link AutowireCapableBeanFactory#createBean(Class) create a new bean} * that is autowired in the usual way. * * @param the type of the context that the match method actually needs to use. Can be From a437028249e33f3153ae8eb0400a72c7a0fdd811 Mon Sep 17 00:00:00 2001 From: arefbehboudi Date: Mon, 9 Sep 2024 19:35:47 +0330 Subject: [PATCH 2/2] Resolve Reviews --- .../boot/jdbc/DataSourceBuilder.java | 1 + .../boot/json/JsonValueWriter.java | 5 +- ...PortInfoApplicationContextInitializer.java | 58 ++++++++++--------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java index 27526d92d0ad..cea962a7fa74 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java @@ -698,6 +698,7 @@ private void setDriverClass(ComboPooledDataSource dataSource, String driverClass */ private static class SimpleDataSourceProperties extends MappedDataSourceProperties { + @SuppressWarnings("unchecked") SimpleDataSourceProperties() { add(DataSourceProperty.URL, SimpleDriverDataSource::getUrl, SimpleDriverDataSource::setUrl); add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, (dataSource) -> dataSource.getDriver().getClass(), diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java index 861f38a2b31f..00194b550431 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java @@ -103,12 +103,9 @@ else if (ObjectUtils.isArray(value)) { else if (value instanceof Map map) { writeObject(map::forEach); } - else if (value instanceof Number) { + else if (value instanceof Number || value instanceof Boolean) { append(value.toString()); } - else if (value instanceof Boolean bool) { - append(bool ? "true" : "false"); - } else { writeString(value); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java index e4b6a9c6c1d4..dd94ba58204a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java @@ -51,43 +51,49 @@ public void initialize(ConfigurableApplicationContext applicationContext) { applicationContext.addApplicationListener(new Listener(applicationContext)); } - private record Listener(ConfigurableApplicationContext applicationContext) implements ApplicationListener { + private static class Listener implements ApplicationListener { - private static final String PROPERTY_NAME = "local.rsocket.server.port"; + private static final String PROPERTY_NAME = "local.rsocket.server.port"; - private static final String PROPERTY_SOURCE_NAME = "server.ports"; + private static final String PROPERTY_SOURCE_NAME = "server.ports"; + + private final ConfigurableApplicationContext applicationContext; + + Listener(ConfigurableApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } @Override - public void onApplicationEvent(RSocketServerInitializedEvent event) { - if (event.getServer().address() != null) { - setPortProperty(this.applicationContext, event.getServer().address().getPort()); - } + public void onApplicationEvent(RSocketServerInitializedEvent event) { + if (event.getServer().address() != null) { + setPortProperty(this.applicationContext, event.getServer().address().getPort()); } + } - private void setPortProperty(ApplicationContext context, int port) { - if (context instanceof ConfigurableApplicationContext configurableContext) { - setPortProperty(configurableContext.getEnvironment(), port); - } - if (context.getParent() != null) { - setPortProperty(context.getParent(), port); - } + private void setPortProperty(ApplicationContext context, int port) { + if (context instanceof ConfigurableApplicationContext configurableContext) { + setPortProperty(configurableContext.getEnvironment(), port); } - - private void setPortProperty(ConfigurableEnvironment environment, int port) { - MutablePropertySources sources = environment.getPropertySources(); - PropertySource source = sources.get(PROPERTY_SOURCE_NAME); - if (source == null) { - source = new MapPropertySource(PROPERTY_SOURCE_NAME, new HashMap<>()); - sources.addFirst(source); - } - setPortProperty(port, source); + if (context.getParent() != null) { + setPortProperty(context.getParent(), port); } + } - @SuppressWarnings("unchecked") - private void setPortProperty(int port, PropertySource source) { - ((Map) source.getSource()).put(PROPERTY_NAME, port); + private void setPortProperty(ConfigurableEnvironment environment, int port) { + MutablePropertySources sources = environment.getPropertySources(); + PropertySource source = sources.get(PROPERTY_SOURCE_NAME); + if (source == null) { + source = new MapPropertySource(PROPERTY_SOURCE_NAME, new HashMap<>()); + sources.addFirst(source); } + setPortProperty(port, source); + } + @SuppressWarnings("unchecked") + private void setPortProperty(int port, PropertySource source) { + ((Map) source.getSource()).put(PROPERTY_NAME, port); } + } + }