Add @DeprecatedConfigurationProperties annotation

Add a new @DeprecatedConfigurationProperties annotation which can be
used by the `ConfigurationMetadataAnnotationProcessor` to generating
meta-data deprecated blocks.

Fixes gh-3543
This commit is contained in:
Phillip Webb
2015-07-17 14:13:11 -07:00
parent db41e0d7d5
commit e9d252e05c
7 changed files with 205 additions and 11 deletions

View File

@@ -43,6 +43,7 @@ import org.springframework.boot.configurationsample.method.EmptyTypeMethodConfig
import org.springframework.boot.configurationsample.method.InvalidMethodConfig;
import org.springframework.boot.configurationsample.method.MethodAndClassConfig;
import org.springframework.boot.configurationsample.method.SimpleMethodConfig;
import org.springframework.boot.configurationsample.simple.DeprecatedSingleProperty;
import org.springframework.boot.configurationsample.simple.HierarchicalProperties;
import org.springframework.boot.configurationsample.simple.NotAnnotated;
import org.springframework.boot.configurationsample.simple.SimpleCollectionProperties;
@@ -188,6 +189,17 @@ public class ConfigurationMetadataAnnotationProcessorTests {
.fromSource(type).withDeprecation(null, null));
}
@Test
public void singleDeprecatedProprety() throws Exception {
Class<?> type = DeprecatedSingleProperty.class;
ConfigurationMetadata metadata = compile(type);
assertThat(metadata, containsGroup("singledeprecated").fromSource(type));
assertThat(metadata, containsProperty("singledeprecated.new-name", String.class)
.fromSource(type));
assertThat(metadata, containsProperty("singledeprecated.name", String.class)
.fromSource(type).withDeprecation("renamed", "singledeprecated.new-name"));
}
@Test
public void parseCollectionConfig() throws Exception {
ConfigurationMetadata metadata = compile(SimpleCollectionProperties.class);

View File

@@ -41,6 +41,8 @@ public class TestConfigurationMetadataAnnotationProcessor extends
static final String NESTED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.NestedConfigurationProperty";
static final String DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.DeprecatedConfigurationProperty";
private ConfigurationMetadata metadata;
private final File outputLocation;
@@ -59,6 +61,11 @@ public class TestConfigurationMetadataAnnotationProcessor extends
return NESTED_CONFIGURATION_PROPERTY_ANNOTATION;
}
@Override
protected String deprecatedConfigurationPropertyAnnotation() {
return DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION;
}
@Override
protected ConfigurationMetadata writeMetaData() {
super.writeMetaData();
@@ -83,4 +90,4 @@ public class TestConfigurationMetadataAnnotationProcessor extends
return this.metadata;
}
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a getter in a {@link ConfigurationProperties} object is deprecated. This
* annotation has no bearing on the actual binding processes, but it is used by the
* {@code spring-boot-configuration-processor} to add deprecation meta-data.
* <p>
* This annotation <strong>must</strong> be used on the getter of the deprecated element.
*
* @author Phillip Webb
* @since 1.3.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DeprecatedConfigurationProperty {
/**
* The reason for the deprecation.
* @return the deprecation reason
*/
String reason() default "";
/**
* The field that should be used instead (if any).
* @return the replacement field
*/
String replacement() default "";
}

View File

@@ -32,7 +32,7 @@ public class DeprecatedProperties {
private String description;
public String getName() {
return name;
return this.name;
}
public void setName(String name) {
@@ -40,7 +40,7 @@ public class DeprecatedProperties {
}
public String getDescription() {
return description;
return this.description;
}
public void setDescription(String description) {

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.DeprecatedConfigurationProperty;
/**
* Configuration properties with a single deprecated element.
*
* @author Phillip Webb
*/
@ConfigurationProperties("singledeprecated")
public class DeprecatedSingleProperty {
private String newName;
@Deprecated
@DeprecatedConfigurationProperty(reason = "renamed", replacement = "singledeprecated.new-name")
public String getName() {
return getNewName();
}
@Deprecated
public void setName(String name) {
setNewName(name);
}
public String getNewName() {
return this.newName;
}
public void setNewName(String newName) {
this.newName = newName;
}
}