Skip to content

Commit

Permalink
Added validation for array of primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
mikigal committed Nov 9, 2021
1 parent 81335cb commit 9c00c52
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ maven {
url = 'https://repo.mikigal.pl/releases'
}
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.1.8'
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.1.9'
```

#### Maven
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'pl.mikigal'
version '1.1.8'
version '1.1.9'

publishing {
repositories {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/pl/mikigal/config/ConfigInvocationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ private void prepareMethods() {
}
}

if (TypeUtils.isPrimitiveArray(method.getReturnType())) {
throw new InvalidConfigException("Arrays with primitives are not supported");
}

ConfigPath configPath = method.getAnnotation(ConfigPath.class);
this.configPaths.put(name, configPath == null ? configuration.getNameStyle().format(name) : configPath.value());
}
Expand All @@ -182,6 +186,10 @@ private void prepareMethods() {
throw new InvalidConfigException("Setter method " + name + " is not void type");
}

if (TypeUtils.isPrimitiveArray(method.getReturnType())) {
throw new InvalidConfigException("Arrays with primitives are not supported");
}

if (method.getParameterCount() != 1) {
throw new InvalidConfigException("Setter method " + name + " has not 1 parameter");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Serializable deserialize(String path, BukkitConfiguration configuration)
throw new RuntimeException("An error occurred while deserializing class '" + classPath + "'", e);
}

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

//System.out.println("Called deserialization with " + serializer.getClass().getName() + " for " + path + "." + configuration.getNameStyle().format(field.getName()));
field.set(instance, serializer.deserialize(path + "." + configuration.getNameStyle().format(field.getName()), configuration));
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/pl/mikigal/config/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,30 @@ public static boolean isPrimitiveOrWrapper(Class<?> clazz) {
public static Class<?> getWrapper(Class<?> primitive) {
return WRAPPERS.get(primitive);
}

/**
* Check is given object array of primitives
* @param object to check
* @return true if given object is array of primitives, else false
*/
public static boolean isPrimitiveArray(Object object) {
return isPrimitiveArray(object.getClass());
}

/**
* Check is given clazz is array of primitives
* @param clazz to check
* @return true if given clazz is array of primitives, else false
*/
public static boolean isPrimitiveArray(Class<?> clazz) {
return clazz.isArray() ||
clazz.equals(boolean[].class) ||
clazz.equals(int[].class) ||
clazz.equals(char[].class) ||
clazz.equals(byte[].class) ||
clazz.equals(short[].class) ||
clazz.equals(double[].class) ||
clazz.equals(long[].class) ||
clazz.equals(float[].class);
}
}

0 comments on commit 9c00c52

Please sign in to comment.