Skip to content

Commit

Permalink
Merge pull request #43917 from quaff
Browse files Browse the repository at this point in the history
* pr/43917:
  Polish 'Refactor `@ConfigurationProperties` that only use `prefix`'
  Refactor `@ConfigurationProperties` that only use `prefix`
  Enforce `@ConfigurationProperties` don't use only `prefix`

Closes gh-43917
  • Loading branch information
philwebb committed Jan 24, 2025
2 parents f8ae1ff + 8ec61b9 commit 62a0e54
Show file tree
Hide file tree
Showing 174 changed files with 419 additions and 383 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.tngtech.archunit.core.domain.JavaParameter;
import com.tngtech.archunit.core.domain.JavaType;
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
import com.tngtech.archunit.core.domain.properties.HasAnnotations;
import com.tngtech.archunit.core.domain.properties.HasName;
import com.tngtech.archunit.core.domain.properties.HasOwner.Predicates.With;
import com.tngtech.archunit.core.domain.properties.HasParameterTypes;
Expand Down Expand Up @@ -97,7 +98,9 @@ public ArchitectureCheck() {
noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallStringToUpperCaseWithoutLocale(),
noClassesShouldCallStringToLowerCaseWithoutLocale(),
conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType(),
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType());
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType(),
classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute(),
methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
getRules().addAll(getProhibitObjectsRequireNonNull()
.map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
Expand Down Expand Up @@ -344,6 +347,39 @@ public void check(JavaMethod item, ConditionEvents events) {
};
}

private ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() {
return ArchRuleDefinition.classes()
.that()
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties")
.should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties())
.allowEmptyShould(true);
}

private ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() {
return ArchRuleDefinition.methods()
.that()
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties")
.should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties())
.allowEmptyShould(true);
}

private ArchCondition<? super HasAnnotations<?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties() {
return new ArchCondition<>("not specify only prefix attribute of @ConfigurationProperties") {

@Override
public void check(HasAnnotations<?> item, ConditionEvents events) {
JavaAnnotation<?> configurationProperties = item
.getAnnotationOfType("org.springframework.boot.context.properties.ConfigurationProperties");
Map<String, Object> properties = configurationProperties.getProperties();
if (properties.size() == 1 && properties.containsKey("prefix")) {
events.add(SimpleConditionEvent.violated(item, configurationProperties.getDescription()
+ " should specify implicit 'value' attribute other than explicit 'prefix' attribute"));
}
}

};
}

public void setClasses(FileCollection classes) {
this.classes = classes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@
* @author Andy Wilkinson
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.endpoints.web.cors")
@ConfigurationProperties("management.endpoints.web.cors")
public class CorsEndpointProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @author Phillip Webb
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.endpoints.web")
@ConfigurationProperties("management.endpoints.web")
public class WebEndpointProperties {

private final Exposure exposure = new Exposure();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,7 @@
* @author Julio Gomez
* @since 2.4.0
*/
@ConfigurationProperties(prefix = "management.health.db")
@ConfigurationProperties("management.health.db")
public class DataSourceHealthIndicatorProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.endpoint.logfile")
@ConfigurationProperties("management.endpoint.logfile")
public class LogFileWebEndpointProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,7 @@
* @author Stephane Nicoll
* @since 2.1.0
*/
@ConfigurationProperties(prefix = "management.appoptics.metrics.export")
@ConfigurationProperties("management.appoptics.metrics.export")
public class AppOpticsProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.atlas.metrics.export")
@ConfigurationProperties("management.atlas.metrics.export")
public class AtlasProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.datadog.metrics.export")
@ConfigurationProperties("management.datadog.metrics.export")
public class DatadogProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@
* @author Georg Pirklbauer
* @since 2.1.0
*/
@ConfigurationProperties(prefix = "management.dynatrace.metrics.export")
@ConfigurationProperties("management.dynatrace.metrics.export")
public class DynatraceProperties extends StepRegistryProperties {

private final V1 v1 = new V1();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,7 @@
* @author Andy Wilkinson
* @since 2.1.0
*/
@ConfigurationProperties(prefix = "management.elastic.metrics.export")
@ConfigurationProperties("management.elastic.metrics.export")
public class ElasticProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.ganglia.metrics.export")
@ConfigurationProperties("management.ganglia.metrics.export")
public class GangliaProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,7 +32,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.graphite.metrics.export")
@ConfigurationProperties("management.graphite.metrics.export")
public class GraphiteProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
* @author Andy Wilkinson
* @since 2.1.0
*/
@ConfigurationProperties(prefix = "management.humio.metrics.export")
@ConfigurationProperties("management.humio.metrics.export")
public class HumioProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.influx.metrics.export")
@ConfigurationProperties("management.influx.metrics.export")
public class InfluxProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.jmx.metrics.export")
@ConfigurationProperties("management.jmx.metrics.export")
public class JmxProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,7 @@
* @author Stephane Nicoll
* @since 2.1.0
*/
@ConfigurationProperties(prefix = "management.kairos.metrics.export")
@ConfigurationProperties("management.kairos.metrics.export")
public class KairosProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,7 @@
* @author Neil Powell
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.newrelic.metrics.export")
@ConfigurationProperties("management.newrelic.metrics.export")
public class NewRelicProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,7 @@
* @author Jonatan Ivanov
* @since 3.4.0
*/
@ConfigurationProperties(prefix = "management.otlp.metrics.export")
@ConfigurationProperties("management.otlp.metrics.export")
public class OtlpMetricsProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.prometheus.metrics.export")
@ConfigurationProperties("management.prometheus.metrics.export")
public class PrometheusProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.signalfx.metrics.export")
@ConfigurationProperties("management.signalfx.metrics.export")
public class SignalFxProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.simple.metrics.export")
@ConfigurationProperties("management.simple.metrics.export")
public class SimpleProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@
* @author Stephane Nicoll
* @since 2.3.0
*/
@ConfigurationProperties(prefix = "management.stackdriver.metrics.export")
@ConfigurationProperties("management.stackdriver.metrics.export")
public class StackdriverProperties extends StepRegistryProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,7 @@
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.statsd.metrics.export")
@ConfigurationProperties("management.statsd.metrics.export")
public class StatsdProperties {

/**
Expand Down
Loading

0 comments on commit 62a0e54

Please sign in to comment.