Skip to content

Commit d935426

Browse files
Add UtLinkedListWithNullableCheck
1 parent c74fb35 commit d935426

File tree

4 files changed

+139
-8
lines changed

4 files changed

+139
-8
lines changed

utbot-framework/src/main/java/org/utbot/engine/overrides/collections/UtLinkedList.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public UtLinkedList(Collection<? extends E> c) {
7272
* <li> elementData is marked as parameter </li>
7373
* <li> elementData.storage and it's elements are marked as parameters </li>
7474
*/
75-
private void preconditionCheck() {
75+
protected void preconditionCheck() {
7676
if (alreadyVisited(this)) {
7777
return;
7878
}
@@ -88,13 +88,13 @@ private void preconditionCheck() {
8888
visit(this);
8989
}
9090

91-
private void rangeCheck(int index) {
91+
protected void rangeCheck(int index) {
9292
if (index < 0 || index >= elementData.end - elementData.begin) {
9393
throw new IndexOutOfBoundsException();
9494
}
9595
}
9696

97-
private void rangeCheckForAdd(int index) {
97+
protected void rangeCheckForAdd(int index) {
9898
if (index < 0 || index > elementData.end - elementData.begin) {
9999
throw new IndexOutOfBoundsException();
100100
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.utbot.engine.overrides.collections;
2+
3+
import java.util.Collection;
4+
5+
/**
6+
* This list forbids inserting null elements to support some implementations of {@link java.util.Deque} like
7+
* {@link java.util.ArrayDeque}.
8+
*
9+
* TODO: <a href="https://github.com/UnitTestBot/UTBotJava/issues/819">Support super calls in inherited wrappers</a>
10+
*
11+
* @see UtLinkedList
12+
* @param <E>
13+
*/
14+
public class UtLinkedListWithNullableCheck<E> extends UtLinkedList<E> {
15+
@SuppressWarnings("unused")
16+
UtLinkedListWithNullableCheck(RangeModifiableUnlimitedArray<E> elementData, int fromIndex, int toIndex) {
17+
super(elementData, fromIndex, toIndex);
18+
for (int i = elementData.begin; i < elementData.end; i++) {
19+
if (elementData.get(i) == null) {
20+
throw new NullPointerException();
21+
}
22+
}
23+
}
24+
25+
@SuppressWarnings("unused")
26+
public UtLinkedListWithNullableCheck() {
27+
super();
28+
}
29+
30+
@SuppressWarnings("unused")
31+
public UtLinkedListWithNullableCheck(Collection<? extends E> c) {
32+
super(c);
33+
}
34+
35+
@Override
36+
public E set(int index, E element) {
37+
if (element == null) {
38+
throw new NullPointerException();
39+
}
40+
preconditionCheck();
41+
rangeCheck(index);
42+
E oldElement = elementData.get(index + elementData.begin);
43+
elementData.set(index + elementData.begin, element);
44+
return oldElement;
45+
}
46+
47+
@Override
48+
public void addFirst(E e) {
49+
if (e == null) {
50+
throw new NullPointerException();
51+
}
52+
preconditionCheck();
53+
elementData.set(--elementData.begin, e);
54+
}
55+
56+
@Override
57+
public void addLast(E e) {
58+
if (e == null) {
59+
throw new NullPointerException();
60+
}
61+
preconditionCheck();
62+
elementData.set(elementData.end++, e);
63+
}
64+
65+
@Override
66+
public boolean offerFirst(E e) {
67+
if (e == null) {
68+
throw new NullPointerException();
69+
}
70+
preconditionCheck();
71+
elementData.set(--elementData.begin, e);
72+
return true;
73+
}
74+
75+
@Override
76+
public boolean offerLast(E e) {
77+
if (e == null) {
78+
throw new NullPointerException();
79+
}
80+
preconditionCheck();
81+
elementData.set(elementData.end++, e);
82+
return true;
83+
}
84+
85+
@Override
86+
public void add(int index, E element) {
87+
if (element == null) {
88+
throw new NullPointerException();
89+
}
90+
preconditionCheck();
91+
rangeCheckForAdd(index);
92+
elementData.end++;
93+
elementData.insert(index + elementData.begin, element);
94+
}
95+
96+
@Override
97+
public boolean addAll(Collection<? extends E> c) {
98+
for (Object elem : c.toArray()) {
99+
if (elem == null) {
100+
throw new NullPointerException();
101+
}
102+
}
103+
preconditionCheck();
104+
elementData.setRange(elementData.end, c.toArray(), 0, c.size());
105+
elementData.end += c.size();
106+
return true;
107+
}
108+
109+
@Override
110+
public boolean addAll(int index, Collection<? extends E> c) {
111+
for (Object elem : c.toArray()) {
112+
if (elem == null) {
113+
throw new NullPointerException();
114+
}
115+
}
116+
preconditionCheck();
117+
rangeCheckForAdd(index);
118+
elementData.insertRange(index + elementData.begin, c.toArray(), 0, c.size());
119+
elementData.end += c.size();
120+
return true;
121+
}
122+
}

utbot-framework/src/main/kotlin/org/utbot/engine/CollectionWrappers.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.utbot.engine.overrides.collections.UtGenericStorage
88
import org.utbot.engine.overrides.collections.UtHashMap
99
import org.utbot.engine.overrides.collections.UtHashSet
1010
import org.utbot.engine.overrides.collections.UtLinkedList
11+
import org.utbot.engine.overrides.collections.UtLinkedListWithNullableCheck
1112
import org.utbot.engine.pc.UtAddrExpression
1213
import org.utbot.engine.pc.UtExpression
1314
import org.utbot.engine.pc.select
@@ -195,24 +196,30 @@ abstract class BaseGenericStorageBasedContainerWrapper(containerClassName: Strin
195196
*/
196197
enum class UtListClass {
197198
UT_ARRAY_LIST,
198-
UT_LINKED_LIST;
199+
UT_LINKED_LIST,
200+
UT_LINKED_LIST_WITH_NULLABLE_CHECK;
199201

200202
val className: String
201203
get() = when (this) {
202204
UT_ARRAY_LIST -> UtArrayList::class.java.canonicalName
203205
UT_LINKED_LIST -> UtLinkedList::class.java.canonicalName
206+
UT_LINKED_LIST_WITH_NULLABLE_CHECK -> UtLinkedListWithNullableCheck::class.java.canonicalName
204207
}
205208
}
206209

207210
/**
208211
* BaseCollectionWrapper that uses implementation of [UtArrayList] or [UtLinkedList]
209212
* depending on specified [utListClass] parameter.
210213
*
214+
* This class is also used for wrapping [java.util.Deque] as [UtLinkedList] and [UtLinkedListWithNullableCheck]
215+
* implement [java.util.Deque].
216+
*
211217
* At resolving stage ListWrapper is resolved to [UtAssembleModel].
212-
* This model is instantiated by [java.util.ArrayList] constructor if utListClass is [UtListClass.UT_ARRAY_LIST]
213-
* or by [java.util.LinkedList] constructor, if utListClass is [UtListClass.UT_LINKED_LIST]
218+
* This model is instantiated by [java.util.ArrayList] constructor if utListClass is [UtListClass.UT_ARRAY_LIST],
219+
* by [java.util.LinkedList] constructor if utListClass is [UtListClass.UT_LINKED_LIST] or by other constructors
220+
* depending on concrete type from passed [ReferenceValue] in [resolveValueModels] function.
214221
*
215-
* Modification chain consists of consequent [java.util.List.add] methods
222+
* Modification chain consists of consequent [java.util.Collection.add] methods
216223
* that are arranged to iterating order of list.
217224
*/
218225
class ListWrapper(private val utListClass: UtListClass) : BaseGenericStorageBasedContainerWrapper(utListClass.className) {
@@ -227,7 +234,7 @@ class ListWrapper(private val utListClass: UtListClass) : BaseGenericStorageBase
227234
}
228235

229236
override val modificationMethodId: MethodId = methodId(
230-
classId = java.util.List::class.id,
237+
classId = java.util.Collection::class.id,
231238
name = "add",
232239
returnType = booleanClassId,
233240
arguments = arrayOf(objectClassId),

utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.utbot.engine.overrides.collections.UtOptionalDouble
3434
import org.utbot.engine.overrides.collections.UtOptionalInt
3535
import org.utbot.engine.overrides.collections.UtOptionalLong
3636
import org.utbot.engine.overrides.collections.AbstractCollection
37+
import org.utbot.engine.overrides.collections.UtLinkedListWithNullableCheck
3738
import org.utbot.engine.overrides.stream.Arrays
3839
import org.utbot.engine.overrides.stream.Stream
3940
import org.utbot.engine.overrides.stream.UtStream
@@ -129,6 +130,7 @@ private val classesToLoad = arrayOf(
129130
UtArrayList::class,
130131
UtArrayList.UtArrayListIterator::class,
131132
UtLinkedList::class,
133+
UtLinkedListWithNullableCheck::class,
132134
UtLinkedList.UtLinkedListIterator::class,
133135
UtLinkedList.ReverseIteratorWrapper::class,
134136
UtHashSet::class,

0 commit comments

Comments
 (0)