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 c0cc317f77e5..893f789dc756 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 @@ -281,22 +281,17 @@ public String toString() { } Method findSetter(Class type) { - return extracted("set", type, true); + return extracted("set", type, String.class); } Method findGetter(Class type) { - return extracted("get", type, false); + return extracted("get", type); } - private Method extracted(String prefix, Class type, boolean hasParameter) { + private Method extracted(String prefix, Class type, Class... paramTypes) { for (String candidate : this.names) { - Method method; - if (hasParameter) { - method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate), String.class); - } - else { - method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate)); - } + Method method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate), + paramTypes); if (method != null) { return method; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java index d807b93b50e9..d8912457910e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java @@ -331,8 +331,23 @@ void buildWhenDerivedFromExistingDatabaseWithTypeChange() { assertThat(built.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres"); } - @Test // gh -27295 - void buildWhenDerivedFromCustomTypeSpecifiedReturnsDataSource() { + @Test // gh-27295 + void buildWhenDerivedFromCustomType() { + CustomDataSource dataSource = new CustomDataSource(); + dataSource.setUsername("test"); + dataSource.setPassword("secret"); + dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres"); + DataSourceBuilder builder = DataSourceBuilder.derivedFrom(dataSource).username("alice") + .password("confidential"); + CustomDataSource testSource = (CustomDataSource) builder.build(); + assertThat(testSource).isNotSameAs(dataSource); + assertThat(testSource.getUsername()).isEqualTo("alice"); + assertThat(testSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres"); + assertThat(testSource.getPassword()).isEqualTo("confidential"); + } + + @Test // gh-27295 + void buildWhenDerivedFromCustomTypeWithTypeChange() { CustomDataSource dataSource = new CustomDataSource(); dataSource.setUsername("test"); dataSource.setPassword("secret");