20
20
import java .util .Objects ;
21
21
import java .util .Set ;
22
22
import java .util .StringJoiner ;
23
+ import java .util .logging .Level ;
24
+ import java .util .logging .Logger ;
23
25
import javax .annotation .Nullable ;
24
26
25
27
/**
36
38
*/
37
39
public final class YamlDeclarativeConfigProperties implements DeclarativeConfigProperties {
38
40
41
+ private static final Logger logger =
42
+ Logger .getLogger (YamlDeclarativeConfigProperties .class .getName ());
43
+
39
44
private static final Set <Class <?>> SUPPORTED_SCALAR_TYPES =
40
45
Collections .unmodifiableSet (
41
46
new LinkedHashSet <>(
@@ -151,13 +156,13 @@ private static boolean isMap(Object object) {
151
156
@ Nullable
152
157
@ Override
153
158
public String getString (String name ) {
154
- return stringOrNull (simpleEntries .get (name ));
159
+ return stringOrNull (simpleEntries .get (name ), name );
155
160
}
156
161
157
162
@ Nullable
158
163
@ Override
159
164
public Boolean getBoolean (String name ) {
160
- return booleanOrNull (simpleEntries .get (name ));
165
+ return booleanOrNull (simpleEntries .get (name ), name );
161
166
}
162
167
163
168
@ Nullable
@@ -176,13 +181,13 @@ public Integer getInt(String name) {
176
181
@ Nullable
177
182
@ Override
178
183
public Long getLong (String name ) {
179
- return longOrNull (simpleEntries .get (name ));
184
+ return longOrNull (simpleEntries .get (name ), name );
180
185
}
181
186
182
187
@ Nullable
183
188
@ Override
184
189
public Double getDouble (String name ) {
185
- return doubleOrNull (simpleEntries .get (name ));
190
+ return doubleOrNull (simpleEntries .get (name ), name );
186
191
}
187
192
188
193
@ Nullable
@@ -210,13 +215,13 @@ public <T> List<T> getScalarList(String name, Class<T> scalarType) {
210
215
.map (
211
216
entry -> {
212
217
if (scalarType == String .class ) {
213
- return stringOrNull (entry );
218
+ return stringOrNull (entry , name );
214
219
} else if (scalarType == Boolean .class ) {
215
- return booleanOrNull (entry );
220
+ return booleanOrNull (entry , name );
216
221
} else if (scalarType == Long .class ) {
217
- return longOrNull (entry );
222
+ return longOrNull (entry , name );
218
223
} else if (scalarType == Double .class ) {
219
- return doubleOrNull (entry );
224
+ return doubleOrNull (entry , name );
220
225
}
221
226
return null ;
222
227
})
@@ -231,40 +236,52 @@ public <T> List<T> getScalarList(String name, Class<T> scalarType) {
231
236
}
232
237
233
238
@ Nullable
234
- private static String stringOrNull (@ Nullable Object value ) {
239
+ private static String stringOrNull (@ Nullable Object value , String name ) {
235
240
if (value instanceof String ) {
236
241
return (String ) value ;
237
242
}
243
+ if (value != null ) {
244
+ logTypeWarning (name , value , String .class );
245
+ }
238
246
return null ;
239
247
}
240
248
241
249
@ Nullable
242
- private static Boolean booleanOrNull (@ Nullable Object value ) {
250
+ private static Boolean booleanOrNull (@ Nullable Object value , String name ) {
243
251
if (value instanceof Boolean ) {
244
252
return (Boolean ) value ;
245
253
}
254
+ if (value != null ) {
255
+ logTypeWarning (name , value , Boolean .class );
256
+ }
246
257
return null ;
247
258
}
248
259
249
260
@ Nullable
250
- private static Long longOrNull (@ Nullable Object value ) {
261
+ private static Long longOrNull (@ Nullable Object value , String name ) {
251
262
if (value instanceof Integer ) {
252
263
return ((Integer ) value ).longValue ();
253
264
}
254
265
if (value instanceof Long ) {
255
266
return (Long ) value ;
256
267
}
268
+ if (value != null ) {
269
+ logTypeWarning (name , value , Long .class );
270
+ }
257
271
return null ;
258
272
}
259
273
260
274
@ Nullable
261
- private static Double doubleOrNull (@ Nullable Object value ) {
275
+ private static Double doubleOrNull (@ Nullable Object value , String name ) {
262
276
if (value instanceof Float ) {
263
277
return ((Float ) value ).doubleValue ();
264
278
}
265
279
if (value instanceof Double ) {
266
280
return (Double ) value ;
267
281
}
282
+ if (value != null ) {
283
+ logTypeWarning (name , value , Double .class );
284
+ }
268
285
return null ;
269
286
}
270
287
@@ -307,4 +324,11 @@ public String toString() {
307
324
public ComponentLoader getComponentLoader () {
308
325
return componentLoader ;
309
326
}
327
+
328
+ private static void logTypeWarning (String key , Object value , Class <?> expected ) {
329
+ logger .log (
330
+ Level .WARNING ,
331
+ "Ignoring value for key [{0}] because it is {1} instead of {2}: {3}" ,
332
+ new Object [] {key , value .getClass ().getSimpleName (), expected .getSimpleName (), value });
333
+ }
310
334
}
0 commit comments