From f75b266d647e5bd48ec9563acaa8cf63b38babd6 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 19 Nov 2014 14:15:35 -0800 Subject: [PATCH] Add 'deprecated' field to configuration meta-data Update the ConfigurationMetadataAnnotationProcessor to detect the @Deprecated annotation on getters or setters. Fixes gh-1846 --- ...figurationMetadataAnnotationProcessor.java | 8 ++++- .../metadata/ItemMetadata.java | 20 ++++++++---- .../metadata/JsonMarshaller.java | 6 +++- ...ationMetadataAnnotationProcessorTests.java | 7 +++-- .../ConfigurationMetadataMatchers.java | 31 ++++++++++++++----- .../metadata/JsonMarshallerTests.java | 14 +++++---- .../simple/SimpleProperties.java | 2 ++ 7 files changed, 63 insertions(+), 25 deletions(-) diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java index b915318192b..6f6b580635f 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java @@ -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()) { diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemMetadata.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemMetadata.java index f261f4a9b50..1a465dfad77 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemMetadata.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemMetadata.java @@ -36,13 +36,15 @@ public class ItemMetadata implements Comparable { 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 { 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 { 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 { 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 { 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); } /** diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java index c05f7b6156c..14e38706c05 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java @@ -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 { diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java index 14acc8e117e..eee8c714a4e 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java @@ -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"))); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataMatchers.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataMatchers.java index 0ffe042db40..d6ee84bb968 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataMatchers.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataMatchers.java @@ -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 Matcher withDefaultValue(Object defaultValue) { + public ContainsItemMatcher withDefaultValue(Object defaultValue) { return new ContainsItemMatcher(this.itemType, this.name, this.type, - this.sourceType, this.description, defaultValue); + this.sourceType, this.description, defaultValue, this.deprecated); + } + + public ContainsItemMatcher withDeprecated() { + return new ContainsItemMatcher(this.itemType, this.name, this.type, + this.sourceType, this.description, this.defaultValue, true); } private ItemMetadata getFirstPropertyWithName(ConfigurationMetadata metadata, diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java index 76a04631ff7..9780fe06699 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java @@ -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)); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleProperties.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleProperties.java index c5e4379f367..1396993afb8 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleProperties.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleProperties.java @@ -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; }