gt22

Untitled

Jul 20th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. package com.gt22.pbbot.utils;
  2.  
  3. import com.google.gson.JsonArray;
  4. import com.google.gson.JsonElement;
  5. import com.google.gson.JsonObject;
  6. import com.google.gson.JsonParser;
  7. import org.jooq.lambda.Unchecked;
  8.  
  9. import java.io.FileReader;
  10. import java.lang.annotation.ElementType;
  11. import java.lang.annotation.Retention;
  12. import java.lang.annotation.RetentionPolicy;
  13. import java.lang.annotation.Target;
  14. import java.lang.reflect.Array;
  15. import java.lang.reflect.Method;
  16. import java.math.BigDecimal;
  17. import java.math.BigInteger;
  18. import java.util.Arrays;
  19.  
  20. //===============================================================================================================
  21. //              Я серьёзно советую не читать код который идёт дальше если он ещё работает
  22. //              Чтение этого кода может закончится психической травмой от переизбытка рефлексии
  23. //===============================================================================================================
  24. public class ConfigUtils {
  25.  
  26.     @Retention(RetentionPolicy.RUNTIME)
  27.     @Target(ElementType.FIELD)
  28.     public @interface ManualConfigProperty {}
  29.  
  30.     public static <T> T loadConfig(Class<T> configClass, String configFilename, JsonElement defaultConfig) throws Exception {
  31.         return loadConfig(configClass, new JsonParser().parse(new FileReader(configFilename)).getAsJsonObject(), defaultConfig.getAsJsonObject());
  32.     }
  33.  
  34.     public static <T> T loadConfig(Class<T> configClass, JsonObject config, JsonObject defaultConfig) throws Exception {
  35.         T cfg = configClass.newInstance();
  36.         Arrays.stream(configClass.getFields()).forEach(Unchecked.consumer(f -> {
  37.             if(!f.isAnnotationPresent(ManualConfigProperty.class)) {
  38.                 String name = f.getName();
  39.                 f.setAccessible(true);
  40.                 JsonElement value = config.has(name) ? config.get(name) : defaultConfig.get(name);
  41.                 if (value == null) {
  42.                     f.set(cfg, null);
  43.                 } else {
  44.                     f.set(cfg, convertElementToType(f.getType(), value));
  45.                 }
  46.             }
  47.         }));
  48.         try {
  49.             Method loadManual = configClass.getMethod("loadManual", JsonObject.class);
  50.             loadManual.invoke(cfg, config);
  51.         } catch(NoSuchMethodException e) {
  52.             //No manual load
  53.         }
  54.         return cfg;
  55.     }
  56.  
  57.     private static Object convertElementToType(Class<?> type, JsonElement e) {
  58.         if (type == String.class) {
  59.             return e.getAsString();
  60.         } else if (type == byte.class || type == Byte.class) {
  61.             return e.getAsByte();
  62.         } else if (type == short.class || type == Short.class) {
  63.             return e.getAsShort();
  64.         } else if (type == int.class || type == Integer.class) {
  65.             return e.getAsInt();
  66.         } else if (type == long.class || type == Long.class) {
  67.             return e.getAsLong();
  68.         } else if (type == double.class || type == Double.class) {
  69.             return e.getAsDouble();
  70.         } else if (type == float.class || type == Float.class) {
  71.             return e.getAsFloat();
  72.         } else if (type == char.class || type == Character.class) {
  73.             return e.getAsCharacter();
  74.         } else if (type == Number.class) {
  75.             return e.getAsNumber();
  76.         } else if (type == BigInteger.class) {
  77.             return e.getAsBigInteger();
  78.         } else if (type == BigDecimal.class) {
  79.             return e.getAsBigDecimal();
  80.         } else if (type.isArray()) {
  81.             JsonArray arr = e.getAsJsonArray();
  82.             Class<?> arrType = type.getComponentType();
  83.             Object ret = Array.newInstance(arrType, arr.size());
  84.  
  85.             for(int i = 0; i < arr.size(); i++) {
  86.                 Object o = convertElementToType(arrType, arr.get(i));
  87.                 Array.set(ret, i, o);
  88.             }
  89.             return ret;
  90.         } else {
  91.             throw new IllegalArgumentException("Invalid type " + type.getName());
  92.         }
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment