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 abstract class MoshiPolymorphic<OBJ> {
|
||||||
|
|
||||||
|
public enum GetterStyle {
|
||||||
|
FIELDS,
|
||||||
|
RECORDS_GETTERS,
|
||||||
|
STANDARD_GETTERS
|
||||||
|
}
|
||||||
|
|
||||||
private final boolean instantiateUsingStaticOf;
|
private final boolean instantiateUsingStaticOf;
|
||||||
private final boolean useGetters;
|
private final GetterStyle getterStyle;
|
||||||
private boolean initialized = false;
|
private boolean initialized = false;
|
||||||
private Moshi abstractMoshi;
|
private Moshi abstractMoshi;
|
||||||
private final Map<Type, JsonAdapter<OBJ>> abstractClassesSerializers = new ConcurrentHashMap<>();
|
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<>();
|
private final Map<String, JsonAdapter<OBJ>> customAdapters = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public MoshiPolymorphic() {
|
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.instantiateUsingStaticOf = instantiateUsingStaticOf;
|
||||||
this.useGetters = useGetters;
|
this.getterStyle = getterStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void initialize() {
|
private synchronized void initialize() {
|
||||||
@ -201,25 +207,40 @@ public abstract class MoshiPolymorphic<OBJ> {
|
|||||||
for (Field declaredField : this.declaredFields) {
|
for (Field declaredField : this.declaredFields) {
|
||||||
fieldNames[i] = declaredField.getName();
|
fieldNames[i] = declaredField.getName();
|
||||||
|
|
||||||
if (useGetters) {
|
switch (getterStyle) {
|
||||||
var getterMethod = declaredField
|
case STANDARD_GETTERS:
|
||||||
.getDeclaringClass()
|
var getterMethod = declaredField
|
||||||
.getMethod("get" + StringUtils.capitalize(declaredField.getName()));
|
.getDeclaringClass()
|
||||||
fieldGetters[i] = obj -> {
|
.getMethod("get" + StringUtils.capitalize(declaredField.getName()));
|
||||||
try {
|
fieldGetters[i] = obj -> {
|
||||||
return getterMethod.invoke(obj);
|
try {
|
||||||
} catch (InvocationTargetException | IllegalAccessException e) {
|
return getterMethod.invoke(obj);
|
||||||
throw new RuntimeException(e);
|
} catch (InvocationTargetException | IllegalAccessException e) {
|
||||||
}
|
throw new RuntimeException(e);
|
||||||
};
|
}
|
||||||
} else {
|
};
|
||||||
fieldGetters[i] = t -> {
|
break;
|
||||||
try {
|
case RECORDS_GETTERS:
|
||||||
return declaredField.get(t);
|
var getterMethod2 = declaredField
|
||||||
} catch (IllegalAccessException e) {
|
.getDeclaringClass()
|
||||||
throw new RuntimeException(e);
|
.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);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user