Skip to content

Commit 440e73b

Browse files
committed
Refactoring
1 parent d0313b5 commit 440e73b

File tree

2 files changed

+40
-69
lines changed

2 files changed

+40
-69
lines changed

src/main/java/pl/mikigal/config/BukkitConfiguration.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public String saveToString() {
113113

114114
List<String> lines = new ArrayList<>();
115115
if (this.configComment != null) {
116-
lines.add(this.configComment);
116+
lines.add("# " + this.configComment);
117117
}
118118

119119
for (String line : yaml.split("\n")) {
@@ -147,9 +147,14 @@ public String saveToString() {
147147
* Loads data from config file
148148
*/
149149
public void load() {
150+
this.load(this.file);
151+
}
152+
153+
@Override
154+
public void load(File file) {
150155
try {
151156
this.cache.clear();
152-
super.load(this.file);
157+
super.load(file);
153158
} catch (IOException | InvalidConfigurationException e) {
154159
throw new InvalidConfigException("Could not load config file (name: " + this.file.getName() + ")", e);
155160
}

src/main/java/pl/mikigal/config/ConfigInvocationHandler.java

+33-67
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public ConfigInvocationHandler(Class<? extends Config> clazz, BukkitConfiguratio
4343
this.prepareMethods();
4444
}
4545

46-
this.validateConfig();
46+
// Execute all getter for test and fill cache
47+
for (Method method : this.clazz.getDeclaredMethods()) {
48+
this.executeGetter(method);
49+
}
4750
}
4851

4952
@Override
@@ -81,59 +84,46 @@ private Object executeGetter(Method method) {
8184
return null;
8285
}
8386

87+
if (method.getReturnType().isArray()) {
88+
throw new InvalidConfigException("Arrays are not supported, use Collection instead");
89+
}
90+
8491
String path = this.getConfigPath(method);
85-
if (method.getReturnType().equals(String.class) && this.automaticColorStrings) {
86-
String cache = (String) this.configuration.get(path);
87-
if (cache == null) {
92+
Object value = this.configuration.get(path);
93+
94+
if (value == null) {
95+
if (!method.isAnnotationPresent(ConfigOptional.class)) {
8896
throw new InvalidConfigFileException("Variable in config (path: " + path + ") is required, but is not set");
8997
}
9098

99+
return null;
100+
}
101+
102+
if (method.getReturnType().equals(String.class) && this.automaticColorStrings) {
103+
String asString = (String) value;
91104
if (!this.configuration.getCache().containsKey(path)) {
92-
cache = ConversionUtils.fixColors(cache);
93-
this.configuration.addToCache(path, cache);
105+
asString = ConversionUtils.fixColors(asString);
106+
this.configuration.addToCache(path, asString);
94107
}
95108

96-
return cache;
109+
return asString;
97110
}
98111

99112
if (TypeUtils.isSimpleType(method)) {
100-
Object value = this.configuration.get(path);
101-
if (value == null) {
102-
if (!method.isAnnotationPresent(ConfigOptional.class)) {
103-
throw new InvalidConfigFileException("Variable in config (path: " + path + ") is required, but is not set");
104-
}
105-
106-
return null;
107-
}
108-
109-
110-
if (!method.getReturnType().isInstance(value) && !value.getClass().equals(TypeUtils.getWrapper(method.getReturnType()))) {
111-
throw new InvalidConfigException("Method " + method.getName() + " does not return type same as variable in config (path: " + path + "; " + value.getClass() + ")");
112-
}
113-
114113
return value;
115114
}
116115

117-
if (method.getReturnType().isArray()) {
118-
throw new InvalidConfigException("Arrays are not supported, use Collection instead");
119-
}
120-
121116
Serializer<?> serializer = Serializers.of(method.getReturnType());
122117
if (serializer == null) {
123118
throw new MissingSerializerException(method.getReturnType());
124119
}
125120

126-
Object cache = this.configuration.get(path);
127-
if (cache == null) {
128-
throw new InvalidConfigFileException("Variable in config (path: " + path + ") is required, but is not set");
129-
}
130-
131-
if (!serializer.getSerializerType().equals(cache.getClass())) {
132-
cache = serializer.deserialize(path, this.configuration);
133-
this.configuration.addToCache(path, cache);
121+
if (!serializer.getSerializerType().equals(value.getClass())) {
122+
value = serializer.deserialize(path, this.configuration);
123+
this.configuration.addToCache(path, value);
134124
}
135125

136-
return cache;
126+
return value;
137127
}
138128

139129
/**
@@ -160,7 +150,7 @@ private void executeSetter(Method method, Object[] args) {
160150
}
161151

162152
/**
163-
* Prepare paths of fields
153+
* Validate methods, prepare paths of fields
164154
*/
165155
private void prepareMethods() {
166156
// Process getters
@@ -208,10 +198,7 @@ private void prepareMethods() {
208198
throw new InvalidConfigException("Setter method " + name + " has ConfigOptional annotation");
209199
}
210200

211-
String getter = name.replace("set", "get");
212-
if (!this.configPaths.containsKey(getter)) {
213-
throw new InvalidConfigException("Setter method " + name + " has not getter");
214-
}
201+
String getter = name.replaceFirst("set", "get");
215202

216203
try {
217204
if (!this.clazz.getDeclaredMethod(getter).getReturnType().equals(method.getParameters()[0].getType())) {
@@ -243,32 +230,20 @@ private boolean updateConfigFile() {
243230
continue;
244231
}
245232

246-
if (method.getReturnType().isArray()) {
247-
throw new InvalidConfigException("Arrays are not supported, use Collection instead");
248-
}
249-
250233
Object defaultValue = ReflectionUtils.getDefaultValue(proxy, method);
251-
if (defaultValue == null) {
252-
if (!method.isAnnotationPresent(ConfigOptional.class)) {
253-
throw new InvalidConfigException("Method " + method.getName() + " is not optional, but it's default value is null");
254-
}
255-
256-
continue;
234+
if (defaultValue == null && !method.isAnnotationPresent(ConfigOptional.class)) {
235+
throw new InvalidConfigException("Method " + method.getName() + " is not optional, but it's default value is null");
257236
}
258237

259-
modified = true;
260-
if (defaultValue instanceof Collection) {
261-
if (((Collection<?>) defaultValue).size() == 0) {
262-
throw new InvalidConfigException("Could not use empty Collection as default value, method: " + name);
263-
}
238+
if (defaultValue instanceof Collection && ((Collection<?>) defaultValue).size() == 0) {
239+
throw new InvalidConfigException("Could not use empty Collection as default value, method: " + name);
264240
}
265241

266-
if (defaultValue instanceof Map) {
267-
if (((Map<?, ?>) defaultValue).size() == 0) {
268-
throw new InvalidConfigException("Could not use empty Map as default value, method: " + name);
269-
}
242+
if (defaultValue instanceof Map && ((Map<?, ?>) defaultValue).size() == 0) {
243+
throw new InvalidConfigException("Could not use empty Map as default value, method: " + name);
270244
}
271245

246+
modified = true;
272247
this.configuration.set(this.getConfigPath(method), defaultValue, method.getAnnotation(Comment.class));
273248
}
274249

@@ -279,15 +254,6 @@ private boolean updateConfigFile() {
279254
return modified;
280255
}
281256

282-
/**
283-
* Get value from config for every method for the first time, for validation and prepare cache
284-
*/
285-
private void validateConfig() {
286-
for (Method method : this.clazz.getDeclaredMethods()) {
287-
this.executeGetter(method);
288-
}
289-
}
290-
291257
/**
292258
* Get path of method from cache
293259
* @param method instance of method

0 commit comments

Comments
 (0)