Skip to content

Commit c82c702

Browse files
committed
Minimize exceptions in signature
1 parent 67cc7d9 commit c82c702

File tree

9 files changed

+50
-23
lines changed

9 files changed

+50
-23
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/UtExecutionResult.kt

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ data class UtExecutionSuccess(val model: UtModel) : UtExecutionResult() {
1111

1212
sealed class UtExecutionFailure : UtExecutionResult() {
1313
abstract val exception: Throwable
14-
val isCheckedException get() = !(exception is RuntimeException || exception is Error)
1514
}
1615

1716
data class UtOverflowFailure(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.utbot.framework.plugin.api.util
2+
3+
import org.utbot.framework.plugin.api.ClassId
4+
5+
val Throwable.description
6+
get() = message?.replace('\n', '\t') ?: "<Throwable with empty message>"
7+
8+
val Throwable.isCheckedException
9+
get() = !(this is RuntimeException|| this is Error)
10+
11+
val ClassId.isCheckedException
12+
get() = !(RuntimeException::class.java.isAssignableFrom(this.jClass) || Error::class.java.isAssignableFrom(this.jClass))

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ import org.utbot.framework.plugin.api.util.id
153153
import org.utbot.framework.plugin.api.util.jClass
154154
import org.utbot.framework.plugin.api.util.signature
155155
import org.utbot.framework.plugin.api.util.utContext
156-
import org.utbot.framework.util.description
156+
import org.utbot.framework.plugin.api.util.description
157157
import org.utbot.framework.util.executableId
158158
import org.utbot.fuzzer.FuzzedMethodDescription
159159
import org.utbot.fuzzer.ModelProvider

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/context/CgContext.kt

+27-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import org.utbot.framework.plugin.api.UtMethod
4646
import org.utbot.framework.plugin.api.UtModel
4747
import org.utbot.framework.plugin.api.UtReferenceModel
4848
import org.utbot.framework.plugin.api.UtTestCase
49-
import org.utbot.framework.plugin.api.util.executableId
5049
import java.util.IdentityHashMap
5150
import kotlinx.collections.immutable.PersistentList
5251
import kotlinx.collections.immutable.PersistentMap
@@ -55,6 +54,11 @@ import kotlinx.collections.immutable.persistentListOf
5554
import kotlinx.collections.immutable.persistentMapOf
5655
import kotlinx.collections.immutable.persistentSetOf
5756
import org.utbot.framework.codegen.model.constructor.builtin.streamsDeepEqualsMethodId
57+
import org.utbot.framework.plugin.api.util.isSubtypeOf
58+
import org.utbot.framework.plugin.api.util.isNotSubtypeOf
59+
import org.utbot.framework.plugin.api.util.isCheckedException
60+
import org.utbot.framework.plugin.api.util.id
61+
import org.utbot.framework.plugin.api.util.executableId
5862

5963
/**
6064
* Interface for all code generation context aware entities
@@ -232,7 +236,25 @@ internal interface CgContextOwner {
232236
currentExecutable = method.callable.executableId
233237
}
234238

235-
fun addException(exception: ClassId) {
239+
fun addExceptionIfNeeded(exception: ClassId) {
240+
when (exception) {
241+
is BuiltinClassId -> {}
242+
else -> {
243+
if (exception isNotSubtypeOf Throwable::class.id) {
244+
error("Class $exception which is not a Throwable was passed")
245+
}
246+
247+
val isUnchecked = !exception.isCheckedException
248+
val alreadyAdded =
249+
collectedExceptions.any { existingException -> exception isSubtypeOf existingException }
250+
251+
if (isUnchecked || alreadyAdded) return
252+
253+
collectedExceptions
254+
.removeIf { existingException -> existingException isSubtypeOf exception }
255+
}
256+
}
257+
236258
if (collectedExceptions.add(exception)) {
237259
importIfNeeded(exception)
238260
}
@@ -416,9 +438,9 @@ internal data class CgContext(
416438
val simpleName = testClassCustomName ?: "${createTestClassName(classUnderTest.name)}Test"
417439
val name = "$packagePrefix$simpleName"
418440
BuiltinClassId(
419-
name = name,
420-
canonicalName = name,
421-
simpleName = simpleName
441+
name = name,
442+
canonicalName = name,
443+
simpleName = simpleName
422444
)
423445
}
424446

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgCallableAccessManager.kt

+5-8
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,12 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
127127
//Builtin methods does not have jClass, so [methodId.method] will crash on it,
128128
//so we need to collect required exceptions manually from source codes
129129
if (methodId is BuiltinMethodId) {
130-
methodId.findExceptionTypes().forEach { addException(it) }
130+
methodId.findExceptionTypes().forEach { addExceptionIfNeeded(it) }
131131
return
132132
}
133-
//If [InvocationTargetException] is thrown manually in test, we need
134-
// to add "throws Throwable" and other exceptions are not required so on.
133+
135134
if (methodId == getTargetException) {
136-
collectedExceptions.clear()
137-
addException(Throwable::class.id)
138-
return
135+
addExceptionIfNeeded(Throwable::class.id)
139136
}
140137

141138
val methodIsUnderTestAndThrowsExplicitly = methodId == currentExecutable
@@ -148,13 +145,13 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
148145
return
149146
}
150147

151-
methodId.method.exceptionTypes.forEach { addException(it.id) }
148+
methodId.method.exceptionTypes.forEach { addExceptionIfNeeded(it.id) }
152149
}
153150

154151
private fun newConstructorCall(constructorId: ConstructorId) {
155152
importIfNeeded(constructorId.classId)
156153
for (exception in constructorId.exceptions) {
157-
addException(exception)
154+
addExceptionIfNeeded(exception)
158155
}
159156
}
160157

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import org.utbot.framework.codegen.model.visitor.importUtilMethodDependencies
2626
import org.utbot.framework.plugin.api.MethodId
2727
import org.utbot.framework.plugin.api.UtMethod
2828
import org.utbot.framework.plugin.api.UtTestCase
29-
import org.utbot.framework.util.description
29+
import org.utbot.framework.plugin.api.util.description
3030
import kotlin.reflect.KClass
3131

3232
internal class CgTestClassConstructor(val context: CgContext) :

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

-4
This file was deleted.

utbot-framework/src/test/kotlin/org/utbot/framework/codegen/BaseTestCodeGeneratorPipeline.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import org.utbot.framework.plugin.api.UtMethod
1616
import org.utbot.framework.plugin.api.UtTestCase
1717
import org.utbot.framework.plugin.api.util.UtContext
1818
import org.utbot.framework.plugin.api.util.withUtContext
19-
import org.utbot.framework.util.description
19+
import org.utbot.framework.plugin.api.util.description
2020
import kotlin.reflect.KClass
2121
import mu.KotlinLogging
2222
import org.junit.jupiter.api.Assertions.assertTrue

utbot-summary/src/main/kotlin/org/utbot/summary/TagGenerator.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.utbot.framework.plugin.api.UtImplicitlyThrownException
1010
import org.utbot.framework.plugin.api.UtOverflowFailure
1111
import org.utbot.framework.plugin.api.UtTestCase
1212
import org.utbot.framework.plugin.api.UtTimeoutException
13+
import org.utbot.framework.plugin.api.util.isCheckedException
1314
import org.utbot.summary.UtSummarySettings.MIN_NUMBER_OF_EXECUTIONS_FOR_CLUSTERING
1415
import org.utbot.summary.clustering.MatrixUniqueness
1516
import org.utbot.summary.clustering.SplitSteps
@@ -147,8 +148,8 @@ enum class ClusterKind {
147148

148149
private fun UtExecutionResult.clusterKind() = when (this) {
149150
is UtExecutionSuccess -> ClusterKind.SUCCESSFUL_EXECUTIONS
150-
is UtImplicitlyThrownException -> if (this.isCheckedException) ClusterKind.CHECKED_EXCEPTIONS else ClusterKind.ERROR_SUITE
151-
is UtExplicitlyThrownException -> if (this.isCheckedException) ClusterKind.CHECKED_EXCEPTIONS else ClusterKind.EXPLICITLY_THROWN_UNCHECKED_EXCEPTIONS
151+
is UtImplicitlyThrownException -> if (this.exception.isCheckedException) ClusterKind.CHECKED_EXCEPTIONS else ClusterKind.ERROR_SUITE
152+
is UtExplicitlyThrownException -> if (this.exception.isCheckedException) ClusterKind.CHECKED_EXCEPTIONS else ClusterKind.EXPLICITLY_THROWN_UNCHECKED_EXCEPTIONS
152153
is UtOverflowFailure -> ClusterKind.OVERFLOWS
153154
is UtTimeoutException -> ClusterKind.TIMEOUTS
154155
is UtConcreteExecutionFailure -> ClusterKind.CRASH_SUITE

0 commit comments

Comments
 (0)