From d18324619c357ccb6077a4a75260c7ea9be413f5 Mon Sep 17 00:00:00 2001 From: Speiger Date: Mon, 26 Apr 2021 23:02:51 +0200 Subject: [PATCH] Added Safty methods and test into IObjectArray to reduce crashes. --- .../templates/lists/ArrayList.template | 10 ++++++ .../templates/utils/IArray.template | 11 ++++++- .../templates/utils/Lists.template | 7 +++- .../objects/list/ObjectArrayListTest.java | 33 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/test/java/speiger/src/collections/objects/list/ObjectArrayListTest.java diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template index 4689b21a..71ad6406 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -556,6 +556,16 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE return data; } +#if TYPE_OBJECT + /** + * @return if the array is castable + */ + @Override + public boolean isCastable() { + return data.getClass() != Object[].class; + } + +#endif /** * A Type Specific foreach function that reduces (un)boxing * diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/IArray.template b/src/builder/resources/speiger/assets/collections/templates/utils/IArray.template index 3cc3b023..ff040f0a 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/IArray.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/IArray.template @@ -19,13 +19,22 @@ public interface IARRAY KEY_GENERIC_TYPE extends IArray */ public KEY_TYPE[] elements(); +#if TYPE_OBJECT + /** + * Method to indicate if the function is save to use + * @return true if the object is castable to the type that is requested + */ + public boolean isCastable(); +#endif /** * Provides the Underlying Array in the Implementation. This function exists purely because of Synchronization wrappers to help run Synchronized Code * @param action the action that handles the array * @throws NullPointerException if action is null - * @throws ClassCastException if the return type does not match the underlying array. (Only for Object Implementations) */ public default void elements(Consumer action) { +#if TYPE_OBJECT + if(!isCastable()) return; +#endif Objects.requireNonNull(action); action.accept(elements()); } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template index 8b348087..ce121808 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template @@ -149,11 +149,16 @@ public class LISTS @Override public KEY_TYPE[] elements() { synchronized(mutex) { return l.elements(); } } +#if TYPE_OBJECT + @Override + public boolean isCastable() { synchronized(mutex) { return l.isCastable(); } } + +#endif @Override public void elements(Consumer action) { Objects.requireNonNull(action); synchronized(mutex) { - action.accept(elements()); + l.elements(action); } } } diff --git a/src/test/java/speiger/src/collections/objects/list/ObjectArrayListTest.java b/src/test/java/speiger/src/collections/objects/list/ObjectArrayListTest.java new file mode 100644 index 00000000..7984b1e5 --- /dev/null +++ b/src/test/java/speiger/src/collections/objects/list/ObjectArrayListTest.java @@ -0,0 +1,33 @@ +package speiger.src.collections.objects.list; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import speiger.src.collections.objects.lists.ObjectArrayList; +import speiger.src.collections.objects.utils.IObjectArray; + +@SuppressWarnings("javadoc") +public class ObjectArrayListTest +{ + @Test + public void testCastable() + { + ObjectArrayList list = new ObjectArrayList<>(); + list.add("Test"); + list.add("Testing"); + list.add("Testing stuff"); + testCastable(list, false); + testCastable(ObjectArrayList.wrap("Test", "Testing", "Testing stuff"), true); + } + + public void testCastable(IObjectArray castable, boolean result) + { + Assert.assertTrue(castable.isCastable() == result); + AtomicInteger amount = new AtomicInteger(); + castable.elements(T -> amount.set(T.length)); + Assert.assertTrue(amount.get() > 0 == result); + + } +}