diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java index bd6239e19df1..eb57cdd11444 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java @@ -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. @@ -23,6 +23,7 @@ import java.util.Map; import org.springframework.boot.configurationprocessor.json.JSONArray; +import org.springframework.boot.configurationprocessor.json.JSONException; import org.springframework.boot.configurationprocessor.json.JSONObject; import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType; @@ -36,7 +37,7 @@ class JsonConverter { private static final ItemMetadataComparator ITEM_COMPARATOR = new ItemMetadataComparator(); - JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws Exception { + JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws JSONException { JSONArray jsonArray = new JSONArray(); List items = metadata.getItems() .stream() @@ -51,7 +52,7 @@ JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws return jsonArray; } - JSONArray toJsonArray(Collection hints) throws Exception { + JSONArray toJsonArray(Collection hints) throws JSONException { JSONArray jsonArray = new JSONArray(); for (ItemHint hint : hints) { jsonArray.put(toJsonObject(hint)); @@ -59,7 +60,7 @@ JSONArray toJsonArray(Collection hints) throws Exception { return jsonArray; } - JSONObject toJsonObject(ItemMetadata item) throws Exception { + JSONObject toJsonObject(ItemMetadata item) throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put("name", item.getName()); jsonObject.putOpt("type", item.getType()); @@ -73,25 +74,30 @@ JSONObject toJsonObject(ItemMetadata item) throws Exception { ItemDeprecation deprecation = item.getDeprecation(); if (deprecation != null) { jsonObject.put("deprecated", true); // backward compatibility - JSONObject deprecationJsonObject = new JSONObject(); - if (deprecation.getLevel() != null) { - deprecationJsonObject.put("level", deprecation.getLevel()); - } - if (deprecation.getReason() != null) { - deprecationJsonObject.put("reason", deprecation.getReason()); - } - if (deprecation.getReplacement() != null) { - deprecationJsonObject.put("replacement", deprecation.getReplacement()); - } - if (deprecation.getSince() != null) { - deprecationJsonObject.put("since", deprecation.getSince()); - } + JSONObject deprecationJsonObject = getDeprecationJsonObject(deprecation); jsonObject.put("deprecation", deprecationJsonObject); } return jsonObject; } - private JSONObject toJsonObject(ItemHint hint) throws Exception { + private static JSONObject getDeprecationJsonObject(ItemDeprecation deprecation) throws JSONException { + JSONObject deprecationJsonObject = new JSONObject(); + if (deprecation.getLevel() != null) { + deprecationJsonObject.put("level", deprecation.getLevel()); + } + if (deprecation.getReason() != null) { + deprecationJsonObject.put("reason", deprecation.getReason()); + } + if (deprecation.getReplacement() != null) { + deprecationJsonObject.put("replacement", deprecation.getReplacement()); + } + if (deprecation.getSince() != null) { + deprecationJsonObject.put("since", deprecation.getSince()); + } + return deprecationJsonObject; + } + + private JSONObject toJsonObject(ItemHint hint) throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put("name", hint.getName()); if (!hint.getValues().isEmpty()) { @@ -103,7 +109,7 @@ private JSONObject toJsonObject(ItemHint hint) throws Exception { return jsonObject; } - private JSONArray getItemHintValues(ItemHint hint) throws Exception { + private JSONArray getItemHintValues(ItemHint hint) throws JSONException { JSONArray values = new JSONArray(); for (ItemHint.ValueHint value : hint.getValues()) { values.put(getItemHintValue(value)); @@ -111,14 +117,14 @@ private JSONArray getItemHintValues(ItemHint hint) throws Exception { return values; } - private JSONObject getItemHintValue(ItemHint.ValueHint value) throws Exception { + private JSONObject getItemHintValue(ItemHint.ValueHint value) throws JSONException { JSONObject result = new JSONObject(); putHintValue(result, value.getValue()); result.putOpt("description", value.getDescription()); return result; } - private JSONArray getItemHintProviders(ItemHint hint) throws Exception { + private JSONArray getItemHintProviders(ItemHint hint) throws JSONException { JSONArray providers = new JSONArray(); for (ItemHint.ValueProvider provider : hint.getProviders()) { providers.put(getItemHintProvider(provider)); @@ -126,7 +132,7 @@ private JSONArray getItemHintProviders(ItemHint hint) throws Exception { return providers; } - private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws Exception { + private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws JSONException { JSONObject result = new JSONObject(); result.put("name", provider.getName()); if (provider.getParameters() != null && !provider.getParameters().isEmpty()) { @@ -139,12 +145,12 @@ private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws E return result; } - private void putHintValue(JSONObject jsonObject, Object value) throws Exception { + private void putHintValue(JSONObject jsonObject, Object value) throws JSONException { Object hintValue = extractItemValue(value); jsonObject.put("value", hintValue); } - private void putDefaultValue(JSONObject jsonObject, Object value) throws Exception { + private void putDefaultValue(JSONObject jsonObject, Object value) throws JSONException { Object defaultValue = extractItemValue(value); jsonObject.put("defaultValue", defaultValue); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MergeMetadataGenerationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MergeMetadataGenerationTests.java index c35e74755c85..fee21edc5a66 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MergeMetadataGenerationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MergeMetadataGenerationTests.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.configurationprocessor.json.JSONArray; +import org.springframework.boot.configurationprocessor.json.JSONException; import org.springframework.boot.configurationprocessor.json.JSONObject; import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata; import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation; @@ -47,7 +48,7 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests { @Test - void mergingOfAdditionalProperty() throws Exception { + void mergingOfAdditionalProperty() throws JSONException { ItemMetadata property = ItemMetadata.newProperty(null, "foo", "java.lang.String", AdditionalMetadata.class.getName(), null, null, null, null); String additionalMetadata = buildAdditionalMetadata(property); @@ -57,7 +58,7 @@ void mergingOfAdditionalProperty() throws Exception { } @Test - void mergingOfAdditionalPropertyMatchingGroup() throws Exception { + void mergingOfAdditionalPropertyMatchingGroup() throws JSONException { ItemMetadata property = ItemMetadata.newProperty(null, "simple", "java.lang.String", null, null, null, null, null); String additionalMetadata = buildAdditionalMetadata(property); @@ -67,7 +68,7 @@ void mergingOfAdditionalPropertyMatchingGroup() throws Exception { } @Test - void mergeExistingPropertyDefaultValue() throws Exception { + void mergeExistingPropertyDefaultValue() throws JSONException { ItemMetadata property = ItemMetadata.newProperty("simple", "flag", null, null, null, null, true, null); String additionalMetadata = buildAdditionalMetadata(property); ConfigurationMetadata metadata = compile(additionalMetadata, SimpleProperties.class); @@ -80,7 +81,7 @@ void mergeExistingPropertyDefaultValue() throws Exception { } @Test - void mergeExistingPropertyWithSeveralCandidates() throws Exception { + void mergeExistingPropertyWithSeveralCandidates() throws JSONException { ItemMetadata property = ItemMetadata.newProperty("simple", "flag", Boolean.class.getName(), null, null, null, true, null); String additionalMetadata = buildAdditionalMetadata(property); @@ -111,7 +112,7 @@ void mergeExistingPropertyWithSeveralCandidates() throws Exception { } @Test - void mergeExistingPropertyDescription() throws Exception { + void mergeExistingPropertyDescription() throws JSONException { ItemMetadata property = ItemMetadata.newProperty("simple", "comparator", null, null, null, "A nice comparator.", null, null); String additionalMetadata = buildAdditionalMetadata(property); @@ -123,7 +124,7 @@ void mergeExistingPropertyDescription() throws Exception { } @Test - void mergeExistingPropertyDeprecation() throws Exception { + void mergeExistingPropertyDeprecation() throws JSONException { ItemMetadata property = ItemMetadata.newProperty("simple", "comparator", null, null, null, null, null, new ItemDeprecation("Don't use this.", "simple.complex-comparator", "1.2.3", "error")); String additionalMetadata = buildAdditionalMetadata(property); @@ -135,7 +136,7 @@ void mergeExistingPropertyDeprecation() throws Exception { } @Test - void mergeExistingPropertyDeprecationOverride() throws Exception { + void mergeExistingPropertyDeprecationOverride() throws JSONException { ItemMetadata property = ItemMetadata.newProperty("singledeprecated", "name", null, null, null, null, null, new ItemDeprecation("Don't use this.", "single.name", "1.2.3")); String additionalMetadata = buildAdditionalMetadata(property); @@ -147,7 +148,7 @@ void mergeExistingPropertyDeprecationOverride() throws Exception { } @Test - void mergeExistingPropertyDeprecationOverrideLevel() throws Exception { + void mergeExistingPropertyDeprecationOverrideLevel() throws JSONException { ItemMetadata property = ItemMetadata.newProperty("singledeprecated", "name", null, null, null, null, null, new ItemDeprecation(null, null, null, "error")); String additionalMetadata = buildAdditionalMetadata(property); @@ -167,7 +168,7 @@ void mergeOfInvalidAdditionalMetadata() { } @Test - void mergingOfSimpleHint() throws Exception { + void mergingOfSimpleHint() throws JSONException { String hints = buildAdditionalHints(ItemHint.newHint("simple.the-name", new ItemHint.ValueHint("boot", "Bla bla"), new ItemHint.ValueHint("spring", null))); ConfigurationMetadata metadata = compile(hints, SimpleProperties.class); @@ -181,7 +182,7 @@ void mergingOfSimpleHint() throws Exception { } @Test - void mergingOfHintWithNonCanonicalName() throws Exception { + void mergingOfHintWithNonCanonicalName() throws JSONException { String hints = buildAdditionalHints( ItemHint.newHint("simple.theName", new ItemHint.ValueHint("boot", "Bla bla"))); ConfigurationMetadata metadata = compile(hints, SimpleProperties.class); @@ -194,7 +195,7 @@ void mergingOfHintWithNonCanonicalName() throws Exception { } @Test - void mergingOfHintWithProvider() throws Exception { + void mergingOfHintWithProvider() throws JSONException { String hints = buildAdditionalHints(new ItemHint("simple.theName", Collections.emptyList(), Arrays.asList(new ItemHint.ValueProvider("first", Collections.singletonMap("target", "org.foo")), new ItemHint.ValueProvider("second", null)))); @@ -209,7 +210,7 @@ void mergingOfHintWithProvider() throws Exception { } @Test - void mergingOfAdditionalDeprecation() throws Exception { + void mergingOfAdditionalDeprecation() throws JSONException { String deprecations = buildPropertyDeprecations( ItemMetadata.newProperty("simple", "wrongName", "java.lang.String", null, null, null, null, new ItemDeprecation("Lame name.", "simple.the-name", "1.2.3"))); @@ -219,7 +220,7 @@ void mergingOfAdditionalDeprecation() throws Exception { } @Test - void mergingOfAdditionalMetadata() throws Exception { + void mergingOfAdditionalMetadata() throws JSONException { JSONObject property = new JSONObject(); property.put("name", "foo"); property.put("type", "java.lang.String"); @@ -234,7 +235,7 @@ void mergingOfAdditionalMetadata() throws Exception { assertThat(metadata).has(Metadata.withProperty("foo", String.class).fromSource(AdditionalMetadata.class)); } - private String buildAdditionalMetadata(ItemMetadata... metadata) throws Exception { + private String buildAdditionalMetadata(ItemMetadata... metadata) throws JSONException { TestJsonConverter converter = new TestJsonConverter(); JSONObject additionalMetadata = new JSONObject(); JSONArray properties = new JSONArray(); @@ -245,14 +246,14 @@ private String buildAdditionalMetadata(ItemMetadata... metadata) throws Exceptio return additionalMetadata.toString(); } - private String buildAdditionalHints(ItemHint... hints) throws Exception { + private String buildAdditionalHints(ItemHint... hints) throws JSONException { TestJsonConverter converter = new TestJsonConverter(); JSONObject additionalMetadata = new JSONObject(); additionalMetadata.put("hints", converter.toJsonArray(Arrays.asList(hints))); return additionalMetadata.toString(); } - private String buildPropertyDeprecations(ItemMetadata... items) throws Exception { + private String buildPropertyDeprecations(ItemMetadata... items) throws JSONException { JSONArray propertiesArray = new JSONArray(); for (ItemMetadata item : items) { JSONObject jsonObject = new JSONObject(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java index 0f2c92e9445d..b53cdc332cdd 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java @@ -19,6 +19,7 @@ import java.util.Collection; import org.springframework.boot.configurationprocessor.json.JSONArray; +import org.springframework.boot.configurationprocessor.json.JSONException; import org.springframework.boot.configurationprocessor.json.JSONObject; import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType; @@ -30,17 +31,17 @@ public class TestJsonConverter extends JsonConverter { @Override - public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws Exception { + public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws JSONException { return super.toJsonArray(metadata, itemType); } @Override - public JSONArray toJsonArray(Collection hints) throws Exception { + public JSONArray toJsonArray(Collection hints) throws JSONException { return super.toJsonArray(hints); } @Override - public JSONObject toJsonObject(ItemMetadata item) throws Exception { + public JSONObject toJsonObject(ItemMetadata item) throws JSONException { return super.toJsonObject(item); }