Dear all,
My name is Facundo, and I'm currently working on automated test generation.
As part of an experiment, I came across a potential issue
with org.apache.commons.collections4.list.AbstractLinkedList.hashCode. The
code of this method is:
@Override
public int hashCode() {
int hashCode = 1;
for (final E e : this) {
hashCode = 31 * hashCode + (e == null ? 0 : e.hashCode());
}
return hashCode;
}
The problem may arise when generating a collection of Object, and inserting
the same collection as an element of itself. For example, consider the
following test case using the class NodeCachingLinkedList which extends
AbstractLinkedList:
@Test
public void test0() {
NodeCachingLinkedList<Object> nodeCachingLinkedListGA0 = new
NodeCachingLinkedList<Object>();
nodeCachingLinkedListGA0.addFirst(nodeCachingLinkedListGA0);
nodeCachingLinkedListGA0.hashCode();
}
This will result in an infinite recursive call, leading to a stack overflow
exception.
I would like to know if you consider this to be an issue. A possible
solution would be the following:
@Override
public int hashCode() {
int hashCode = 1;
for (final E e : this) {
hashCode = 31 * hashCode + (e == null || e == this ? 0 : e
.hashCode());
}
return hashCode;
}
Please let me know what you think about it.
Best regards,
Facundo
--
Facundo Molina