Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Convert a class to array with strings
- *
- * @param lines a list where save all lines
- * @param root a class with config fields
- * @param instance a instance of this class
- */
- private void save(List<String> lines, Class root, final Object instance) {
- try {
- for (Field field : root.getDeclaredFields()) {
- if (field.getAnnotation(Ignore.class) != null) { //Skip this field
- continue;
- }
- Comment comment = field.getAnnotation(Comment.class);
- if (comment != null) { //Add comments
- for (String line : comment.value()) {
- lines.add("# " + line);
- }
- }
- Alias alias = field.getAnnotation(Alias.class);
- String name = alias == null ? field.getName() : alias.value();
- setAccessible(field);
- if (field.getAnnotation(Create.class) != null) {
- lines.add(toTableName(name));
- Class<?> current = field.getType();
- Object value = field.get(instance);
- if (value == null) {
- Constructor<?> constructor = current.getDeclaredConstructor();
- constructor.setAccessible(true);
- field.set(instance, value = constructor.newInstance());
- }
- save(lines, current, value); // read a fields from a section(table)
- } else {
- if (field.getAnnotation(AsMap.class) != null) {
- Map<String, ?> map = (Map<String, ?>) field.get(instance);
- for (Entry<String, ?> entry : map.entrySet()) {
- lines.add(entry + " = " + toString(entry.getValue()));
- }
- continue;
- }
- Object value = field.get(instance);
- if (field.getAnnotation(AsBytes.class) != null) {
- value = new String((byte[]) value, StandardCharsets.UTF_8);
- }
- lines.add(name + " = " + toString(value));
- lines.add("");
- }
- }
- } catch (IllegalAccessException | IllegalArgumentException | SecurityException | NoSuchFieldException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
- logger.log(Level.ERROR, "", e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement