|
| 1 | +package pl.mikigal.config.serializer.universal; |
| 2 | + |
| 3 | +import pl.mikigal.config.BukkitConfiguration; |
| 4 | +import pl.mikigal.config.serializer.Serializer; |
| 5 | + |
| 6 | +import java.io.Serializable; |
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.lang.reflect.Modifier; |
| 9 | + |
| 10 | +/** |
| 11 | + * Helper built-in serializer for custom objects which implemenet Serializable interface. |
| 12 | + * It uses reflections to serialize all fields from given Object, which are not transient and static. |
| 13 | + * Class must have default constructor (no-args). |
| 14 | + * @see Serializer |
| 15 | + * @see Serializable |
| 16 | + * @since 1.1.7 |
| 17 | + * @author Mikołaj Gałązka |
| 18 | + */ |
| 19 | +public class UniversalObjectSerializer extends Serializer<Serializable> { |
| 20 | + |
| 21 | + @Override |
| 22 | + protected void saveObject(String path, Serializable object, BukkitConfiguration configuration) { |
| 23 | + this.validateDefaultConstructor(object); |
| 24 | + |
| 25 | + try { |
| 26 | + for (Field field : object.getClass().getDeclaredFields()) { |
| 27 | + if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + field.setAccessible(true); |
| 32 | + Object value = field.get(object); |
| 33 | + |
| 34 | + try { |
| 35 | + configuration.set(path + "." + configuration.getNameStyle().format(field.getName()), value); |
| 36 | + } catch (Exception e) { |
| 37 | + throw new RuntimeException("An error occurred while serializing field '" + field.getName() + "' from class '" + object.getClass().getName() + "'", e); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + configuration.set(path + "." + configuration.getNameStyle().format("type"), object.getClass().getName()); |
| 42 | + } catch (IllegalAccessException e) { |
| 43 | + throw new RuntimeException("An error occurred while serializing class '" + object.getClass().getName() + "'", e); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Serializable deserialize(String path, BukkitConfiguration configuration) { |
| 49 | + String classPath = configuration.getString(path + "." + configuration.getNameStyle().format("type")); |
| 50 | + Class<?> clazz; |
| 51 | + |
| 52 | + try { |
| 53 | + clazz = Class.forName(classPath); |
| 54 | + } catch (ClassNotFoundException e) { |
| 55 | + throw new RuntimeException("An error occurred while deserializing class '" + classPath + "'", e); |
| 56 | + } |
| 57 | + |
| 58 | + if (!Serializable.class.isAssignableFrom(clazz)) { |
| 59 | + throw new RuntimeException("Class " + classPath + " does not implements Serializable"); |
| 60 | + } |
| 61 | + |
| 62 | + Serializable instance; |
| 63 | + try { |
| 64 | + instance = (Serializable) clazz.newInstance(); |
| 65 | + } catch (InstantiationException | IllegalAccessException e) { |
| 66 | + throw new RuntimeException("Could not create instance of class (" + classPath + ") with default constructor", e); |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + for (Field field : clazz.getDeclaredFields()) { |
| 71 | + if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + field.setAccessible(true); |
| 76 | + field.set(instance, configuration.get(path + "." + configuration.getNameStyle().format(field.getName()))); |
| 77 | + } |
| 78 | + } catch (IllegalAccessException e) { |
| 79 | + throw new RuntimeException("Could not deserialize " + classPath, e); |
| 80 | + } |
| 81 | + |
| 82 | + return instance; |
| 83 | + } |
| 84 | + |
| 85 | + private void validateDefaultConstructor(Object object) { |
| 86 | + try { |
| 87 | + object.getClass().getConstructor(); |
| 88 | + } catch (NoSuchMethodException e) { |
| 89 | + throw new IllegalArgumentException("Class " + object.getClass().getName() + " does not have a default constructor"); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments