Skip to content

Commit

Permalink
More tests, minor changes and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdxd44 committed Sep 2, 2023
1 parent e078b5c commit 60a9f29
Show file tree
Hide file tree
Showing 7 changed files with 584 additions and 566 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,23 +254,24 @@ public Object readByType(@Nullable Field owner, @Nullable Object holder, Type ty

@SuppressWarnings("unchecked")
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
private Collection<Object> readCollectionByType(Field owner, Type type, Class<?> clazz) {
Type collectionEntryType = GenericUtils.getParameterType(Collection.class, type, 0);
private Map<Object, Object> readMapByType(Field owner, Type type) {
Type mapKeyType = GenericUtils.getParameterType(Map.class, type, 0);
Type mapValueType = GenericUtils.getParameterType(Map.class, type, 1);
if (owner != null) {
CollectionType collectionType = owner.getAnnotation(CollectionType.class);
if (collectionType != null) {
MapType mapType = owner.getAnnotation(MapType.class);
if (mapType != null) {
try {
var constructor = collectionType.value().getDeclaredConstructor();
var constructor = mapType.value().getDeclaredConstructor();
constructor.setAccessible(true);
return this.readCollection(owner, (Collection<Object>) constructor.newInstance(), collectionEntryType);
return this.readMap(owner, (Map<Object, Object>) constructor.newInstance(), mapKeyType, mapValueType);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new SerializableReadException(e);
}
} else if (Collection.class.isAssignableFrom(owner.getType())) {
} else if (Map.class.isAssignableFrom(owner.getType())) {
try {
Constructor<?> constructor = owner.getType().getDeclaredConstructor();
constructor.setAccessible(true);
return this.readCollection(owner, (Collection<Object>) constructor.newInstance(), collectionEntryType);
return this.readMap(owner, (Map<Object, Object>) constructor.newInstance(), mapKeyType, mapValueType);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new SerializableReadException(e);
} catch (NoSuchMethodException e) {
Expand All @@ -279,31 +280,28 @@ private Collection<Object> readCollectionByType(Field owner, Type type, Class<?>
}
}

return Set.class.isAssignableFrom(clazz) ? this.readSet(owner, collectionEntryType)
: Queue.class.isAssignableFrom(clazz) ? this.readDeque(owner, collectionEntryType)
: this.readList(owner, collectionEntryType);
return this.readMap(owner, mapKeyType, mapValueType);
}

@SuppressWarnings("unchecked")
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
private Map<Object, Object> readMapByType(Field owner, Type type) {
Type mapKeyType = GenericUtils.getParameterType(Map.class, type, 0);
Type mapValueType = GenericUtils.getParameterType(Map.class, type, 1);
private Collection<Object> readCollectionByType(Field owner, Type type, Class<?> clazz) {
Type collectionEntryType = GenericUtils.getParameterType(Collection.class, type, 0);
if (owner != null) {
MapType mapType = owner.getAnnotation(MapType.class);
if (mapType != null) {
CollectionType collectionType = owner.getAnnotation(CollectionType.class);
if (collectionType != null) {
try {
var constructor = mapType.value().getDeclaredConstructor();
var constructor = collectionType.value().getDeclaredConstructor();
constructor.setAccessible(true);
return this.readMap(owner, (Map<Object, Object>) constructor.newInstance(), mapKeyType, mapValueType);
return this.readCollection(owner, (Collection<Object>) constructor.newInstance(), collectionEntryType);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new SerializableReadException(e);
}
} else if (Map.class.isAssignableFrom(owner.getType())) {
} else if (Collection.class.isAssignableFrom(owner.getType())) {
try {
Constructor<?> constructor = owner.getType().getDeclaredConstructor();
constructor.setAccessible(true);
return this.readMap(owner, (Map<Object, Object>) constructor.newInstance(), mapKeyType, mapValueType);
return this.readCollection(owner, (Collection<Object>) constructor.newInstance(), collectionEntryType);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new SerializableReadException(e);
} catch (NoSuchMethodException e) {
Expand All @@ -312,7 +310,9 @@ private Map<Object, Object> readMapByType(Field owner, Type type) {
}
}

return this.readMap(owner, mapKeyType, mapValueType);
return Set.class.isAssignableFrom(clazz) ? this.readSet(owner, collectionEntryType)
: Queue.class.isAssignableFrom(clazz) ? this.readDeque(owner, collectionEntryType)
: this.readList(owner, collectionEntryType);
}

public Object readGuessingType() {
Expand Down Expand Up @@ -343,28 +343,6 @@ public <C extends Map<Object, Object>> C readMap(C result, Type keyType, Type va

public abstract <C extends Map<Object, Object>> C readMap(@Nullable Field owner, C result, Type keyType, Type valueType);

public <C extends Collection<Object>> C readCollection(@Nullable Field owner, C result) {
return this.readCollection(owner, result, Object.class);
}

public <C extends Collection<Object>> C readCollection(C result, Type type) {
return this.readCollection(null, result, type);
}

public abstract <C extends Collection<Object>> C readCollection(@Nullable Field owner, C result, Type type);

public List<Object> readList(@Nullable Field owner) {
return this.readList(owner, Object.class);
}

public List<Object> readList(Type type) {
return this.readList(null, type);
}

public List<Object> readList(@Nullable Field owner, Type type) {
return this.readCollection(owner, new ArrayList<>(), type);
}

public Set<Object> readSet(@Nullable Field owner) {
return this.readSet(owner, Object.class);
}
Expand All @@ -389,6 +367,28 @@ public Deque<Object> readDeque(@Nullable Field owner, Type type) {
return this.readCollection(owner, new ArrayDeque<>(), type);
}

public List<Object> readList(@Nullable Field owner) {
return this.readList(owner, Object.class);
}

public List<Object> readList(Type type) {
return this.readList(null, type);
}

public List<Object> readList(@Nullable Field owner, Type type) {
return this.readCollection(owner, new ArrayList<>(), type);
}

public <C extends Collection<Object>> C readCollection(@Nullable Field owner, C result) {
return this.readCollection(owner, result, Object.class);
}

public <C extends Collection<Object>> C readCollection(C result, Type type) {
return this.readCollection(null, result, type);
}

public abstract <C extends Collection<Object>> C readCollection(@Nullable Field owner, C result, Type type);

public String readString() {
return this.readString(null);
}
Expand Down Expand Up @@ -458,8 +458,8 @@ public void skipNode(Class<?> clazz) {
public void skipNode(@Nullable Field owner, Class<?> clazz) {
if (Map.class.isAssignableFrom(clazz)) {
this.skipMap(owner);
} else if (List.class.isAssignableFrom(clazz)) {
this.skipList(owner);
} else if (Collection.class.isAssignableFrom(clazz)) {
this.skipCollection(owner);
} else if (clazz.isEnum()
|| String.class.isAssignableFrom(clazz)
|| Boolean.class.isAssignableFrom(clazz) || boolean.class.isAssignableFrom(clazz)
Expand All @@ -477,11 +477,11 @@ public void skipMap() {

public abstract void skipMap(@Nullable Field owner);

public void skipList() {
this.skipList(null);
public void skipCollection() {
this.skipCollection(null);
}

public abstract void skipList(@Nullable Field owner);
public abstract void skipCollection(@Nullable Field owner);

public void skipString() {
this.skipString(null);
Expand Down
Loading

0 comments on commit 60a9f29

Please sign in to comment.