1717
1818package net .elytrium .serializer .language .reader ;
1919
20+ import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
2021import java .io .BufferedReader ;
2122import java .io .IOException ;
2223import java .lang .reflect .Constructor ;
2930import java .util .Collection ;
3031import java .util .Deque ;
3132import java .util .HashSet ;
33+ import java .util .LinkedHashMap ;
3234import java .util .List ;
3335import java .util .Map ;
3436import java .util .Queue ;
3840import javax .annotation .Nullable ;
3941import net .elytrium .serializer .SerializerConfig ;
4042import net .elytrium .serializer .annotations .CollectionType ;
43+ import net .elytrium .serializer .annotations .MapType ;
4144import net .elytrium .serializer .annotations .Serializer ;
4245import net .elytrium .serializer .custom .ClassSerializer ;
4346import net .elytrium .serializer .exceptions .ReflectionException ;
@@ -203,42 +206,9 @@ public Object readByType(@Nullable Field owner, @Nullable Object holder, Type ty
203206 } else if (type instanceof ParameterizedType parameterizedType ) {
204207 Class <?> clazz = (Class <?>) parameterizedType .getRawType ();
205208 if (Map .class .isAssignableFrom (clazz )) {
206- return this .readMap (owner ,
207- GenericUtils .getParameterType (Map .class , parameterizedType , 0 ),
208- GenericUtils .getParameterType (Map .class , parameterizedType , 1 ));
209+ return this .readMapByType (owner , parameterizedType );
209210 } else if (Collection .class .isAssignableFrom (clazz )) {
210- Type collectionEntryType = GenericUtils .getParameterType (Collection .class , parameterizedType , 0 );
211- if (owner != null ) {
212- CollectionType collectionType = owner .getAnnotation (CollectionType .class );
213- if (collectionType != null ) {
214- try {
215- //noinspection unchecked
216- return this .readCollection (owner ,
217- (Collection <Object >) collectionType .value ().getDeclaredConstructor ().newInstance (),
218- collectionEntryType );
219- } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
220- throw new SerializableReadException (e );
221- }
222- } else {
223- try {
224- //noinspection unchecked
225- return this .readCollection (owner ,
226- (Collection <Object >) owner .getType ().getDeclaredConstructor ().newInstance (),
227- collectionEntryType );
228- } catch (InstantiationException | IllegalAccessException | InvocationTargetException e ) {
229- throw new SerializableReadException (e );
230- } catch (NoSuchMethodException e ) {
231- // Ignoring NoSuchMethod.
232- }
233- }
234- }
235- if (Set .class .isAssignableFrom (clazz )) {
236- return this .readSet (collectionEntryType );
237- } else if (Queue .class .isAssignableFrom (clazz )) {
238- return this .readDeque (collectionEntryType );
239- } else {
240- return this .readList (collectionEntryType );
241- }
211+ return this .readCollectionByType (owner , parameterizedType , clazz );
242212 } else {
243213 return this .readGuessingType (owner );
244214 }
@@ -286,6 +256,72 @@ public Object readByType(@Nullable Field owner, @Nullable Object holder, Type ty
286256 }
287257 }
288258
259+ @ SuppressFBWarnings ("NP_LOAD_OF_KNOWN_NULL_VALUE" )
260+ private Collection <Object > readCollectionByType (Field owner , ParameterizedType parameterizedType , Class <?> clazz ) {
261+ Type collectionEntryType = GenericUtils .getParameterType (Collection .class , parameterizedType , 0 );
262+ if (owner != null ) {
263+ CollectionType collectionType = owner .getAnnotation (CollectionType .class );
264+ if (collectionType != null ) {
265+ try {
266+ //noinspection unchecked
267+ return this .readCollection (owner ,
268+ (Collection <Object >) collectionType .value ().getDeclaredConstructor ().newInstance (),
269+ collectionEntryType );
270+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
271+ throw new SerializableReadException (e );
272+ }
273+ } else {
274+ try {
275+ //noinspection unchecked
276+ return this .readCollection (owner ,
277+ (Collection <Object >) owner .getType ().getDeclaredConstructor ().newInstance (), collectionEntryType );
278+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException e ) {
279+ throw new SerializableReadException (e );
280+ } catch (NoSuchMethodException e ) {
281+ // Ignoring NoSuchMethod.
282+ }
283+ }
284+ }
285+
286+ if (Set .class .isAssignableFrom (clazz )) {
287+ return this .readSet (owner , collectionEntryType );
288+ } else if (Queue .class .isAssignableFrom (clazz )) {
289+ return this .readDeque (owner , collectionEntryType );
290+ } else {
291+ return this .readList (owner , collectionEntryType );
292+ }
293+ }
294+
295+ @ SuppressFBWarnings ("NP_LOAD_OF_KNOWN_NULL_VALUE" )
296+ private Map <Object , Object > readMapByType (Field owner , ParameterizedType parameterizedType ) {
297+ Type mapKeyType = GenericUtils .getParameterType (Map .class , parameterizedType , 0 );
298+ Type mapValueType = GenericUtils .getParameterType (Map .class , parameterizedType , 1 );
299+ if (owner != null ) {
300+ MapType mapType = owner .getAnnotation (MapType .class );
301+ if (mapType != null ) {
302+ try {
303+ //noinspection unchecked
304+ return this .readMap (owner ,
305+ (Map <Object , Object >) mapType .value ().getDeclaredConstructor ().newInstance (), mapKeyType , mapValueType );
306+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
307+ throw new SerializableReadException (e );
308+ }
309+ } else {
310+ try {
311+ //noinspection unchecked
312+ return this .readMap (owner ,
313+ (Map <Object , Object >) owner .getType ().getDeclaredConstructor ().newInstance (), mapKeyType , mapValueType );
314+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException e ) {
315+ throw new SerializableReadException (e );
316+ } catch (NoSuchMethodException e ) {
317+ // Ignoring NoSuchMethod.
318+ }
319+ }
320+ }
321+
322+ return this .readMap (owner , mapKeyType , mapValueType );
323+ }
324+
289325 public Object readGuessingType () {
290326 return this .readGuessingType (null );
291327 }
@@ -297,10 +333,22 @@ public Map<Object, Object> readMap(@Nullable Field owner) {
297333 }
298334
299335 public Map <Object , Object > readMap (Type keyType , Type valueType ) {
300- return this .readMap (null , keyType , valueType );
336+ return this .readMap (( Field ) null , keyType , valueType );
301337 }
302-
303- public abstract Map <Object , Object > readMap (@ Nullable Field owner , Type keyType , Type valueType );
338+
339+ public Map <Object , Object > readMap (@ Nullable Field owner , Type keyType , Type valueType ) {
340+ return this .readMap (owner , new LinkedHashMap <>(), keyType , valueType );
341+ }
342+
343+ public <C extends Map <Object , Object >> C readMap (@ Nullable Field owner , C result ) {
344+ return this .readMap (owner , result , Object .class , Object .class );
345+ }
346+
347+ public <C extends Map <Object , Object >> C readMap (C result , Type keyType , Type valueType ) {
348+ return this .readMap (null , result , keyType , valueType );
349+ }
350+
351+ public abstract <C extends Map <Object , Object >> C readMap (@ Nullable Field owner , C result , Type keyType , Type valueType );
304352
305353 public <C extends Collection <Object >> C readCollection (@ Nullable Field owner , C result ) {
306354 return this .readCollection (owner , result , Object .class );
0 commit comments