Skip to content

Commit 75a3b67

Browse files
committed
Fix obtaining exception types of util methods
1 parent 49d1bc5 commit 75a3b67

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/builtin/UtilMethodBuiltins.kt

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -147,33 +147,6 @@ internal abstract class UtilMethodProvider(val utilClassId: ClassId) {
147147
returnType = intClassId,
148148
arguments = arrayOf(objectClassId)
149149
)
150-
151-
//WARN: if you make changes in the following sets of exceptions,
152-
//don't forget to change them in hardcoded [UtilMethods] as well
153-
internal fun findExceptionTypesOf(methodId: MethodId): Set<ClassId> {
154-
if (methodId !in utilMethodIds) return emptySet()
155-
156-
with(this) {
157-
return when (methodId) {
158-
getEnumConstantByNameMethodId -> setOf(java.lang.IllegalAccessException::class.id)
159-
getStaticFieldValueMethodId,
160-
getFieldValueMethodId,
161-
setStaticFieldMethodId,
162-
setFieldMethodId -> setOf(java.lang.IllegalAccessException::class.id, java.lang.NoSuchFieldException::class.id)
163-
createInstanceMethodId -> setOf(Exception::class.id)
164-
getUnsafeInstanceMethodId -> setOf(java.lang.ClassNotFoundException::class.id, java.lang.NoSuchFieldException::class.id, java.lang.IllegalAccessException::class.id)
165-
createArrayMethodId -> setOf(java.lang.ClassNotFoundException::class.id)
166-
deepEqualsMethodId,
167-
arraysDeepEqualsMethodId,
168-
iterablesDeepEqualsMethodId,
169-
streamsDeepEqualsMethodId,
170-
mapsDeepEqualsMethodId,
171-
hasCustomEqualsMethodId,
172-
getArrayLengthMethodId -> emptySet()
173-
else -> error("Unknown util method $this")
174-
}
175-
}
176-
}
177150
}
178151

179152
/**

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import org.utbot.framework.codegen.model.util.at
3636
import org.utbot.framework.codegen.model.util.isAccessibleFrom
3737
import org.utbot.framework.codegen.model.util.nullLiteral
3838
import org.utbot.framework.codegen.model.util.resolve
39+
import org.utbot.framework.plugin.api.BuiltinMethodId
3940
import org.utbot.framework.plugin.api.ClassId
4041
import org.utbot.framework.plugin.api.ConstructorId
4142
import org.utbot.framework.plugin.api.ExecutableId
@@ -111,9 +112,8 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
111112

112113
//Builtin methods does not have jClass, so [methodId.method] will crash on it,
113114
//so we need to collect required exceptions manually from source codes
114-
if (isUtil(methodId)) {
115-
utilMethodProvider
116-
.findExceptionTypesOf(methodId)
115+
if (methodId is BuiltinMethodId) {
116+
findExceptionTypesOf(methodId)
117117
.forEach { addExceptionIfNeeded(it) }
118118
return
119119
}
@@ -364,4 +364,35 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
364364

365365
return argumentsArrayVariable
366366
}
367+
368+
//WARN: if you make changes in the following sets of exceptions,
369+
//don't forget to change them in hardcoded [UtilMethods] as well
370+
private fun findExceptionTypesOf(methodId: MethodId): Set<ClassId> {
371+
// TODO: at the moment we treat BuiltinMethodIds that are not util method ids
372+
// as if they have no exceptions. This should be fixed by storing exception types in BuiltinMethodId
373+
// or allowing us to access actual java.lang.Class for classes from mockito and other libraries
374+
// (this could be possibly solved by using user project's class loaders in UtContext)
375+
if (methodId !in utilMethodProvider.utilMethodIds) return emptySet()
376+
377+
with(utilMethodProvider) {
378+
return when (methodId) {
379+
getEnumConstantByNameMethodId -> setOf(java.lang.IllegalAccessException::class.id)
380+
getStaticFieldValueMethodId,
381+
getFieldValueMethodId,
382+
setStaticFieldMethodId,
383+
setFieldMethodId -> setOf(java.lang.IllegalAccessException::class.id, java.lang.NoSuchFieldException::class.id)
384+
createInstanceMethodId -> setOf(Exception::class.id)
385+
getUnsafeInstanceMethodId -> setOf(java.lang.ClassNotFoundException::class.id, java.lang.NoSuchFieldException::class.id, java.lang.IllegalAccessException::class.id)
386+
createArrayMethodId -> setOf(java.lang.ClassNotFoundException::class.id)
387+
deepEqualsMethodId,
388+
arraysDeepEqualsMethodId,
389+
iterablesDeepEqualsMethodId,
390+
streamsDeepEqualsMethodId,
391+
mapsDeepEqualsMethodId,
392+
hasCustomEqualsMethodId,
393+
getArrayLengthMethodId -> emptySet()
394+
else -> error("Unknown util method $this")
395+
}
396+
}
397+
}
367398
}

0 commit comments

Comments
 (0)