Created
July 12, 2016 16:28
-
-
Save oehme/bd548e122c2d47144328cbfb714c2142 to your computer and use it in GitHub Desktop.
Why ignoring type safety warnings is bad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Foo> specialThings = Lists.newArrayList(); //Foo extends Base | |
Bean bean = new Bean(); | |
// normally you would correctly get a compile error here, | |
//but the setter in your example does an unsafe cast that makes this line compile | |
bean.setStuff(specialThings); | |
List<Base> things = bean.getStuff(); | |
things.add(new Bar()); // Bar extends Base | |
//compiles fine, but explodes at runtime as the first element in the list is a Bar, not a Foo | |
Foo boom = specialThings.get(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment