Skip to content

Commit 441b65c

Browse files
committed
Fix #2339
1 parent 9e3c679 commit 441b65c

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

release-notes/CREDITS-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,5 @@ Manuel Hegner (manuel-hegner@github)
879879
Victor Noël (victornoel@github)
880880
* Reported #2338: Suboptimal return type for `JsonNode.withArray()`
881881
(2.10.0)
882+
* Reported #2339: Suboptimal return type for `ObjectNode.set()`
883+
(2.10.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Project: jackson-databind
4646
(suggested by Manuel H)
4747
#2338: Suboptimal return type for `JsonNode.withArray()`
4848
(reported by Victor N)
49+
2339: Suboptimal return type for `ObjectNode.set()`
50+
(reported by Victor N)
4951

5052
2.9.9 (16-May-2019)
5153

src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java

+27-12
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ public void serializeWithType(JsonGenerator g, SerializerProvider provider,
358358
*<p>
359359
* NOTE: added to replace those uses of {@link #put(String, JsonNode)}
360360
* where chaining with 'this' is desired.
361+
*<p>
362+
* NOTE: co-variant return type since 2.10
361363
*
362364
* @param value to set field to; if null, will be converted
363365
* to a {@link NullNode} first (to remove field entry, call
@@ -367,26 +369,30 @@ public void serializeWithType(JsonGenerator g, SerializerProvider provider,
367369
*
368370
* @since 2.1
369371
*/
370-
public JsonNode set(String fieldName, JsonNode value)
372+
@SuppressWarnings("unchecked")
373+
public <T extends JsonNode> T set(String fieldName, JsonNode value)
371374
{
372375
if (value == null) {
373376
value = nullNode();
374377
}
375378
_children.put(fieldName, value);
376-
return this;
379+
return (T) this;
377380
}
378381

379382
/**
380383
* Method for adding given properties to this object node, overriding
381384
* any existing values for those properties.
385+
*<p>
386+
* NOTE: co-variant return type since 2.10
382387
*
383388
* @param properties Properties to add
384389
*
385390
* @return This node after adding/replacing property values (to allow chaining)
386391
*
387392
* @since 2.1
388393
*/
389-
public JsonNode setAll(Map<String,? extends JsonNode> properties)
394+
@SuppressWarnings("unchecked")
395+
public <T extends JsonNode> T setAll(Map<String,? extends JsonNode> properties)
390396
{
391397
for (Map.Entry<String,? extends JsonNode> en : properties.entrySet()) {
392398
JsonNode n = en.getValue();
@@ -395,25 +401,28 @@ public JsonNode setAll(Map<String,? extends JsonNode> properties)
395401
}
396402
_children.put(en.getKey(), n);
397403
}
398-
return this;
404+
return (T) this;
399405
}
400406

401407
/**
402408
* Method for adding all properties of the given Object, overriding
403409
* any existing values for those properties.
410+
*<p>
411+
* NOTE: co-variant return type since 2.10
404412
*
405413
* @param other Object of which properties to add to this object
406414
*
407415
* @return This node after addition (to allow chaining)
408416
*
409417
* @since 2.1
410418
*/
411-
public JsonNode setAll(ObjectNode other)
419+
@SuppressWarnings("unchecked")
420+
public <T extends JsonNode> T setAll(ObjectNode other)
412421
{
413422
_children.putAll(other._children);
414-
return this;
423+
return (T) this;
415424
}
416-
425+
417426
/**
418427
* Method for replacing value of specific property with passed
419428
* value, and returning value (or null if none).
@@ -437,31 +446,37 @@ public JsonNode replace(String fieldName, JsonNode value)
437446
/**
438447
* Method for removing field entry from this ObjectNode, and
439448
* returning instance after removal.
449+
*<p>
450+
* NOTE: co-variant return type since 2.10
440451
*
441452
* @return This node after removing entry (if any)
442453
*
443454
* @since 2.1
444455
*/
445-
public JsonNode without(String fieldName)
456+
@SuppressWarnings("unchecked")
457+
public <T extends JsonNode> T without(String fieldName)
446458
{
447459
_children.remove(fieldName);
448-
return this;
460+
return (T) this;
449461
}
450462

451463
/**
452464
* Method for removing specified field properties out of
453465
* this ObjectNode.
466+
*<p>
467+
* NOTE: co-variant return type since 2.10
454468
*
455469
* @param fieldNames Names of fields to remove
456470
*
457471
* @return This node after removing entries
458472
*
459473
* @since 2.1
460474
*/
461-
public ObjectNode without(Collection<String> fieldNames)
475+
@SuppressWarnings("unchecked")
476+
public <T extends JsonNode> T without(Collection<String> fieldNames)
462477
{
463478
_children.keySet().removeAll(fieldNames);
464-
return this;
479+
return (T) this;
465480
}
466481

467482
/*
@@ -490,7 +505,7 @@ public JsonNode put(String fieldName, JsonNode value)
490505
}
491506
return _children.put(fieldName, value);
492507
}
493-
508+
494509
/**
495510
* Method for removing field entry from this ObjectNode.
496511
* Will return value of the field, if such field existed;

0 commit comments

Comments
 (0)