Skip to content

Commit 7d6d01a

Browse files
committed
Disabled ClassCastException check for streams constructing
1 parent 4758aa3 commit 7d6d01a

File tree

8 files changed

+56
-18
lines changed

8 files changed

+56
-18
lines changed

utbot-api/src/main/java/org/utbot/api/mock/UtMock.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ public static void assumeOrExecuteConcretely(boolean predicate) {
2323
// In oppose to assume, we don't have predicate check here
2424
// to avoid RuntimeException during concrete execution
2525
}
26+
27+
@SuppressWarnings("unused")
28+
public static void disableClassCastExceptionCheck(Object object) {}
2629
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.engine.overrides.collections;
22

3+
import org.utbot.api.mock.UtMock;
34
import org.utbot.engine.overrides.UtArrayMock;
45
import java.util.AbstractList;
56
import java.util.ArrayList;
@@ -378,10 +379,9 @@ public Stream<E> stream() {
378379
preconditionCheck();
379380

380381
int size = elementData.end;
381-
E[] data = (E[]) new Object[size];
382-
for (int i = 0; i < size; i++) {
383-
data[i] = elementData.get(i);
384-
}
382+
final Object[] toArray = elementData.toArray(0, size);
383+
UtMock.disableClassCastExceptionCheck(toArray);
384+
E[] data = (E[]) toArray;
385385

386386
return new UtStream<>(data, size);
387387
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.engine.overrides.collections;
22

3+
import org.utbot.api.mock.UtMock;
34
import org.utbot.engine.overrides.UtArrayMock;
45
import java.util.AbstractSet;
56
import java.util.Collection;
@@ -272,10 +273,9 @@ public Stream<E> stream() {
272273
preconditionCheck();
273274

274275
int size = elementData.end;
275-
E[] data = (E[]) new Object[size];
276-
for (int i = 0; i < size; i++) {
277-
data[i] = elementData.get(i);
278-
}
276+
final Object[] toArray = elementData.toArray(0, size);
277+
UtMock.disableClassCastExceptionCheck(toArray);
278+
E[] data = (E[]) toArray;
279279

280280
return new UtStream<>(data, size);
281281
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.engine.overrides.collections;
22

3+
import org.utbot.api.mock.UtMock;
34
import org.utbot.engine.overrides.UtArrayMock;
45
import java.util.AbstractSequentialList;
56
import java.util.Collection;
@@ -458,10 +459,9 @@ public Stream<E> stream() {
458459
preconditionCheck();
459460

460461
int size = elementData.end;
461-
E[] data = (E[]) new Object[size];
462-
for (int i = 0; i < size; i++) {
463-
data[i] = elementData.get(i);
464-
}
462+
final Object[] toArray = elementData.toArray(0, size);
463+
UtMock.disableClassCastExceptionCheck(toArray);
464+
E[] data = (E[]) toArray;
465465

466466
return new UtStream<>(data, size);
467467
}

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/UtStream.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.engine.overrides.stream;
22

3+
import org.utbot.api.mock.UtMock;
34
import org.utbot.engine.overrides.UtArrayMock;
45
import org.utbot.engine.overrides.collections.RangeModifiableUnlimitedArray;
56
import org.utbot.engine.overrides.collections.UtGenericStorage;
@@ -145,40 +146,49 @@ public <R> Stream<R> map(Function<? super E, ? extends R> mapper) {
145146
return new UtStream<>((R[]) mapped, size);
146147
}
147148

149+
@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
148150
@Override
149151
public IntStream mapToInt(ToIntFunction<? super E> mapper) {
150152
preconditionCheckWithClosingStream();
151153

152154
int size = elementData.end;
153155
Integer[] data = new Integer[size];
154156
for (int i = 0; i < size; i++) {
155-
data[i] = mapper.applyAsInt(elementData.get(i));
157+
final Object object = elementData.get(i);
158+
UtMock.disableClassCastExceptionCheck(object);
159+
data[i] = mapper.applyAsInt((E) object);
156160
}
157161

158162
return new UtIntStream(data, size);
159163
}
160164

165+
@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
161166
@Override
162167
public LongStream mapToLong(ToLongFunction<? super E> mapper) {
163168
preconditionCheckWithClosingStream();
164169

165170
int size = elementData.end;
166171
Long[] data = new Long[size];
167172
for (int i = 0; i < size; i++) {
168-
data[i] = mapper.applyAsLong(elementData.get(i));
173+
final Object object = elementData.get(i);
174+
UtMock.disableClassCastExceptionCheck(object);
175+
data[i] = mapper.applyAsLong((E) object);
169176
}
170177

171178
return new UtLongStream(data, size);
172179
}
173180

181+
@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
174182
@Override
175183
public DoubleStream mapToDouble(ToDoubleFunction<? super E> mapper) {
176184
preconditionCheckWithClosingStream();
177185

178186
int size = elementData.end;
179187
Double[] data = new Double[size];
180188
for (int i = 0; i < size; i++) {
181-
data[i] = mapper.applyAsDouble(elementData.get(i));
189+
final Object object = elementData.get(i);
190+
UtMock.disableClassCastExceptionCheck(object);
191+
data[i] = mapper.applyAsDouble((E) object);
182192
}
183193

184194
return new UtDoubleStream(data, size);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ val SootMethod.isUtMockAssume
478478
val SootMethod.isUtMockAssumeOrExecuteConcretely
479479
get() = signature == assumeOrExecuteConcretelyMethod.signature
480480

481+
val SootMethod.isUtMockForbidClassCastException
482+
get() = signature == disableClassCastExceptionCheckMethod.signature
483+
481484
/**
482485
* Returns true is the [SootMethod] is defined in a class from
483486
* [UTBOT_OVERRIDE_PACKAGE_NAME] package and its name is `preconditionCheck`.

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class Mocker(
176176
): Boolean {
177177
if (isUtMockAssume(mockInfo)) return false // never mock UtMock.assume invocation
178178
if (isUtMockAssumeOrExecuteConcretely(mockInfo)) return false // never mock UtMock.assumeOrExecuteConcretely invocation
179+
if (isUtMockDisableClassCastExceptionCheck(mockInfo)) return false // never mock UtMock.disableClassCastExceptionCheck invocation
179180
if (isOverriddenClass(type)) return false // never mock overriden classes
180181
if (isMakeSymbolic(mockInfo)) return true // support for makeSymbolic
181182
if (type.sootClass.isArtificialEntity) return false // never mock artificial types, i.e. Maps$lambda_computeValue_1__7
@@ -205,17 +206,20 @@ class Mocker(
205206
/**
206207
* Checks whether [mockInfo] containing information about UtMock.makeSymbolic call or not.
207208
*/
208-
private fun isMakeSymbolic(mockInfo: UtMockInfo) =
209+
private fun isMakeSymbolic(mockInfo: UtMockInfo): Boolean =
209210
mockInfo is UtStaticMethodMockInfo &&
210211
(mockInfo.methodId.signature == makeSymbolicBytecodeSignature ||
211212
mockInfo.methodId.signature == nonNullableMakeSymbolicBytecodeSignature)
212213

213-
private fun isUtMockAssume(mockInfo: UtMockInfo) =
214+
private fun isUtMockAssume(mockInfo: UtMockInfo): Boolean =
214215
mockInfo is UtStaticMethodMockInfo && mockInfo.methodId.signature == assumeBytecodeSignature
215216

216-
private fun isUtMockAssumeOrExecuteConcretely(mockInfo: UtMockInfo) =
217+
private fun isUtMockAssumeOrExecuteConcretely(mockInfo: UtMockInfo): Boolean =
217218
mockInfo is UtStaticMethodMockInfo && mockInfo.methodId.signature == assumeOrExecuteConcretelyBytecodeSignature
218219

220+
private fun isUtMockDisableClassCastExceptionCheck(mockInfo: UtMockInfo): Boolean =
221+
mockInfo is UtStaticMethodMockInfo && mockInfo.methodId.signature == disableClassCastExceptionCheckBytecodeSignature
222+
219223
private fun isEngineClass(type: RefType) = type.className in engineClasses
220224

221225
private fun mockAlways(type: RefType) = type.className in classesToMockAlways
@@ -326,6 +330,9 @@ internal val assumeMethod: SootMethod
326330
internal val assumeOrExecuteConcretelyMethod: SootMethod
327331
get() = utMockClass.getMethod(ASSUME_OR_EXECUTE_CONCRETELY_NAME, listOf(BooleanType.v()))
328332

333+
internal val disableClassCastExceptionCheckMethod: SootMethod
334+
get() = utMockClass.getMethod(DISABLE_CLASS_CAST_EXCEPTION_CHECK_NAME, listOf(OBJECT_TYPE))
335+
329336
val makeSymbolicBytecodeSignature: String
330337
get() = makeSymbolicMethod.executableId.signature
331338

@@ -338,6 +345,9 @@ val assumeBytecodeSignature: String
338345
val assumeOrExecuteConcretelyBytecodeSignature: String
339346
get() = assumeOrExecuteConcretelyMethod.executableId.signature
340347

348+
val disableClassCastExceptionCheckBytecodeSignature: String
349+
get() = disableClassCastExceptionCheckMethod.executableId.signature
350+
341351
internal val UTBOT_OVERRIDE_PACKAGE_NAME = UtOverrideMock::class.java.packageName
342352

343353
private val arraycopyMethod : KFunction5<Array<out Any>, Int, Array<out Any>, Int, Int, Unit> = UtArrayMock::arraycopy
@@ -357,3 +367,4 @@ internal val utLogicMockIteMethodName = UtLogicMock::ite.name
357367
private const val MAKE_SYMBOLIC_NAME = "makeSymbolic"
358368
private const val ASSUME_NAME = "assume"
359369
private const val ASSUME_OR_EXECUTE_CONCRETELY_NAME = "assumeOrExecuteConcretely"
370+
private const val DISABLE_CLASS_CAST_EXCEPTION_CHECK_NAME = "disableClassCastExceptionCheck"

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,7 @@ class Traverser(
25022502
declaringClass == utOverrideMockClass -> utOverrideMockInvoke(target, parameters)
25032503
declaringClass == utLogicMockClass -> utLogicMockInvoke(target, parameters)
25042504
declaringClass == utArrayMockClass -> utArrayMockInvoke(target, parameters)
2505+
isUtMockForbidClassCastException -> isUtMockDisableClassCastExceptionCheckInvoke(parameters)
25052506
else -> {
25062507
val graph = substitutedMethod?.jimpleBody()?.graph() ?: jimpleBody().graph()
25072508
pushToPathSelector(graph, target.instance, parameters, target.constraints, isLibraryMethod)
@@ -2510,6 +2511,16 @@ class Traverser(
25102511
}
25112512
}
25122513

2514+
private fun isUtMockDisableClassCastExceptionCheckInvoke(
2515+
parameters: List<SymbolicValue>
2516+
): List<MethodResult> {
2517+
val param = parameters.single() as ReferenceValue
2518+
val paramAddr = param.addr
2519+
typeRegistry.disableCastClassExceptionCheck(paramAddr)
2520+
2521+
return listOf(MethodResult(voidValue))
2522+
}
2523+
25132524
private fun TraversalContext.utOverrideMockInvoke(target: InvocationTarget, parameters: List<SymbolicValue>): List<MethodResult> {
25142525
when (target.method.name) {
25152526
utOverrideMockAlreadyVisitedMethodName -> {

0 commit comments

Comments
 (0)