Skip to content

Commit 6abcde3

Browse files
committed
Prevent deprecation for DBCP2 username and password properties
Closes gh-40076
1 parent e9e2bc9 commit 6abcde3

File tree

5 files changed

+69
-27
lines changed

5 files changed

+69
-27
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ dependencies {
2727
testImplementation("org.mockito:mockito-core")
2828
testImplementation("org.projectlombok:lombok")
2929
testImplementation("org.springframework:spring-core")
30+
testImplementation("org.apache.commons:commons-dbcp2") {
31+
exclude group: "commons-logging", module: "commons-logging"
32+
}
3033
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import java.io.StringWriter;
2222
import java.time.Duration;
2323
import java.util.ArrayDeque;
24-
import java.util.Arrays;
25-
import java.util.Collections;
2624
import java.util.Deque;
27-
import java.util.HashSet;
2825
import java.util.LinkedHashMap;
2926
import java.util.List;
3027
import java.util.Map;
@@ -105,7 +102,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
105102

106103
static final String AUTO_CONFIGURATION_ANNOTATION = "org.springframework.boot.autoconfigure.AutoConfiguration";
107104

108-
private static final Set<String> SUPPORTED_OPTIONS = Collections.singleton(ADDITIONAL_METADATA_LOCATIONS_OPTION);
105+
private static final Set<String> SUPPORTED_OPTIONS = Set.of(ADDITIONAL_METADATA_LOCATIONS_OPTION);
109106

110107
private MetadataStore metadataStore;
111108

@@ -138,8 +135,8 @@ protected String defaultValueAnnotation() {
138135
}
139136

140137
protected Set<String> endpointAnnotations() {
141-
return new HashSet<>(Arrays.asList(CONTROLLER_ENDPOINT_ANNOTATION, ENDPOINT_ANNOTATION, JMX_ENDPOINT_ANNOTATION,
142-
REST_CONTROLLER_ENDPOINT_ANNOTATION, SERVLET_ENDPOINT_ANNOTATION, WEB_ENDPOINT_ANNOTATION));
138+
return Set.of(CONTROLLER_ENDPOINT_ANNOTATION, ENDPOINT_ANNOTATION, JMX_ENDPOINT_ANNOTATION,
139+
REST_CONTROLLER_ENDPOINT_ANNOTATION, SERVLET_ENDPOINT_ANNOTATION, WEB_ENDPOINT_ANNOTATION);
143140
}
144141

145142
protected String readOperationAnnotation() {

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,30 +50,22 @@
5050
*
5151
* @author Stephane Nicoll
5252
* @author Scott Frederick
53+
* @author Moritz Halbritter
5354
*/
5455
class MetadataGenerationEnvironment {
5556

5657
private static final String NULLABLE_ANNOTATION = "org.springframework.lang.Nullable";
5758

58-
private static final Set<String> TYPE_EXCLUDES;
59-
static {
60-
Set<String> excludes = new HashSet<>();
61-
excludes.add("com.zaxxer.hikari.IConnectionCustomizer");
62-
excludes.add("groovy.lang.MetaClass");
63-
excludes.add("groovy.text.markup.MarkupTemplateEngine");
64-
excludes.add("java.io.Writer");
65-
excludes.add("java.io.PrintWriter");
66-
excludes.add("java.lang.ClassLoader");
67-
excludes.add("java.util.concurrent.ThreadFactory");
68-
excludes.add("jakarta.jms.XAConnectionFactory");
69-
excludes.add("javax.sql.DataSource");
70-
excludes.add("javax.sql.XADataSource");
71-
excludes.add("org.apache.tomcat.jdbc.pool.PoolConfiguration");
72-
excludes.add("org.apache.tomcat.jdbc.pool.Validator");
73-
excludes.add("org.flywaydb.core.api.callback.FlywayCallback");
74-
excludes.add("org.flywaydb.core.api.resolver.MigrationResolver");
75-
TYPE_EXCLUDES = Collections.unmodifiableSet(excludes);
76-
}
59+
private static final Set<String> TYPE_EXCLUDES = Set.of("com.zaxxer.hikari.IConnectionCustomizer",
60+
"groovy.lang.MetaClass", "groovy.text.markup.MarkupTemplateEngine", "java.io.Writer", "java.io.PrintWriter",
61+
"java.lang.ClassLoader", "java.util.concurrent.ThreadFactory", "jakarta.jms.XAConnectionFactory",
62+
"javax.sql.DataSource", "javax.sql.XADataSource", "org.apache.tomcat.jdbc.pool.PoolConfiguration",
63+
"org.apache.tomcat.jdbc.pool.Validator", "org.flywaydb.core.api.callback.FlywayCallback",
64+
"org.flywaydb.core.api.resolver.MigrationResolver");
65+
66+
private static final Set<String> DEPRECATION_EXCLUDES = Set.of(
67+
"org.apache.commons.dbcp2.BasicDataSource#getPassword",
68+
"org.apache.commons.dbcp2.BasicDataSource#getUsername");
7769

7870
private final TypeUtils typeUtils;
7971

@@ -162,6 +154,13 @@ boolean isExcluded(TypeMirror type) {
162154
}
163155

164156
boolean isDeprecated(Element element) {
157+
if (element == null) {
158+
return false;
159+
}
160+
String elementName = element.getEnclosingElement() + "#" + element.getSimpleName();
161+
if (DEPRECATION_EXCLUDES.contains(elementName)) {
162+
return false;
163+
}
165164
if (isElementDeprecated(element)) {
166165
return true;
167166
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
2222
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
2323
import org.springframework.boot.configurationprocessor.metadata.Metadata;
24+
import org.springframework.boot.configurationsample.deprecation.Dbcp2Configuration;
2425
import org.springframework.boot.configurationsample.record.RecordWithGetter;
2526
import org.springframework.boot.configurationsample.recursive.RecursiveProperties;
2627
import org.springframework.boot.configurationsample.simple.ClassWithNestedProperties;
@@ -508,4 +509,11 @@ void recordWithGetter() {
508509
assertThat(metadata).doesNotHave(Metadata.withProperty("record-with-getter.bravo"));
509510
}
510511

512+
@Test
513+
void shouldNotMarkDbcp2UsernameOrPasswordAsDeprecated() {
514+
ConfigurationMetadata metadata = compile(Dbcp2Configuration.class);
515+
assertThat(metadata).has(Metadata.withProperty("spring.datasource.dbcp2.username").withNoDeprecation());
516+
assertThat(metadata).has(Metadata.withProperty("spring.datasource.dbcp2.password").withNoDeprecation());
517+
}
518+
511519
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2024 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.configurationsample.deprecation;
18+
19+
import org.apache.commons.dbcp2.BasicDataSource;
20+
21+
import org.springframework.boot.configurationsample.ConfigurationProperties;
22+
23+
/**
24+
* Test configuration for DBCP2 {@link BasicDataSource}.
25+
*
26+
* @author Moritz Halbritter
27+
*/
28+
public class Dbcp2Configuration {
29+
30+
@ConfigurationProperties(prefix = "spring.datasource.dbcp2")
31+
BasicDataSource basicDataSource() {
32+
return new BasicDataSource();
33+
}
34+
35+
}

0 commit comments

Comments
 (0)