Skip to content

Commit 78dae86

Browse files
author
Piyal Ahmed
committed
Throwing specific exception(JSONException) instead of Exception
1 parent 0f44880 commit 78dae86

File tree

3 files changed

+51
-43
lines changed

3 files changed

+51
-43
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424

2525
import org.springframework.boot.configurationprocessor.json.JSONArray;
26+
import org.springframework.boot.configurationprocessor.json.JSONException;
2627
import org.springframework.boot.configurationprocessor.json.JSONObject;
2728
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
2829

@@ -36,7 +37,7 @@ class JsonConverter {
3637

3738
private static final ItemMetadataComparator ITEM_COMPARATOR = new ItemMetadataComparator();
3839

39-
JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws Exception {
40+
JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws JSONException {
4041
JSONArray jsonArray = new JSONArray();
4142
List<ItemMetadata> items = metadata.getItems()
4243
.stream()
@@ -51,15 +52,15 @@ JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws
5152
return jsonArray;
5253
}
5354

54-
JSONArray toJsonArray(Collection<ItemHint> hints) throws Exception {
55+
JSONArray toJsonArray(Collection<ItemHint> hints) throws JSONException {
5556
JSONArray jsonArray = new JSONArray();
5657
for (ItemHint hint : hints) {
5758
jsonArray.put(toJsonObject(hint));
5859
}
5960
return jsonArray;
6061
}
6162

62-
JSONObject toJsonObject(ItemMetadata item) throws Exception {
63+
JSONObject toJsonObject(ItemMetadata item) throws JSONException {
6364
JSONObject jsonObject = new JSONObject();
6465
jsonObject.put("name", item.getName());
6566
jsonObject.putOpt("type", item.getType());
@@ -73,25 +74,30 @@ JSONObject toJsonObject(ItemMetadata item) throws Exception {
7374
ItemDeprecation deprecation = item.getDeprecation();
7475
if (deprecation != null) {
7576
jsonObject.put("deprecated", true); // backward compatibility
76-
JSONObject deprecationJsonObject = new JSONObject();
77-
if (deprecation.getLevel() != null) {
78-
deprecationJsonObject.put("level", deprecation.getLevel());
79-
}
80-
if (deprecation.getReason() != null) {
81-
deprecationJsonObject.put("reason", deprecation.getReason());
82-
}
83-
if (deprecation.getReplacement() != null) {
84-
deprecationJsonObject.put("replacement", deprecation.getReplacement());
85-
}
86-
if (deprecation.getSince() != null) {
87-
deprecationJsonObject.put("since", deprecation.getSince());
88-
}
77+
JSONObject deprecationJsonObject = getDeprecationJsonObject(deprecation);
8978
jsonObject.put("deprecation", deprecationJsonObject);
9079
}
9180
return jsonObject;
9281
}
9382

94-
private JSONObject toJsonObject(ItemHint hint) throws Exception {
83+
private static JSONObject getDeprecationJsonObject(ItemDeprecation deprecation) throws JSONException {
84+
JSONObject deprecationJsonObject = new JSONObject();
85+
if (deprecation.getLevel() != null) {
86+
deprecationJsonObject.put("level", deprecation.getLevel());
87+
}
88+
if (deprecation.getReason() != null) {
89+
deprecationJsonObject.put("reason", deprecation.getReason());
90+
}
91+
if (deprecation.getReplacement() != null) {
92+
deprecationJsonObject.put("replacement", deprecation.getReplacement());
93+
}
94+
if (deprecation.getSince() != null) {
95+
deprecationJsonObject.put("since", deprecation.getSince());
96+
}
97+
return deprecationJsonObject;
98+
}
99+
100+
private JSONObject toJsonObject(ItemHint hint) throws JSONException {
95101
JSONObject jsonObject = new JSONObject();
96102
jsonObject.put("name", hint.getName());
97103
if (!hint.getValues().isEmpty()) {
@@ -103,30 +109,30 @@ private JSONObject toJsonObject(ItemHint hint) throws Exception {
103109
return jsonObject;
104110
}
105111

106-
private JSONArray getItemHintValues(ItemHint hint) throws Exception {
112+
private JSONArray getItemHintValues(ItemHint hint) throws JSONException {
107113
JSONArray values = new JSONArray();
108114
for (ItemHint.ValueHint value : hint.getValues()) {
109115
values.put(getItemHintValue(value));
110116
}
111117
return values;
112118
}
113119

114-
private JSONObject getItemHintValue(ItemHint.ValueHint value) throws Exception {
120+
private JSONObject getItemHintValue(ItemHint.ValueHint value) throws JSONException {
115121
JSONObject result = new JSONObject();
116122
putHintValue(result, value.getValue());
117123
result.putOpt("description", value.getDescription());
118124
return result;
119125
}
120126

121-
private JSONArray getItemHintProviders(ItemHint hint) throws Exception {
127+
private JSONArray getItemHintProviders(ItemHint hint) throws JSONException {
122128
JSONArray providers = new JSONArray();
123129
for (ItemHint.ValueProvider provider : hint.getProviders()) {
124130
providers.put(getItemHintProvider(provider));
125131
}
126132
return providers;
127133
}
128134

129-
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws Exception {
135+
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws JSONException {
130136
JSONObject result = new JSONObject();
131137
result.put("name", provider.getName());
132138
if (provider.getParameters() != null && !provider.getParameters().isEmpty()) {
@@ -139,12 +145,12 @@ private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws E
139145
return result;
140146
}
141147

142-
private void putHintValue(JSONObject jsonObject, Object value) throws Exception {
148+
private void putHintValue(JSONObject jsonObject, Object value) throws JSONException {
143149
Object hintValue = extractItemValue(value);
144150
jsonObject.put("value", hintValue);
145151
}
146152

147-
private void putDefaultValue(JSONObject jsonObject, Object value) throws Exception {
153+
private void putDefaultValue(JSONObject jsonObject, Object value) throws JSONException {
148154
Object defaultValue = extractItemValue(value);
149155
jsonObject.put("defaultValue", defaultValue);
150156
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MergeMetadataGenerationTests.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import org.springframework.boot.configurationprocessor.json.JSONArray;
26+
import org.springframework.boot.configurationprocessor.json.JSONException;
2627
import org.springframework.boot.configurationprocessor.json.JSONObject;
2728
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
2829
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
@@ -47,7 +48,7 @@
4748
class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
4849

4950
@Test
50-
void mergingOfAdditionalProperty() throws Exception {
51+
void mergingOfAdditionalProperty() throws JSONException {
5152
ItemMetadata property = ItemMetadata.newProperty(null, "foo", "java.lang.String",
5253
AdditionalMetadata.class.getName(), null, null, null, null);
5354
String additionalMetadata = buildAdditionalMetadata(property);
@@ -57,7 +58,7 @@ void mergingOfAdditionalProperty() throws Exception {
5758
}
5859

5960
@Test
60-
void mergingOfAdditionalPropertyMatchingGroup() throws Exception {
61+
void mergingOfAdditionalPropertyMatchingGroup() throws JSONException {
6162
ItemMetadata property = ItemMetadata.newProperty(null, "simple", "java.lang.String", null, null, null, null,
6263
null);
6364
String additionalMetadata = buildAdditionalMetadata(property);
@@ -67,7 +68,7 @@ void mergingOfAdditionalPropertyMatchingGroup() throws Exception {
6768
}
6869

6970
@Test
70-
void mergeExistingPropertyDefaultValue() throws Exception {
71+
void mergeExistingPropertyDefaultValue() throws JSONException {
7172
ItemMetadata property = ItemMetadata.newProperty("simple", "flag", null, null, null, null, true, null);
7273
String additionalMetadata = buildAdditionalMetadata(property);
7374
ConfigurationMetadata metadata = compile(additionalMetadata, SimpleProperties.class);
@@ -80,7 +81,7 @@ void mergeExistingPropertyDefaultValue() throws Exception {
8081
}
8182

8283
@Test
83-
void mergeExistingPropertyWithSeveralCandidates() throws Exception {
84+
void mergeExistingPropertyWithSeveralCandidates() throws JSONException {
8485
ItemMetadata property = ItemMetadata.newProperty("simple", "flag", Boolean.class.getName(), null, null, null,
8586
true, null);
8687
String additionalMetadata = buildAdditionalMetadata(property);
@@ -111,7 +112,7 @@ void mergeExistingPropertyWithSeveralCandidates() throws Exception {
111112
}
112113

113114
@Test
114-
void mergeExistingPropertyDescription() throws Exception {
115+
void mergeExistingPropertyDescription() throws JSONException {
115116
ItemMetadata property = ItemMetadata.newProperty("simple", "comparator", null, null, null, "A nice comparator.",
116117
null, null);
117118
String additionalMetadata = buildAdditionalMetadata(property);
@@ -123,7 +124,7 @@ void mergeExistingPropertyDescription() throws Exception {
123124
}
124125

125126
@Test
126-
void mergeExistingPropertyDeprecation() throws Exception {
127+
void mergeExistingPropertyDeprecation() throws JSONException {
127128
ItemMetadata property = ItemMetadata.newProperty("simple", "comparator", null, null, null, null, null,
128129
new ItemDeprecation("Don't use this.", "simple.complex-comparator", "1.2.3", "error"));
129130
String additionalMetadata = buildAdditionalMetadata(property);
@@ -135,7 +136,7 @@ void mergeExistingPropertyDeprecation() throws Exception {
135136
}
136137

137138
@Test
138-
void mergeExistingPropertyDeprecationOverride() throws Exception {
139+
void mergeExistingPropertyDeprecationOverride() throws JSONException {
139140
ItemMetadata property = ItemMetadata.newProperty("singledeprecated", "name", null, null, null, null, null,
140141
new ItemDeprecation("Don't use this.", "single.name", "1.2.3"));
141142
String additionalMetadata = buildAdditionalMetadata(property);
@@ -147,7 +148,7 @@ void mergeExistingPropertyDeprecationOverride() throws Exception {
147148
}
148149

149150
@Test
150-
void mergeExistingPropertyDeprecationOverrideLevel() throws Exception {
151+
void mergeExistingPropertyDeprecationOverrideLevel() throws JSONException {
151152
ItemMetadata property = ItemMetadata.newProperty("singledeprecated", "name", null, null, null, null, null,
152153
new ItemDeprecation(null, null, null, "error"));
153154
String additionalMetadata = buildAdditionalMetadata(property);
@@ -167,7 +168,7 @@ void mergeOfInvalidAdditionalMetadata() {
167168
}
168169

169170
@Test
170-
void mergingOfSimpleHint() throws Exception {
171+
void mergingOfSimpleHint() throws JSONException {
171172
String hints = buildAdditionalHints(ItemHint.newHint("simple.the-name",
172173
new ItemHint.ValueHint("boot", "Bla bla"), new ItemHint.ValueHint("spring", null)));
173174
ConfigurationMetadata metadata = compile(hints, SimpleProperties.class);
@@ -181,7 +182,7 @@ void mergingOfSimpleHint() throws Exception {
181182
}
182183

183184
@Test
184-
void mergingOfHintWithNonCanonicalName() throws Exception {
185+
void mergingOfHintWithNonCanonicalName() throws JSONException {
185186
String hints = buildAdditionalHints(
186187
ItemHint.newHint("simple.theName", new ItemHint.ValueHint("boot", "Bla bla")));
187188
ConfigurationMetadata metadata = compile(hints, SimpleProperties.class);
@@ -194,7 +195,7 @@ void mergingOfHintWithNonCanonicalName() throws Exception {
194195
}
195196

196197
@Test
197-
void mergingOfHintWithProvider() throws Exception {
198+
void mergingOfHintWithProvider() throws JSONException {
198199
String hints = buildAdditionalHints(new ItemHint("simple.theName", Collections.emptyList(),
199200
Arrays.asList(new ItemHint.ValueProvider("first", Collections.singletonMap("target", "org.foo")),
200201
new ItemHint.ValueProvider("second", null))));
@@ -209,7 +210,7 @@ void mergingOfHintWithProvider() throws Exception {
209210
}
210211

211212
@Test
212-
void mergingOfAdditionalDeprecation() throws Exception {
213+
void mergingOfAdditionalDeprecation() throws JSONException {
213214
String deprecations = buildPropertyDeprecations(
214215
ItemMetadata.newProperty("simple", "wrongName", "java.lang.String", null, null, null, null,
215216
new ItemDeprecation("Lame name.", "simple.the-name", "1.2.3")));
@@ -219,7 +220,7 @@ void mergingOfAdditionalDeprecation() throws Exception {
219220
}
220221

221222
@Test
222-
void mergingOfAdditionalMetadata() throws Exception {
223+
void mergingOfAdditionalMetadata() throws JSONException {
223224
JSONObject property = new JSONObject();
224225
property.put("name", "foo");
225226
property.put("type", "java.lang.String");
@@ -234,7 +235,7 @@ void mergingOfAdditionalMetadata() throws Exception {
234235
assertThat(metadata).has(Metadata.withProperty("foo", String.class).fromSource(AdditionalMetadata.class));
235236
}
236237

237-
private String buildAdditionalMetadata(ItemMetadata... metadata) throws Exception {
238+
private String buildAdditionalMetadata(ItemMetadata... metadata) throws JSONException {
238239
TestJsonConverter converter = new TestJsonConverter();
239240
JSONObject additionalMetadata = new JSONObject();
240241
JSONArray properties = new JSONArray();
@@ -245,14 +246,14 @@ private String buildAdditionalMetadata(ItemMetadata... metadata) throws Exceptio
245246
return additionalMetadata.toString();
246247
}
247248

248-
private String buildAdditionalHints(ItemHint... hints) throws Exception {
249+
private String buildAdditionalHints(ItemHint... hints) throws JSONException {
249250
TestJsonConverter converter = new TestJsonConverter();
250251
JSONObject additionalMetadata = new JSONObject();
251252
additionalMetadata.put("hints", converter.toJsonArray(Arrays.asList(hints)));
252253
return additionalMetadata.toString();
253254
}
254255

255-
private String buildPropertyDeprecations(ItemMetadata... items) throws Exception {
256+
private String buildPropertyDeprecations(ItemMetadata... items) throws JSONException {
256257
JSONArray propertiesArray = new JSONArray();
257258
for (ItemMetadata item : items) {
258259
JSONObject jsonObject = new JSONObject();

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collection;
2020

2121
import org.springframework.boot.configurationprocessor.json.JSONArray;
22+
import org.springframework.boot.configurationprocessor.json.JSONException;
2223
import org.springframework.boot.configurationprocessor.json.JSONObject;
2324
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
2425

@@ -30,17 +31,17 @@
3031
public class TestJsonConverter extends JsonConverter {
3132

3233
@Override
33-
public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws Exception {
34+
public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws JSONException {
3435
return super.toJsonArray(metadata, itemType);
3536
}
3637

3738
@Override
38-
public JSONArray toJsonArray(Collection<ItemHint> hints) throws Exception {
39+
public JSONArray toJsonArray(Collection<ItemHint> hints) throws JSONException {
3940
return super.toJsonArray(hints);
4041
}
4142

4243
@Override
43-
public JSONObject toJsonObject(ItemMetadata item) throws Exception {
44+
public JSONObject toJsonObject(ItemMetadata item) throws JSONException {
4445
return super.toJsonObject(item);
4546
}
4647

0 commit comments

Comments
 (0)