Skip to content

Commit

Permalink
Make JsonObject.getMap generic
Browse files Browse the repository at this point in the history
  • Loading branch information
purplefox committed Mar 24, 2015
1 parent 0f30048 commit 9d14e8b
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/vertx/core/json/JsonObject.java
Expand Up @@ -661,8 +661,9 @@ public JsonObject copy() {
*
* @return the underlying Map.
*/
public Map<String, Object> getMap() {
return map;
@SuppressWarnings("unchecked")
public <T> Map<String, T> getMap() {
return (Map<String, T>)map;
}

/**
Expand Down

4 comments on commit 9d14e8b

@vietj
Copy link
Member

@vietj vietj commented on 9d14e8b Mar 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows to write type unsafe code like:

JsonObject obj = new JsonObject();
obj.put("foo", 4);
Map<String, String> map = obj.getMap();
String b = map.get("foo"); // ClassCastException

@purplefox
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True but in the old code you could also write type unsafe code:

JsonObject obj = new JsonObject();
obj.put("foo", 4);
Map<String, Object> map = obj.getMap();
String b = (String)map.get("foo"); // ClassCastException

So, I'm not sure it's really any worse ;)

@vietj
Copy link
Member

@vietj vietj commented on 9d14e8b Mar 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the explicit cast warns the developer there is a potential classcastexception, with the free cast the developer assumes classcast exception cannot happen.

without being dogmatic, I think this lead the developer to make wrong assumption/reasoning about the code, when someone sees a stacktrace happening at line:

String b = map.get("foo");

He will not understand where this CCE is coming from nor why it happens.

@purplefox
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. I will revert the commits and tell the user that the request is rejected on the BZ.

Please sign in to comment.