Skip to content

Commit 063a637

Browse files
committed
Improve CgIsInstance processing
1 parent 4e52d21 commit 063a637

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,13 +1229,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12291229
if (containsReflectiveCall) {
12301230
currentTryBlock.catch(InvocationTargetException::class.java.id) { exception ->
12311231
testFrameworkManager.assertBoolean(
1232-
CgIsInstance(expectedErrorVariable, exception[getTargetException]())
1232+
expectedErrorVariable.isInstance(exception[getTargetException]())
12331233
)
12341234
}
12351235
} else {
12361236
currentTryBlock.catch(Throwable::class.java.id) { throwable ->
12371237
testFrameworkManager.assertBoolean(
1238-
CgIsInstance(expectedErrorVariable, throwable)
1238+
expectedErrorVariable.isInstance(throwable)
12391239
)
12401240
}
12411241
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/util/CgStatementConstructor.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ import org.utbot.framework.plugin.api.util.objectArrayClassId
5858
import org.utbot.framework.plugin.api.util.objectClassId
5959
import fj.data.Either
6060
import org.utbot.framework.codegen.model.tree.CgArrayInitializer
61+
import org.utbot.framework.codegen.model.tree.CgIsInstance
62+
import org.utbot.framework.plugin.api.util.classClassId
6163
import java.lang.reflect.Constructor
6264
import java.lang.reflect.Method
6365
import kotlin.reflect.KFunction
@@ -116,6 +118,8 @@ interface CgStatementConstructor {
116118
fun CgTryCatch.catch(exception: ClassId, init: (CgVariable) -> Unit): CgTryCatch
117119
fun CgTryCatch.finally(init: () -> Unit): CgTryCatch
118120

121+
fun CgExpression.isInstance(typeExpr: CgExpression): CgIsInstance
122+
119123
fun innerBlock(init: () -> Unit): CgInnerBlock
120124

121125
// fun CgTryCatchBuilder.statements(init: () -> Unit)
@@ -302,6 +306,14 @@ internal class CgStatementConstructorImpl(context: CgContext) :
302306
return this.copy(finally = finallyBlock)
303307
}
304308

309+
override fun CgExpression.isInstance(typeExpr: CgExpression): CgIsInstance {
310+
require(this.type == classClassId) {
311+
"isInstance method can be called on object with type $classClassId only, but actual type is ${this.type}"
312+
}
313+
314+
return CgIsInstance(this, typeExpr)
315+
}
316+
305317
override fun innerBlock(init: () -> Unit): CgInnerBlock =
306318
CgInnerBlock(block(init)).also {
307319
currentBlock += it

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,13 @@ class CgTypeCast(
538538
override val type: ClassId = targetType
539539
}
540540

541+
/**
542+
* Represents [java.lang.Class.isInstance] method.
543+
*/
541544
class CgIsInstance(
542-
val thisExpr: CgExpression,
543-
val typeExpr: CgExpression,
544-
) : CgExpression {
545+
val classExpression: CgExpression,
546+
val value: CgExpression,
547+
): CgExpression {
545548
override val type: ClassId = booleanClassId
546549
}
547550

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/visitor/CgAbstractRenderer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,9 @@ internal abstract class CgAbstractRenderer(val context: CgContext, val printer:
426426
// isInstance check
427427

428428
override fun visit(element: CgIsInstance) {
429-
element.thisExpr.accept(this)
429+
element.classExpression.accept(this)
430430
print(".isInstance(")
431-
element.typeExpr.accept(this)
431+
element.value.accept(this)
432432
print(")")
433433
}
434434

0 commit comments

Comments
 (0)