Skip to content

Commit 3bd66fc

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

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import kotlinx.collections.immutable.persistentListOf
5555
import kotlinx.collections.immutable.persistentMapOf
5656
import kotlinx.collections.immutable.persistentSetOf
5757
import org.utbot.framework.codegen.model.constructor.builtin.streamsDeepEqualsMethodId
58+
import org.utbot.framework.plugin.api.util.isSubtypeOf
5859

5960
/**
6061
* Interface for all code generation context aware entities
@@ -232,10 +233,18 @@ internal interface CgContextOwner {
232233
currentExecutable = method.callable.executableId
233234
}
234235

235-
fun addException(exception: ClassId) {
236-
if (collectedExceptions.add(exception)) {
237-
importIfNeeded(exception)
236+
fun addExceptionIfNeeded(exception: ClassId) {
237+
for (existingException in collectedExceptions) {
238+
if (exception isSubtypeOf existingException) {
239+
return
240+
}
238241
}
242+
243+
collectedExceptions
244+
.removeIf { existingException -> existingException isSubtypeOf exception }
245+
246+
collectedExceptions.add(exception)
247+
importIfNeeded(exception)
239248
}
240249

241250
fun addAnnotation(annotation: CgAnnotation) {
@@ -416,9 +425,9 @@ internal data class CgContext(
416425
val simpleName = testClassCustomName ?: "${createTestClassName(classUnderTest.name)}Test"
417426
val name = "$packagePrefix$simpleName"
418427
BuiltinClassId(
419-
name = name,
420-
canonicalName = name,
421-
simpleName = simpleName
428+
name = name,
429+
canonicalName = name,
430+
simpleName = simpleName
422431
)
423432
}
424433

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

Lines changed: 5 additions & 8 deletions
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

0 commit comments

Comments
 (0)