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