Skip to content

Commit 9c00c52

Browse files
committed
Added validation for array of primitives
1 parent 81335cb commit 9c00c52

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

README.MD

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ maven {
2121
url = 'https://repo.mikigal.pl/releases'
2222
}
2323
24-
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.1.8'
24+
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.1.9'
2525
```
2626

2727
#### Maven

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'pl.mikigal'
7-
version '1.1.8'
7+
version '1.1.9'
88

99
publishing {
1010
repositories {

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

+8
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ private void prepareMethods() {
163163
}
164164
}
165165

166+
if (TypeUtils.isPrimitiveArray(method.getReturnType())) {
167+
throw new InvalidConfigException("Arrays with primitives are not supported");
168+
}
169+
166170
ConfigPath configPath = method.getAnnotation(ConfigPath.class);
167171
this.configPaths.put(name, configPath == null ? configuration.getNameStyle().format(name) : configPath.value());
168172
}
@@ -182,6 +186,10 @@ private void prepareMethods() {
182186
throw new InvalidConfigException("Setter method " + name + " is not void type");
183187
}
184188

189+
if (TypeUtils.isPrimitiveArray(method.getReturnType())) {
190+
throw new InvalidConfigException("Arrays with primitives are not supported");
191+
}
192+
185193
if (method.getParameterCount() != 1) {
186194
throw new InvalidConfigException("Setter method " + name + " has not 1 parameter");
187195
}

src/main/java/pl/mikigal/config/serializer/universal/UniversalObjectSerializer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Serializable deserialize(String path, BukkitConfiguration configuration)
6969
throw new RuntimeException("An error occurred while deserializing class '" + classPath + "'", e);
7070
}
7171

72-
//this.validateDefaultConstructor(clazz);
72+
this.validateDefaultConstructor(clazz);
7373
if (!Serializable.class.isAssignableFrom(clazz)) {
7474
throw new RuntimeException("Class " + classPath + " does not implements Serializable");
7575
}
@@ -99,7 +99,6 @@ public Serializable deserialize(String path, BukkitConfiguration configuration)
9999
throw new MissingSerializerException(field.getType());
100100
}
101101

102-
//System.out.println("Called deserialization with " + serializer.getClass().getName() + " for " + path + "." + configuration.getNameStyle().format(field.getName()));
103102
field.set(instance, serializer.deserialize(path + "." + configuration.getNameStyle().format(field.getName()), configuration));
104103
}
105104
}

src/main/java/pl/mikigal/config/util/TypeUtils.java

+26
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,30 @@ public static boolean isPrimitiveOrWrapper(Class<?> clazz) {
159159
public static Class<?> getWrapper(Class<?> primitive) {
160160
return WRAPPERS.get(primitive);
161161
}
162+
163+
/**
164+
* Check is given object array of primitives
165+
* @param object to check
166+
* @return true if given object is array of primitives, else false
167+
*/
168+
public static boolean isPrimitiveArray(Object object) {
169+
return isPrimitiveArray(object.getClass());
170+
}
171+
172+
/**
173+
* Check is given clazz is array of primitives
174+
* @param clazz to check
175+
* @return true if given clazz is array of primitives, else false
176+
*/
177+
public static boolean isPrimitiveArray(Class<?> clazz) {
178+
return clazz.isArray() ||
179+
clazz.equals(boolean[].class) ||
180+
clazz.equals(int[].class) ||
181+
clazz.equals(char[].class) ||
182+
clazz.equals(byte[].class) ||
183+
clazz.equals(short[].class) ||
184+
clazz.equals(double[].class) ||
185+
clazz.equals(long[].class) ||
186+
clazz.equals(float[].class);
187+
}
162188
}

0 commit comments

Comments
 (0)