With Java 8 update101 HashMap.entries cannot be cast to Collection
After updating to Java 8 update 101, I am getting exception in following code. It was working fine with Java 8 update 91. Accessing keystore:
KeyStore ks = KeyStore.getInstance("WINDOWS-MY"); ks.load(null, null); Field field = ks.getClass().getDeclaredField("keyStoreSpi"); field.setAccessible(true); KeyStoreSpi kss = (KeyStoreSpi) field.get(ks); Collection entries; field = kss.getClass().getEnclosingClass().getDeclaredField("entries"); field.setAccessible(true); // This is where the exception happens entries = (Collection) field.get(kss); // I then have to loop on these entries, something like this: for (Object entry : entries) < //code >
java.util.HashMap cannot be cast to java.util.Collection
As chrylis noted, you are messing with the internals of class KeyStore , by accessing a private field via reflection. You’re not supposed to do that, and now you’re suffering the consequences. The implementation of classes might change between Java versions, as has probably happened here from 8u91 to 8u101. Only use the publicly documented API of classes such as KeyStore , don’t hack by accessing private fields via reflection. Why do you need to do this?
@tarunk you are messing around with non-public implementation details of the class. The JDK developers are free to change those details because they’ve not promised any particular behaviour.
@tarunk What you mean by recent changes? Map never was castable to Collection . If it would than Collection interface have for instance add(E e) element method. What that mean in context of Map ‘s key, value?
4 Answers 4
As pointed out in the comments, what your codebase is doing is nasty. It should not be messing around with the internal implementation details of library classes. They are liable to change, and your code will then break . as it has done here.
The best solution is to restrict yourself to using the public API methods for KeyStore ; for example, call aliases() and then iterate the resulting Enumeration looking up the entries for each alias.
If you can’t do that, then you will need to modify your code to work with all of the different implementations of KeyStore that you and other users of your code might encounter.
It has been pointed out that this was (probably) a workaround for an old bug in the Java codebase:
If that is that case, then the fault is with person who put the workaround into your codebase without commenting it properly. They should have left a clear explanation for future developers . ideally including the link to the bug report . which they should have found. You would then been able to look up the bug report, see that it had been fixed, and remove the (now unnecessary) nasty workaround to solve your porting problem.