|
17 | 17 | package com.comphenix.protocol.wrappers;
|
18 | 18 |
|
19 | 19 | import java.lang.ref.WeakReference;
|
20 |
| -import java.lang.reflect.Array; |
21 |
| -import java.lang.reflect.Constructor; |
22 |
| -import java.lang.reflect.Field; |
23 |
| -import java.lang.reflect.Method; |
24 |
| -import java.lang.reflect.Modifier; |
| 20 | +import java.lang.reflect.*; |
25 | 21 | import java.util.ArrayList;
|
26 | 22 | import java.util.Collection;
|
27 | 23 | import java.util.HashMap;
|
@@ -271,6 +267,8 @@ public Class<Map<K, V>> getSpecificType() {
|
271 | 267 | };
|
272 | 268 | }
|
273 | 269 |
|
| 270 | + private static Map<Class<?>, Supplier<List<Object>>> LIST_SUPPLIERS = new ConcurrentHashMap<>(); |
| 271 | + |
274 | 272 | /**
|
275 | 273 | * Retrieve an equivalent converter for a list of generic items.
|
276 | 274 | * @param <T> Type
|
@@ -302,11 +300,26 @@ public List<T> getSpecific(Object generic) {
|
302 | 300 | @Override
|
303 | 301 | public Object getGeneric(List<T> specific) {
|
304 | 302 | List<Object> newList;
|
305 |
| - |
306 |
| - try { |
307 |
| - newList = (List<Object>) specific.getClass().newInstance(); |
308 |
| - } catch (ReflectiveOperationException ex) { |
309 |
| - newList = new ArrayList<>(); |
| 303 | + Supplier<List<Object>> supplier = LIST_SUPPLIERS.get(specific.getClass()); |
| 304 | + if (supplier == null) { |
| 305 | + try { |
| 306 | + Constructor<?> ctor = specific.getClass().getConstructor(); |
| 307 | + newList = (List<Object>) ctor.newInstance(); |
| 308 | + supplier = () -> { |
| 309 | + try { |
| 310 | + return (List<Object>) ctor.newInstance(); |
| 311 | + } catch (ReflectiveOperationException ex) { |
| 312 | + throw new RuntimeException(ex); |
| 313 | + } |
| 314 | + }; |
| 315 | + } catch (ReflectiveOperationException ex) { |
| 316 | + supplier = ArrayList::new; |
| 317 | + newList = new ArrayList<>(); |
| 318 | + } |
| 319 | + |
| 320 | + LIST_SUPPLIERS.put(specific.getClass(), supplier); |
| 321 | + } else { |
| 322 | + newList = supplier.get(); |
310 | 323 | }
|
311 | 324 |
|
312 | 325 | // Convert each object
|
@@ -363,20 +376,6 @@ public Class<Pair<A, B>> getSpecificType() {
|
363 | 376 | });
|
364 | 377 | }
|
365 | 378 |
|
366 |
| - /** |
367 |
| - * @deprecated While this solution is not as abhorrent as I had imagined, I still highly recommend switching to the |
368 |
| - * new conversion API. |
369 |
| - */ |
370 |
| - @Deprecated |
371 |
| - public static <T> EquivalentConverter<Set<T>> getSetConverter(final Class<?> genericType, |
372 |
| - final EquivalentConverter<T> itemConverter) { |
373 |
| - if (itemConverter instanceof EnumWrappers.EnumConverter) { |
374 |
| - ((EnumWrappers.EnumConverter) itemConverter).setGenericType(genericType); |
375 |
| - } |
376 |
| - |
377 |
| - return getSetConverter(itemConverter); |
378 |
| - } |
379 |
| - |
380 | 379 | /**
|
381 | 380 | * Retrieve an equivalent converter for a set of generic items.
|
382 | 381 | * @param <T> Element type
|
|
0 commit comments