Skip to content

Commit 0a50b83

Browse files
committed
Add DataSourceBuilderRuntimeHints
Add hints for DataSourceBuilderRuntime. Closes gh-33692
1 parent 69d6aa4 commit 0a50b83

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jdbc;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import javax.sql.DataSource;
24+
25+
import org.springframework.aot.hint.MemberCategory;
26+
import org.springframework.aot.hint.RuntimeHints;
27+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
28+
29+
/**
30+
* {@link RuntimeHintsRegistrar} implementation for {@link DataSource} types supported by
31+
* the {@link DataSourceBuilder}.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
class DataSourceBuilderRuntimeHints implements RuntimeHintsRegistrar {
36+
37+
private static final List<String> TYPE_NAMES;
38+
static {
39+
List<String> typeNames = new ArrayList<>();
40+
typeNames.add("com.mchange.v2.c3p0.ComboPooledDataSource");
41+
typeNames.add("org.h2.jdbcx.JdbcDataSource");
42+
typeNames.add("com.zaxxer.hikari.HikariDataSource");
43+
typeNames.add("org.apache.commons.dbcp2.BasicDataSource");
44+
typeNames.add("oracle.jdbc.datasource.OracleDataSource");
45+
typeNames.add("oracle.ucp.jdbc.PoolDataSource");
46+
typeNames.add("org.postgresql.ds.PGSimpleDataSource");
47+
typeNames.add("org.springframework.jdbc.datasource.SimpleDriverDataSource");
48+
typeNames.add("org.apache.tomcat.jdbc.pool.DataSource");
49+
TYPE_NAMES = Collections.unmodifiableList(typeNames);
50+
}
51+
52+
@Override
53+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
54+
for (String typeName : TYPE_NAMES) {
55+
hints.reflection().registerTypeIfPresent(classLoader, typeName,
56+
(hint) -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
57+
}
58+
}
59+
60+
}

spring-boot-project/spring-boot/src/main/resources/META-INF/spring/aot.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ org.springframework.boot.WebApplicationType.WebApplicationTypeRuntimeHints,\
55
org.springframework.boot.context.config.ConfigDataLocationRuntimeHints,\
66
org.springframework.boot.context.config.ConfigDataPropertiesRuntimeHints,\
77
org.springframework.boot.env.PropertySourceRuntimeHints,\
8+
org.springframework.boot.jdbc.DataSourceBuilderRuntimeHints,\
89
org.springframework.boot.json.JacksonRuntimeHints,\
910
org.springframework.boot.logging.java.JavaLoggingSystemRuntimeHints,\
1011
org.springframework.boot.logging.logback.LogbackRuntimeHints,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jdbc;
18+
19+
import java.util.Set;
20+
import java.util.stream.Stream;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.aot.hint.MemberCategory;
25+
import org.springframework.aot.hint.ReflectionHints;
26+
import org.springframework.aot.hint.RuntimeHints;
27+
import org.springframework.aot.hint.TypeHint;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link DataSourceBuilderRuntimeHints}.
33+
*
34+
* @author Phillip Webb
35+
*/
36+
class DataSourceBuilderRuntimeHintsTests {
37+
38+
@Test
39+
void shouldRegisterDataSourceConstructors() {
40+
ReflectionHints hints = registerHints();
41+
Stream.of(com.mchange.v2.c3p0.ComboPooledDataSource.class, org.h2.jdbcx.JdbcDataSource.class,
42+
com.zaxxer.hikari.HikariDataSource.class, org.apache.commons.dbcp2.BasicDataSource.class,
43+
oracle.jdbc.datasource.OracleDataSource.class, oracle.ucp.jdbc.PoolDataSource.class,
44+
org.postgresql.ds.PGSimpleDataSource.class,
45+
org.springframework.jdbc.datasource.SimpleDriverDataSource.class,
46+
org.apache.tomcat.jdbc.pool.DataSource.class).forEach((dataSourceType) -> {
47+
TypeHint typeHint = hints.getTypeHint(dataSourceType);
48+
assertThat(typeHint).withFailMessage(() -> "No hints found for data source type " + dataSourceType)
49+
.isNotNull();
50+
Set<MemberCategory> memberCategories = typeHint.getMemberCategories();
51+
assertThat(memberCategories).containsExactly(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
52+
});
53+
}
54+
55+
private ReflectionHints registerHints() {
56+
RuntimeHints hints = new RuntimeHints();
57+
new DataSourceBuilderRuntimeHints().registerHints(hints, getClass().getClassLoader());
58+
return hints.reflection();
59+
}
60+
61+
}

0 commit comments

Comments
 (0)