Add record getters
This commit is contained in:
parent
51eaeaae33
commit
ff4c803536
@ -27,8 +27,14 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class MoshiPolymorphic<OBJ> {
|
||||
|
||||
public enum GetterStyle {
|
||||
FIELDS,
|
||||
RECORDS_GETTERS,
|
||||
STANDARD_GETTERS
|
||||
}
|
||||
|
||||
private final boolean instantiateUsingStaticOf;
|
||||
private final boolean useGetters;
|
||||
private final GetterStyle getterStyle;
|
||||
private boolean initialized = false;
|
||||
private Moshi abstractMoshi;
|
||||
private final Map<Type, JsonAdapter<OBJ>> abstractClassesSerializers = new ConcurrentHashMap<>();
|
||||
@ -39,12 +45,12 @@ public abstract class MoshiPolymorphic<OBJ> {
|
||||
private final Map<String, JsonAdapter<OBJ>> customAdapters = new ConcurrentHashMap<>();
|
||||
|
||||
public MoshiPolymorphic() {
|
||||
this(false, false);
|
||||
this(false, GetterStyle.FIELDS);
|
||||
}
|
||||
|
||||
public MoshiPolymorphic(boolean instantiateUsingStaticOf, boolean useGetters) {
|
||||
public MoshiPolymorphic(boolean instantiateUsingStaticOf, GetterStyle getterStyle) {
|
||||
this.instantiateUsingStaticOf = instantiateUsingStaticOf;
|
||||
this.useGetters = useGetters;
|
||||
this.getterStyle = getterStyle;
|
||||
}
|
||||
|
||||
private synchronized void initialize() {
|
||||
@ -201,7 +207,8 @@ public abstract class MoshiPolymorphic<OBJ> {
|
||||
for (Field declaredField : this.declaredFields) {
|
||||
fieldNames[i] = declaredField.getName();
|
||||
|
||||
if (useGetters) {
|
||||
switch (getterStyle) {
|
||||
case STANDARD_GETTERS:
|
||||
var getterMethod = declaredField
|
||||
.getDeclaringClass()
|
||||
.getMethod("get" + StringUtils.capitalize(declaredField.getName()));
|
||||
@ -212,7 +219,20 @@ public abstract class MoshiPolymorphic<OBJ> {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
break;
|
||||
case RECORDS_GETTERS:
|
||||
var getterMethod2 = declaredField
|
||||
.getDeclaringClass()
|
||||
.getMethod(declaredField.getName());
|
||||
fieldGetters[i] = obj -> {
|
||||
try {
|
||||
return getterMethod2.invoke(obj);
|
||||
} catch (InvocationTargetException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
break;
|
||||
case FIELDS:
|
||||
fieldGetters[i] = t -> {
|
||||
try {
|
||||
return declaredField.get(t);
|
||||
@ -220,6 +240,7 @@ public abstract class MoshiPolymorphic<OBJ> {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
|
Loading…
x
Reference in New Issue
Block a user