Well... I'm actually implementing a class to convert any object to another using a class called "Adapter". I'm mapping each pair of types SOURCE -> TARGET using a HashMap: HashMap , HashMap < Class< ? >, Adapter > >. Each type have your adapters. For example: String can be converted to Integer, Double and so on. To get an adapter:
Adapter adapter = map.get(String.class).get(Integer.class);
I will get the adapters dinamically:
Adapter adapter = map.get(< source_object >.getClass()).get(Integer.class);
My problem is that I need put in that HashMap a key to java.util.Collections$UnmodifiableMap. The Request parameter collection from Servlets is a map. But, when I use .getClass() returns java.util.Collections$UnmodifiableMap. Is impossible to put a key java.util.Collections$UnmodifiableMap.class on the adapters HashMap :(
Someone can help me?
Would it help to wrap the UnmodifiableMap in a HashMap?
source_object = new HashMap<Object, Object>(source_object);
Quite inefficient and not very elegant but maybe a temporary workaround?
Thanks to tmmllr for the help! I based on his propose and improve solution! I put the index to source on HashMap adapters using: java.util.Collections.unmodifiableMap(new HashMap<>()).getClass(). Is more efficient way:
adapters.put(java.util.Collections.unmodifiableMap(new HashMap< >()).getClass(),...);
Thus I can pass java.util.Collections$UnmodifiableMap instance and get right adapter! This works \\0/
Million thanks to veer for found second solution:
Class.forName("java.util.Collections$UnmodifiableMap");
I think this is the most practice way!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.