Skip to content

Commit

Permalink
Auto-saving static fields & saving null values
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubix327 committed Nov 3, 2022
1 parent 6994e96 commit 4624607
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
32 changes: 4 additions & 28 deletions src/main/java/org/mineacademy/fo/SerializeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,10 @@ public static List<Field> getFieldsToAutoSerialize(Class<?> classOf){
if (hasAnnotation){
isEnabled = field.getAnnotation(AutoSerialize.class).value();
}
else if (Modifier.isStatic(field.getModifiers())) {
continue;
}

if (Modifier.isStatic(field.getModifiers())) continue;
if (ReflectionUtil.isAnnotationAttached(isAboveClass, hasAnnotation, isEnabled)) fields.add(field);
}

Expand Down Expand Up @@ -743,7 +745,7 @@ public static <T> T autoDeserialize(Class<T> classOf, SerializedMap map){
try {
instance = (T) constructor.newInstance();

for (Field field : getFieldsToAutoDeserialize(classOf)) {
for (Field field : getFieldsToAutoSerialize(classOf)) {
field.setAccessible(true);
String name = getFormattedFieldName(classOf, field);
Object value = map.get(name, field.getType());
Expand All @@ -758,32 +760,6 @@ public static <T> T autoDeserialize(Class<T> classOf, SerializedMap map){
return instance;
}

public static List<Field> getFieldsToAutoDeserialize(Class<?> classOf){
List<Field> fields = new ArrayList<>();
boolean isAboveClass = false;

// Do nothing if AutoSerialize is disabled for the whole class
if (classOf.isAnnotationPresent(AutoSerialize.class)){
isAboveClass = true;
if (!classOf.getAnnotation(AutoSerialize.class).value()){
return new ArrayList<>();
}
}

for (Field field : classOf.getDeclaredFields()){
boolean hasAnnotation = field.isAnnotationPresent(AutoSerialize.class);
boolean isEnabled = false;
if (hasAnnotation){
isEnabled = field.getAnnotation(AutoSerialize.class).value();
}

if (Modifier.isStatic(field.getModifiers())) continue;
if (ReflectionUtil.isAnnotationAttached(isAboveClass, hasAnnotation, isEnabled)) fields.add(field);
}

return fields;
}

/**
* Get the name under which the value will be located in SerializedMap.
* Based on user-defined AutoSerialize.format() and is lower_underscore by default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* <b>On class</b>:
* <ul>
* <li>Saves and loads all non-static class fields</li>
* <li>Saves and loads those static fields that have separately specified this annotation above themselves</li>
* <li>But skips fields that have disabled this feature by <i>@AutoConfig(false)</i></li>
* </ul>
* When using on class, if you want to prevent one specific field from auto-loading and auto-saving,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* <b>On class</b>:
* <ul>
* <li>Serializes and deserializes all non-static class fields</li>
* <li>Serializes and deserializes those static fields that have separately specified this annotation above themselves</li>
* <li>But skips fields that have disabled this feature by <i>@AutoSerialize(false)</i></li>
* </ul>
* When using on class, if you want to prevent one specific field from auto-serializing and auto-deserializing,
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/org/mineacademy/fo/settings/FileConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ private void loadFields(){

try {
Object value = getBasedOnClass(getFormattedFieldName(field), field);
if (value != null){
if (value != null || loadNullValues()){
field.set(this, value);
}
} catch (IllegalAccessException e) {
Expand Down Expand Up @@ -1410,8 +1410,10 @@ private List<Field> getFieldsToAutoLoad() {
AutoConfig ann = field.getAnnotation(AutoConfig.class);
isEnabled = ann.value() && ann.autoLoad();
}
else if (Modifier.isStatic(field.getModifiers())) {
continue;
}

if (Modifier.isStatic(field.getModifiers())) continue;
if (ReflectionUtil.isAnnotationAttached(isAboveClass, hasAnnotation, isEnabled)) fieldsToLoad.add(field);
}

Expand All @@ -1427,7 +1429,7 @@ private void saveFields() {

try {
Object value = field.get(this);
if (value != null){
if (value != null || saveNullValues()){
set(getFormattedFieldName(field), value);
}
} catch (IllegalAccessException e) {
Expand Down Expand Up @@ -1459,8 +1461,10 @@ private List<Field> getFieldsToAutoSave() {
AutoConfig ann = field.getAnnotation(AutoConfig.class);
isEnabled = ann.value() && ann.autoSave();
}
else if (Modifier.isStatic(field.getModifiers())) {
continue;
}

if (Modifier.isStatic(field.getModifiers())) continue;
if (ReflectionUtil.isAnnotationAttached(isAboveClass, hasAnnotation, isEnabled)) fieldsToSave.add(field);
}

Expand Down Expand Up @@ -1559,6 +1563,22 @@ private String getFormattedFieldName(Field field) {
return name;
}

/**
* Tells @AutoConfig whether save null values to the file or not.<br>
* Only works with @AutoConfig saving (not {@link #onSave()} method).
*/
protected boolean saveNullValues(){
return true;
}

/**
* Tells @AutoConfig whether load null values from the file or not.<br>
* Only works with @AutoConfig loading (not {@link #onLoad()} method).
*/
protected boolean loadNullValues(){
return true;
}

/**
* Return if the file can be saved when calling {@link #save()}
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/mineacademy/fo/settings/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ public static void loadAll(String folder, Class<? extends YamlConfig> clazz){
t = constructor.newInstance();
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e){
throw new RuntimeException("Your class " + clazz.getName() + " must contain a no-arguments constructor.");
throw new RuntimeException("Your class " + clazz.getName() + " must contain a no-arguments constructor " +
"because is annotated with @AutoConfig.");
}
t.loadConfiguration(null, folder + "/" + file.getName());
}
Expand Down

0 comments on commit 4624607

Please sign in to comment.