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 }
Type casting, exception is thrown:
java.util.HashMap cannot be cast to java.util.Collection
Any recent changes in Java 8 update 101? How to solve it?
How to solve it?
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<String>
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.
Before Java 8u101, you needed to access this private field to work around bug http://bugs.openjdk.java.net/browse/JDK-6483657. This bug has been fixed in Java 8u101, so you can go back to using the official keystore API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With