With Java 8, update101 HashMap.entries cannot be assigned to a collection

After upgrading to Java Java 101 update, I get an exception in the following code. It worked great with updating Java 8.

Access to the 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 } 

Casting type is called, exception:

 java.util.HashMap cannot be cast to java.util.Collection 

Any recent changes to the Java 810 update? How to solve it?

+2
java java-8
Jul 21 '16 at 13:11
source share
4 answers

I confirm that this does not work using the following as test code

 import java.util.HashMap; import java.util.Collection; public class HelloWorld { public static void main(String[] args) { HashMap map = new HashMap(); Collection c; c = (Collection) map; } } 

The result is an exception in the "main" thread java.lang.ClassCastException: java.util.HashMap cannot be passed to java.util.Collection on HelloWorld.main (HelloWorld.java:8)

You can override it using values โ€‹โ€‹() method like this

 import java.util.HashMap; import java.util.Collection; public class HelloWorld { public static void main(String[] args) { HashMap map = new HashMap(); Collection c; c = map.values(); } } 

So your code should look like this

 import java.util.HashMap; import java.util.Collection; import java.security.*; import java.lang.reflect.Field; public class HelloWorld { public static void main(String[] args) { try{ 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); entries = ((HashMap) field.get(kss)).values(); }catch(Exception e){ e.printStackTrace(); } } } 
+1
Jul 21 '16 at 13:35
source share
โ€” -

How to solve it?

As stated in the comments, what you do is unpleasant. You do not have to bother with the internal implementation details of library classes. They can change, and your code will then break ... as was done here.

The best solution is to limit yourself to using the public API methods for KeyStore ; for example, call aliases() , and then move the resulting Enumeration<String> search for entries for each alias.

If you cannot do this, you will need to change your code to work with all of the various KeyStore implementations that you and other users of your code may encounter.

+7
Jul 21 '16 at 13:52
source share

Before Java 8u101, you had to access this private field to work around the http://bugs.openjdk.java.net/browse/JDK-6483657 error. This bug was fixed in Java 8u101, so you can revert to using the official keystore API.

+1
Dec 13 '16 at 9:08
source share

The next change will make it work with 8u101, but it will not remove the fact that you are KeyStore with the internal KeyStore classes that you really don't need to mess with.

The problem is that some object changed from Collection to HashMap in Java 8u101, and HashMap not Collection , so trying to apply it to Collection raises a ClassCastException .

 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); entries = (HashMap)field.get(kss); for (Map.Entry entry : entries) { Object key = entry.getKey(); Object value = entry.getValue(); System.out.println(key + " = " + value); } 
0
Jul 21 '16 at 14:21
source share



All Articles