This commit is contained in:
Andrea Cavalli 2022-02-21 00:59:11 +01:00
parent d1b4fe6a96
commit 255829fe2f

View File

@ -11,16 +11,17 @@ import dev.zacsweers.moshix.records.RecordsJsonAdapterFactory;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -191,19 +192,25 @@ public abstract class MoshiPolymorphic<OBJ> {
private final String adapterName;
private final Options names;
private final Class<?> declaredClass;
private final Field[] declaredFields;
private final List<Field> declaredFields;
private final Function<T, Object>[] fieldGetters;
private NormalValueAdapter(String adapterName, Class<?> declaredClass) {
try {
this.adapterName = adapterName;
this.declaredClass = declaredClass;
var declaredFields = new ArrayList<>(List.of(declaredClass.getDeclaredFields()));
declaredFields.removeIf(field -> shouldIgnoreField(field.getName()));
this.declaredFields = declaredFields.toArray(Field[]::new);
String[] fieldNames = new String[this.declaredFields.length];
this.declaredFields = Arrays
.stream(declaredClass.getDeclaredFields())
.filter(field -> {
var modifiers = field.getModifiers();
return !Modifier.isStatic(modifiers)
&& !Modifier.isTransient(modifiers)
&& !shouldIgnoreField(field.getName());
})
.collect(Collectors.toList());
String[] fieldNames = new String[this.declaredFields.size()];
//noinspection unchecked
this.fieldGetters = new Function[this.declaredFields.length];
this.fieldGetters = new Function[this.declaredFields.size()];
int i = 0;
for (Field declaredField : this.declaredFields) {
fieldNames[i] = declaredField.getName();
@ -258,7 +265,7 @@ public abstract class MoshiPolymorphic<OBJ> {
Object instance;
Object[] fields;
if (instantiateUsingStaticOf) {
fields = new Object[declaredFields.length];
fields = new Object[declaredFields.size()];
instance = null;
} else {
fields = null;
@ -268,12 +275,12 @@ public abstract class MoshiPolymorphic<OBJ> {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
var nameId = jsonReader.selectName(names);
if (nameId >= 0 && nameId < this.declaredFields.length) {
var fieldValue = abstractMoshi.adapter(declaredFields[nameId].getGenericType()).fromJson(jsonReader);
if (nameId >= 0 && nameId < this.declaredFields.size()) {
var fieldValue = abstractMoshi.adapter(declaredFields.get(nameId).getGenericType()).fromJson(jsonReader);
if (instantiateUsingStaticOf) {
fields[nameId] = fieldValue;
} else {
declaredFields[nameId].set(instance, fieldValue);
declaredFields.get(nameId).set(instance, fieldValue);
}
} else {
String keyName = jsonReader.nextName();
@ -283,9 +290,9 @@ public abstract class MoshiPolymorphic<OBJ> {
jsonReader.endObject();
if (instantiateUsingStaticOf) {
Class[] params = new Class[declaredFields.length];
for (int i = 0; i < declaredFields.length; i++) {
params[i] = declaredFields[i].getType();
Class[] params = new Class[declaredFields.size()];
for (int i = 0; i < declaredFields.size(); i++) {
params[i] = declaredFields.get(i).getType();
}
instance = declaredClass.getMethod("of", params).invoke(null, fields);
}