From 9c00c52aaa658a86baa6931e451912f2cd1e6abf Mon Sep 17 00:00:00 2001 From: mikigal Date: Tue, 9 Nov 2021 10:52:57 +0100 Subject: [PATCH] Added validation for array of primitives --- README.MD | 2 +- build.gradle | 2 +- .../config/ConfigInvocationHandler.java | 8 ++++++ .../universal/UniversalObjectSerializer.java | 3 +-- .../pl/mikigal/config/util/TypeUtils.java | 26 +++++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.MD b/README.MD index 0fb8789..090fb12 100644 --- a/README.MD +++ b/README.MD @@ -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 diff --git a/build.gradle b/build.gradle index 68ac40a..c7e3cc9 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'pl.mikigal' -version '1.1.8' +version '1.1.9' publishing { repositories { diff --git a/src/main/java/pl/mikigal/config/ConfigInvocationHandler.java b/src/main/java/pl/mikigal/config/ConfigInvocationHandler.java index 4b99c4d..a5a1874 100644 --- a/src/main/java/pl/mikigal/config/ConfigInvocationHandler.java +++ b/src/main/java/pl/mikigal/config/ConfigInvocationHandler.java @@ -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()); } @@ -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"); } diff --git a/src/main/java/pl/mikigal/config/serializer/universal/UniversalObjectSerializer.java b/src/main/java/pl/mikigal/config/serializer/universal/UniversalObjectSerializer.java index 3a339b8..4fdf0b2 100644 --- a/src/main/java/pl/mikigal/config/serializer/universal/UniversalObjectSerializer.java +++ b/src/main/java/pl/mikigal/config/serializer/universal/UniversalObjectSerializer.java @@ -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"); } @@ -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)); } } diff --git a/src/main/java/pl/mikigal/config/util/TypeUtils.java b/src/main/java/pl/mikigal/config/util/TypeUtils.java index ff29e27..e5ac10a 100644 --- a/src/main/java/pl/mikigal/config/util/TypeUtils.java +++ b/src/main/java/pl/mikigal/config/util/TypeUtils.java @@ -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); + } }