Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikigal committed Feb 16, 2021
1 parent d0313b5 commit 440e73b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 69 deletions.
9 changes: 7 additions & 2 deletions src/main/java/pl/mikigal/config/BukkitConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public String saveToString() {

List<String> lines = new ArrayList<>();
if (this.configComment != null) {
lines.add(this.configComment);
lines.add("# " + this.configComment);
}

for (String line : yaml.split("\n")) {
Expand Down Expand Up @@ -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);
}
Expand Down
100 changes: 33 additions & 67 deletions src/main/java/pl/mikigal/config/ConfigInvocationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public ConfigInvocationHandler(Class<? extends Config> clazz, BukkitConfiguratio
this.prepareMethods();
}

this.validateConfig();
// Execute all getter for test and fill cache
for (Method method : this.clazz.getDeclaredMethods()) {
this.executeGetter(method);
}
}

@Override
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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));
}

Expand All @@ -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
Expand Down

0 comments on commit 440e73b

Please sign in to comment.