Questions Tagged With listhttps://answers.axonivy.com/tags/list/?type=rss&user=Genzer%20Hawkerquestions tagged <span class="tag">list</span>enThu, 12 Mar 2015 08:27:40 -0400List converted from an Enum array in Ivy Script doesn't work correctlyhttps://answers.axonivy.com/questions/1224/list-converted-from-an-enum-array-in-ivy-script-doesn-t-work-correctly<p>Today I found something looks like a bug in Ivy Script.</p> <p>In Ivy Script, I want to have a <code>List</code> of all the elements of an <code>enum</code>, say <code>WorkflowPriority</code>, I simply call <code>WorkflowPriority.values()</code>. However, the instance of <code>List</code> returned by the call doesn't work correctly, especially with operation <code>contains</code> and <code>remove</code>.</p> <p>Here are the tests that I made to check:</p> <p>(This is an Ivy Script snippet)</p> <pre><code>import org.junit.Assert; import com.genzerhawherk.bugs.Lists; import ch.ivyteam.ivy.workflow.WorkflowPriority; // Below are all failed tests Assert.assertTrue(WorkflowPriority.values().contains(WorkflowPriority.HIGH)); List&lt;WorkflowPriority&gt; withGenerics = WorkflowPriority.values(); Assert.assertTrue(withGenerics.contains(WorkflowPriority.HIGH)); List fromEnums = Lists.fromEnums(WorkflowPriority.class); Assert.assertTrue(fromEnums.contains(WorkflowPriority.HIGH)); List fromEnumVarargs = Lists.enumVarArgs(WorkflowPriority.HIGH, WorkflowPriority.EXCEPTION); Assert.assertTrue(fromEnumVarargs.contains(WorkflowPriority.HIGH)); // Below are all passed tests List listComprehensive = [WorkflowPriority.HIGH]; Assert.assertTrue(listComprehensive.contains(WorkflowPriority.HIGH)); List newPriorities = new List().addAll(WorkflowPriority.values()); Assert.assertTrue(newPriorities.contains(WorkflowPriority.HIGH)); List fromList = Lists.toArray([WorkflowPriority.HIGH]); Assert.assertTrue(fromList.contains(WorkflowPriority.HIGH)); List fromVarArgs = Lists.normalVarArgs(WorkflowPriority.HIGH); Assert.assertTrue(fromVarArgs.contains(WorkflowPriority.HIGH)); List&lt;WorkflowPriority&gt; manuallyConstructed = new List&lt;WorkflowPriority&gt;(); manuallyConstructed.add(WorkflowPriority.HIGH); Assert.assertTrue(manuallyConstructed.contains(WorkflowPriority.HIGH)); </code></pre> <p><strong>The bug particularly relates to converting <code>&lt;T extends Enum&lt;T&gt;&gt; T[]</code> into a <code>List&lt;T&gt;</code></strong></p> <p>Below is the class <code>Lists</code> I use in the test</p> <pre><code>public class Lists { public static Object[] toArray(Collection&lt;?&gt; collection) { return collection.toArray(); } @SafeVarargs public static &lt;T&gt; T[] normalVarArgs(T...elements) { return Arrays.copyOf(elements, elements.length); } public static &lt;T extends Enum&lt;T&gt;&gt; T[] fromEnums(Class&lt;T&gt; enums) { return enums.getEnumConstants(); } @SafeVarargs public static &lt;T extends Enum&lt;T&gt;&gt; T[] enumVarArgs(T...enums) { return enums; } } </code></pre>Genzer HawkerThu, 12 Mar 2015 08:27:40 -0400https://answers.axonivy.com/questions/1224/list-converted-from-an-enum-array-in-ivy-script-doesn-t-work-correctlyivyscriptenumlistarray