diff --git a/src/main/java/pl/mikigal/config/BukkitConfiguration.java b/src/main/java/pl/mikigal/config/BukkitConfiguration.java index a443040..18f1c86 100644 --- a/src/main/java/pl/mikigal/config/BukkitConfiguration.java +++ b/src/main/java/pl/mikigal/config/BukkitConfiguration.java @@ -113,7 +113,7 @@ public String saveToString() { List lines = new ArrayList<>(); if (this.configComment != null) { - lines.add(this.configComment); + lines.add("# " + this.configComment); } for (String line : yaml.split("\n")) { @@ -147,9 +147,14 @@ public String saveToString() { * Loads data from config file */ public void load() { + this.load(this.file); + } + + @Override + public void load(File file) { try { this.cache.clear(); - super.load(this.file); + super.load(file); } catch (IOException | InvalidConfigurationException e) { throw new InvalidConfigException("Could not load config file (name: " + this.file.getName() + ")", e); } diff --git a/src/main/java/pl/mikigal/config/ConfigInvocationHandler.java b/src/main/java/pl/mikigal/config/ConfigInvocationHandler.java index f9d9fef..cb6779c 100644 --- a/src/main/java/pl/mikigal/config/ConfigInvocationHandler.java +++ b/src/main/java/pl/mikigal/config/ConfigInvocationHandler.java @@ -43,7 +43,10 @@ public ConfigInvocationHandler(Class clazz, BukkitConfiguratio this.prepareMethods(); } - this.validateConfig(); + // Execute all getter for test and fill cache + for (Method method : this.clazz.getDeclaredMethods()) { + this.executeGetter(method); + } } @Override @@ -81,59 +84,46 @@ private Object executeGetter(Method method) { return null; } + if (method.getReturnType().isArray()) { + throw new InvalidConfigException("Arrays are not supported, use Collection instead"); + } + String path = this.getConfigPath(method); - if (method.getReturnType().equals(String.class) && this.automaticColorStrings) { - String cache = (String) this.configuration.get(path); - if (cache == null) { + Object value = this.configuration.get(path); + + if (value == null) { + if (!method.isAnnotationPresent(ConfigOptional.class)) { throw new InvalidConfigFileException("Variable in config (path: " + path + ") is required, but is not set"); } + return null; + } + + if (method.getReturnType().equals(String.class) && this.automaticColorStrings) { + String asString = (String) value; if (!this.configuration.getCache().containsKey(path)) { - cache = ConversionUtils.fixColors(cache); - this.configuration.addToCache(path, cache); + asString = ConversionUtils.fixColors(asString); + this.configuration.addToCache(path, asString); } - return cache; + return asString; } if (TypeUtils.isSimpleType(method)) { - Object value = this.configuration.get(path); - if (value == null) { - if (!method.isAnnotationPresent(ConfigOptional.class)) { - throw new InvalidConfigFileException("Variable in config (path: " + path + ") is required, but is not set"); - } - - return null; - } - - - if (!method.getReturnType().isInstance(value) && !value.getClass().equals(TypeUtils.getWrapper(method.getReturnType()))) { - throw new InvalidConfigException("Method " + method.getName() + " does not return type same as variable in config (path: " + path + "; " + value.getClass() + ")"); - } - return value; } - if (method.getReturnType().isArray()) { - throw new InvalidConfigException("Arrays are not supported, use Collection instead"); - } - Serializer serializer = Serializers.of(method.getReturnType()); if (serializer == null) { throw new MissingSerializerException(method.getReturnType()); } - Object cache = this.configuration.get(path); - if (cache == null) { - throw new InvalidConfigFileException("Variable in config (path: " + path + ") is required, but is not set"); - } - - if (!serializer.getSerializerType().equals(cache.getClass())) { - cache = serializer.deserialize(path, this.configuration); - this.configuration.addToCache(path, cache); + if (!serializer.getSerializerType().equals(value.getClass())) { + value = serializer.deserialize(path, this.configuration); + this.configuration.addToCache(path, value); } - return cache; + return value; } /** @@ -160,7 +150,7 @@ private void executeSetter(Method method, Object[] args) { } /** - * Prepare paths of fields + * Validate methods, prepare paths of fields */ private void prepareMethods() { // Process getters @@ -208,10 +198,7 @@ private void prepareMethods() { throw new InvalidConfigException("Setter method " + name + " has ConfigOptional annotation"); } - String getter = name.replace("set", "get"); - if (!this.configPaths.containsKey(getter)) { - throw new InvalidConfigException("Setter method " + name + " has not getter"); - } + String getter = name.replaceFirst("set", "get"); try { if (!this.clazz.getDeclaredMethod(getter).getReturnType().equals(method.getParameters()[0].getType())) { @@ -243,32 +230,20 @@ private boolean updateConfigFile() { continue; } - if (method.getReturnType().isArray()) { - throw new InvalidConfigException("Arrays are not supported, use Collection instead"); - } - Object defaultValue = ReflectionUtils.getDefaultValue(proxy, method); - if (defaultValue == null) { - if (!method.isAnnotationPresent(ConfigOptional.class)) { - throw new InvalidConfigException("Method " + method.getName() + " is not optional, but it's default value is null"); - } - - continue; + if (defaultValue == null && !method.isAnnotationPresent(ConfigOptional.class)) { + throw new InvalidConfigException("Method " + method.getName() + " is not optional, but it's default value is null"); } - modified = true; - if (defaultValue instanceof Collection) { - if (((Collection) defaultValue).size() == 0) { - throw new InvalidConfigException("Could not use empty Collection as default value, method: " + name); - } + if (defaultValue instanceof Collection && ((Collection) defaultValue).size() == 0) { + throw new InvalidConfigException("Could not use empty Collection as default value, method: " + name); } - if (defaultValue instanceof Map) { - if (((Map) defaultValue).size() == 0) { - throw new InvalidConfigException("Could not use empty Map as default value, method: " + name); - } + if (defaultValue instanceof Map && ((Map) defaultValue).size() == 0) { + throw new InvalidConfigException("Could not use empty Map as default value, method: " + name); } + modified = true; this.configuration.set(this.getConfigPath(method), defaultValue, method.getAnnotation(Comment.class)); } @@ -279,15 +254,6 @@ private boolean updateConfigFile() { return modified; } - /** - * Get value from config for every method for the first time, for validation and prepare cache - */ - private void validateConfig() { - for (Method method : this.clazz.getDeclaredMethods()) { - this.executeGetter(method); - } - } - /** * Get path of method from cache * @param method instance of method