Skip to main content

Type safety: Unchecked cast - Spring and Hibernate

Well, first of all, you're wasting memory with the new HashMap creation call. Your second line completely disregards the reference to this created hashmap, making it then available to the garbage collector. So, don't do that, use:
private Map<String, String> someMap = (HashMap<String, String>)getApplicationContext().getBean("someMap");
Secondly, the compiler is complaining that you cast the object to a HashMap without checking if it is a HashMap. But, even if you were to do:
if(getApplicationContext().getBean("someMap") instanceof HashMap) {
private Map<String, String> someMap = (HashMap<String, String>)getApplicationContext().getBean("someMap");}
You would probably still get this warning. The problem is, getBean returns Object, so it is unknown what the type is. Converting it to HashMap directly would not cause the problem with the second case (and perhaps there would not be a warning in the first case, I'm not sure how pedantic the Java compiler is with warnings for Java 5). However, you are converting it to a HashMap<String, String>.
HashMaps are really maps that take an object as a key and have an object as a value, HashMap<Object, Object> if you will. Thus, there is no guarantee that when you get your bean that it can be represented as a HashMap<String, String> because you could have HashMap<Date, Calendar> because the non-generic representation that is returned can have any objects.

If the code compiles, and you can execute String value = map.get("thisString"); without any errors, don't worry about this warning. But if the map isn't completely of string keys to string values, you will get a ClassCastException at runtime, because the generics cannot block this from happening in this case.

Comments

Popular posts from this blog

SVN to GIT Migration with revision history

This blog explains the process I have followed while moving one of code branch of one of the projects to Gitlab. SVN to Git migration can be done using the git-svn command in Git.  Documentation about this command can be found from here. https://git-scm.com/docs/git-svn My Company svn repository can be accessed using two ways svn+ssh://192.168.x.x/usr/local/ svnroot/branches/PROJECT/PROJECT_BRANCH http://svn.companyName.net/repos/svnroot/branches/ PROJECT/PROJECT_BRANCH SVN to GIT migration can be done using one of the above-mentioned URLs.  Git can be installed and configured to use its inbuilt SSH client or third party ssh client ( https://6xgate.github.io/TortoisePlink/ ) First of all, this process should be run in a server where the system does not go idle. I have tried this on my PC. But when I have locked the PC, it crashes. Therefore I have tried this in one of our internal server. I have tried to get SVN revision history when the P...

Java 11 Modularity and Spring REST

I have recently started working on creating a REST API for a client project.  I was using Java 8 for my past developments. Since Java 11 was introduced in the last September  (https://en.wikipedia.org/wiki/Java_version_history) I thought of developing the REST API in Java 11. Java 11 is having major changes after Java 8. Java 11 is having a concept of modules (This was introduced after Java 9). Several libraries are acting as separate modules. Those are not included in JDK and those have to be imported separately. Ref : http://openjdk.java.net/jeps/261 (I have experienced this in JBoss7 also. They were using the module system and each service can specify which modules which are using. Those modules can be manually configured by the user.) One example is that you have to import the Java fx library separately (For GUI). Java-Fx is decoupled from JDK. <!-- https://mvnrepository.com/artifact/org.openjfx/javafx -->   ...

President - Association of Computer Engineering Students

I was selected to be the president of ACES, Association of Computer Engineering Students, the official student body of the Department of Computer Engineering Students. ACES Site :  aces.ce.pdn.ac.lk  It was a great opportunity to work with fellow undergraduates, lecturers, senior graduates, and industry. Until now We were able to organize few events in University of Peradeniya and Colombo. This event was organized by ACES Council, a group of 14 undergraduates. The events are, ACES Hackathon - This is an inner hackathon for undergraduates in the University of Peradeniya. ACES Hackathon, held annually for the last 5 years was held from the 29th of April to the 1st of May 2016 at the Faculty of Engineering, University of Peradeniya. The Hackathon brings together undergraduates from the Faculty of Engineering and Science for 3 days to develop their creative skills to provide innovative IT solutions. This year there were 150+ participants.   www.readme.lk/saw-ac...