Commit f75b266d authored by Phillip Webb's avatar Phillip Webb

Add 'deprecated' field to configuration meta-data

Update the ConfigurationMetadataAnnotationProcessor to detect the
@Deprecated annotation on getters or setters.

Fixes gh-1846
parent 31253d0d
......@@ -183,8 +183,10 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
String sourceType = this.typeUtils.getType(element);
String description = this.typeUtils.getJavaDoc(field);
Object defaultValue = fieldValues.get(name);
boolean deprecated = hasDeprecateAnnotation(getter)
|| hasDeprecateAnnotation(setter);
this.metadata.add(ItemMetadata.newProperty(prefix, name, dataType,
sourceType, null, description, defaultValue));
sourceType, null, description, defaultValue, deprecated));
}
}
}
......@@ -217,6 +219,10 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
}
}
private boolean hasDeprecateAnnotation(Element element) {
return getAnnotation(element, "java.lang.Deprecated") != null;
}
private AnnotationMirror getAnnotation(Element element, String type) {
if (element != null) {
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
......
......@@ -36,13 +36,15 @@ public class ItemMetadata implements Comparable<ItemMetadata> {
private final String sourceType;
private String sourceMethod;
private final String sourceMethod;
private Object defaultValue;
private final Object defaultValue;
private final boolean deprecated;
ItemMetadata(ItemType itemType, String prefix, String name, String type,
String sourceType, String sourceMethod, String description,
Object defaultValue) {
Object defaultValue, boolean deprecated) {
super();
this.itemType = itemType;
this.name = buildName(prefix, name);
......@@ -51,6 +53,7 @@ public class ItemMetadata implements Comparable<ItemMetadata> {
this.sourceMethod = sourceMethod;
this.description = description;
this.defaultValue = defaultValue;
this.deprecated = deprecated;
}
private String buildName(String prefix, String name) {
......@@ -93,6 +96,10 @@ public class ItemMetadata implements Comparable<ItemMetadata> {
return this.defaultValue;
}
public boolean isDeprecated() {
return this.deprecated;
}
@Override
public String toString() {
StringBuilder string = new StringBuilder(this.name);
......@@ -100,6 +107,7 @@ public class ItemMetadata implements Comparable<ItemMetadata> {
buildToStringProperty(string, "sourceType", this.sourceType);
buildToStringProperty(string, "description", this.description);
buildToStringProperty(string, "defaultValue", this.defaultValue);
buildToStringProperty(string, "deprecated", this.deprecated);
return string.toString();
}
......@@ -118,14 +126,14 @@ public class ItemMetadata implements Comparable<ItemMetadata> {
public static ItemMetadata newGroup(String name, String type, String sourceType,
String sourceMethod) {
return new ItemMetadata(ItemType.GROUP, name, null, type, sourceType,
sourceMethod, null, null);
sourceMethod, null, null, false);
}
public static ItemMetadata newProperty(String prefix, String name, String type,
String sourceType, String sourceMethod, String description,
Object defaultValue) {
Object defaultValue, boolean deprecated) {
return new ItemMetadata(ItemType.PROPERTY, prefix, name, type, sourceType,
sourceMethod, description, defaultValue);
sourceMethod, description, defaultValue, deprecated);
}
/**
......
......@@ -68,6 +68,9 @@ public class JsonMarshaller {
putIfPresent(jsonObject, "sourceType", item.getSourceType());
putIfPresent(jsonObject, "sourceMethod", item.getSourceMethod());
putIfPresent(jsonObject, "defaultValue", item.getDefaultValue());
if (item.isDeprecated()) {
jsonObject.put("deprecated", true);
}
return jsonObject;
}
......@@ -103,8 +106,9 @@ public class JsonMarshaller {
String sourceType = object.optString("sourceType", null);
String sourceMethod = object.optString("sourceMethod", null);
Object defaultValue = object.opt("defaultValue");
boolean deprecated = object.optBoolean("deprecated");
return new ItemMetadata(itemType, name, null, type, sourceType, sourceMethod,
description, defaultValue);
description, defaultValue, deprecated);
}
private String toString(InputStream inputStream) throws IOException {
......
......@@ -76,11 +76,12 @@ public class ConfigurationMetadataAnnotationProcessorTests {
containsProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot"));
.withDefaultValue("boot").withDeprecated());
assertThat(
metadata,
containsProperty("simple.flag", Boolean.class).fromSource(
SimpleProperties.class).withDescription("A simple flag."));
containsProperty("simple.flag", Boolean.class)
.fromSource(SimpleProperties.class)
.withDescription("A simple flag.").withDeprecated());
assertThat(metadata, containsProperty("simple.comparator"));
assertThat(metadata, not(containsProperty("simple.counter")));
assertThat(metadata, not(containsProperty("simple.size")));
......
......@@ -68,18 +68,22 @@ public class ConfigurationMetadataMatchers {
private final Object defaultValue;
private final boolean deprecated;
public ContainsItemMatcher(ItemType itemType, String name) {
this(itemType, name, null, null, null, null);
this(itemType, name, null, null, null, null, false);
}
public ContainsItemMatcher(ItemType itemType, String name, String type,
Class<?> sourceType, String description, Object defaultValue) {
Class<?> sourceType, String description, Object defaultValue,
boolean deprecated) {
this.itemType = itemType;
this.name = name;
this.type = type;
this.sourceType = sourceType;
this.description = description;
this.defaultValue = defaultValue;
this.deprecated = deprecated;
}
@Override
......@@ -104,6 +108,9 @@ public class ConfigurationMetadataMatchers {
&& !this.description.equals(itemMetadata.getDescription())) {
return false;
}
if (this.deprecated != itemMetadata.isDeprecated()) {
return false;
}
return true;
}
......@@ -134,31 +141,39 @@ public class ConfigurationMetadataMatchers {
if (this.description != null) {
description.appendText(" description ").appendValue(this.description);
}
if (this.deprecated) {
description.appendText(" deprecated ").appendValue(true);
}
}
public ContainsItemMatcher ofType(Class<?> dataType) {
return new ContainsItemMatcher(this.itemType, this.name, dataType.getName(),
this.sourceType, this.description, this.defaultValue);
this.sourceType, this.description, this.defaultValue, this.deprecated);
}
public ContainsItemMatcher ofDataType(String dataType) {
return new ContainsItemMatcher(this.itemType, this.name, dataType,
this.sourceType, this.description, this.defaultValue);
this.sourceType, this.description, this.defaultValue, this.deprecated);
}
public ContainsItemMatcher fromSource(Class<?> sourceType) {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
sourceType, this.description, this.defaultValue);
sourceType, this.description, this.defaultValue, this.deprecated);
}
public ContainsItemMatcher withDescription(String description) {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
this.sourceType, description, this.defaultValue);
this.sourceType, description, this.defaultValue, this.deprecated);
}
public ContainsItemMatcher withDefaultValue(Object defaultValue) {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
this.sourceType, this.description, defaultValue, this.deprecated);
}
public Matcher<? super ConfigurationMetadata> withDefaultValue(Object defaultValue) {
public ContainsItemMatcher withDeprecated() {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
this.sourceType, this.description, defaultValue);
this.sourceType, this.description, this.defaultValue, true);
}
private ItemMetadata getFirstPropertyWithName(ConfigurationMetadata metadata,
......
......@@ -38,11 +38,13 @@ public class JsonMarshallerTests {
public void marshallAndUnmarshal() throws IOException {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(),
InputStream.class.getName(), "sourceMethod", "desc", "x"));
metadata.add(ItemMetadata
.newProperty("b.c.d", null, null, null, null, null, null));
metadata.add(ItemMetadata.newProperty("c", null, null, null, null, null, 123));
metadata.add(ItemMetadata.newProperty("d", null, null, null, null, null, true));
InputStream.class.getName(), "sourceMethod", "desc", "x", true));
metadata.add(ItemMetadata.newProperty("b.c.d", null, null, null, null, null,
null, false));
metadata.add(ItemMetadata.newProperty("c", null, null, null, null, null, 123,
false));
metadata.add(ItemMetadata.newProperty("d", null, null, null, null, null, true,
false));
metadata.add(ItemMetadata.newGroup("d", null, null, null));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
......@@ -52,7 +54,7 @@ public class JsonMarshallerTests {
outputStream.toByteArray()));
assertThat(read,
containsProperty("a.b", StringBuffer.class).fromSource(InputStream.class)
.withDescription("desc").withDefaultValue("x"));
.withDescription("desc").withDefaultValue("x").withDeprecated());
assertThat(read, containsProperty("b.c.d"));
assertThat(read, containsProperty("c").withDefaultValue(123));
assertThat(read, containsProperty("d").withDefaultValue(true));
......
......@@ -58,10 +58,12 @@ public class SimpleProperties {
return this.theName;
}
@Deprecated
public void setTheName(String name) {
this.theName = name;
}
@Deprecated
public boolean isFlag() {
return this.flag;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment