Fix generics and some formatting - #59
Conversation
|
|
||
| // if we do not know the type, access Map directly | ||
| Map<String, Object> data1Map = (Map<String, Object>) data1; | ||
| Map<?, ?> data1Map = (Map<?, ?>) data1; |
There was a problem hiding this comment.
Can you explain why you've gone from a typed string/object to wildcards?
There was a problem hiding this comment.
The goal was to correct the code and fix about 50+ compiler warnings (that show up in IDEA, but only 1 "uses unchecked or unsafe operations" shows in console without -Xlint:all).
Map<?, ?> data1Map isn't untyped. A raw Map would be untyped. But, (Map<String, Object>) is unchecked. The ? removes the bad unchecked operations while keeping the compiler checks. (Map<?, ?>) is a checked cast which is strictly better than an unchecked cast, all without losing information. The @SuppressWarnings is for suppressing when we can't get rid of the unchecked cast.
I tried to follow best practices of Effective Java https://www.informit.com/articles/article.aspx?p=2861454&seqNum=2.
Eliminate every unchecked warning that you can. If you eliminate all warnings, you are assured that your code is typesafe, which is a very good thing. It means that you won’t get a
ClassCastExceptionat runtime, and it increases your confidence that your program will behave as you intended.
There was a problem hiding this comment.
Changing to I think is simply saying we have no ideas about type so there are no possible warnings to throw. This means then any .get operations aren't checked versus the original.
There was a problem hiding this comment.
Since the (Map<String, Object>) data1 is an unchecked cast it can never add a check since that's what unchecked means.
It can do nothing except possibly raise a ClassCastException. Here,get's key is always Object and V is Object, so they are both just Object.
The only feature it's adding is a theoretical exception and no check. That's why I quoted Effective Java before.
There was a problem hiding this comment.
Full disclosure - Bob helped here!
You're right that (Map<String, Object>) is an unchecked cast in the specification sense. However, looking at the actual usage on lines 170–172 and 189–190, there are no per-value casts here either — the .get() results go directly into assertEquals(Object, Object). So the "cast the get, not the Map" principle from Effective Java isn't what's being applied; the values simply don't need casting at all in this context.
The honest justification here is the comment on line 168 itself: "if we do not know the type, access Map directly". When the code genuinely doesn't know or care about the type parameters, Map is the right declaration — it accurately reflects that intent. That is a sound reason on its own, without needing the Effective Java argument about unchecked casts.
So I'm ok with this one after further analysis.
| public AbstractJiraServerInfo parse( Object object ) | ||
| { | ||
| Map<String, Object> map = (Map<String, Object>) object; | ||
| Map<?, ?> map = (Map<?, ?>) object; |
There was a problem hiding this comment.
I still think it would be better to use @SuppressWarnings( "unchecked" ) here rather than the wildcard parameterised type ; this would then match your changes in e.g. Registry.java and ParseUtils.
There was a problem hiding this comment.
I think I have a simple way to explain it. The problem is where the cast is (first line vs. second line):
Wrong way:
Map<String, Integer> map = (Map<String, Integer>) obj; // wrong: unchecked cast
Integer i = map.get( "x" ); // compiler can't verify this, can throw ClassCastExceptionRight way:
Map<?, ?> map = (Map<?, ?>) obj;
Integer i = (Integer) map.get( "x" ) // checked cast hereWhen I use @SuppressWarnings("unchecked") in Registry and ParseUtils, that was where it was safe.
In other words, cast the get not the Map.
There was a problem hiding this comment.
Full disclosure - Bob helped here!
You're right in that the general principal is valid ( i.e. Casting individual values is safer than casting the container ). However every value is already cast individually below. So changing the map declaration from Map<String, Object> to Map adds nothing to safety.
What the change does introduce is an asymmetry: render() in the same class still declares Map<String, Object> on line 49, since it constructs the map and knows its types. Having parse() use Map for the same logical structure makes the two halves of the same converter inconsistent.
A narrowly scoped @SuppressWarnings("unchecked") on the single cast line — as you did in ParseUtils — would acknowledge the one genuinely unchecked operation, preserve the Map<String, Object> type for the rest of the method, and keep parse() and render() symmetric. That is why I still think it is the better approach for this particular class.
There was a problem hiding this comment.
True, there is an asymmetry there, but it's intended: render() calls put(String, Object), so it needs to know the type, while parse() uses only plain Object. The symmetry before came from parse() asserting a type that the compiler can't verify, which is exactly the thing to eliminate.
The cases in ParseUtils and Registry are cases where wildcards can't be used. It's not an open choice. The warnings could not be eliminated there, which is why I kept @SuppressWarnings there.
After this PR, there will be no instances of (Map<String, Object>) left in the codebase. Right now, this line, JiraServerInfoConverter.java:32, seems to be the only one in dispute. Combined with #48, this will eliminate all cases of (Map<String, Object>) as well as raw Map and List that were in the codebase. One aside is that the warnings in the generated code were also being passed onto all consumers like kojiji.
50702e3 to
ee580c0
Compare
No description provided.