Skip to content

Commit 9e96a9a

Browse files
committed
Fix rendering of auxiliary classes
1 parent 5290278 commit 9e96a9a

File tree

5 files changed

+126
-114
lines changed

5 files changed

+126
-114
lines changed

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

Lines changed: 81 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ internal abstract class UtilMethodProvider(val utilClassId: ClassId) {
5050
getArrayLengthMethodId,
5151
buildStaticLambdaMethodId,
5252
buildLambdaMethodId,
53+
getLookupInMethodId,
54+
getLambdaCapturedArgumentTypesMethodId,
55+
getLambdaCapturedArgumentValuesMethodId,
56+
getInstantiatedMethodTypeMethodId,
57+
getLambdaMethodMethodId,
5358
getSingleAbstractMethodMethodId
5459
)
5560

@@ -160,7 +165,82 @@ internal abstract class UtilMethodProvider(val utilClassId: ClassId) {
160165
arguments = arrayOf(objectClassId)
161166
)
162167

163-
// TODO: buildStaticLambdaMethodId, buildLambdaMethodId, getSingleAbstractMethodMethodId
168+
val buildStaticLambdaMethodId: MethodId
169+
get() = utilClassId.utilMethodId(
170+
name = "buildStaticLambda",
171+
returnType = objectClassId,
172+
arguments = arrayOf(
173+
classClassId,
174+
classClassId,
175+
stringClassId,
176+
arrayTypeOf(capturedArgumentClassId)
177+
)
178+
)
179+
180+
val buildLambdaMethodId: MethodId
181+
get() = utilClassId.utilMethodId(
182+
name = "buildLambda",
183+
returnType = objectClassId,
184+
arguments = arrayOf(
185+
classClassId,
186+
classClassId,
187+
stringClassId,
188+
objectClassId,
189+
arrayTypeOf(capturedArgumentClassId)
190+
)
191+
)
192+
193+
val getLookupInMethodId: MethodId
194+
get() = utilClassId.utilMethodId(
195+
name = "getLookupIn",
196+
returnType = MethodHandles.Lookup::class.id,
197+
arguments = arrayOf(classClassId)
198+
)
199+
200+
val getLambdaCapturedArgumentTypesMethodId: MethodId
201+
get() = utilClassId.utilMethodId(
202+
name = "getLambdaCapturedArgumentTypes",
203+
returnType = arrayTypeOf(classClassId),
204+
arguments = arrayOf(arrayTypeOf(capturedArgumentClassId))
205+
)
206+
207+
val getLambdaCapturedArgumentValuesMethodId: MethodId
208+
get() = utilClassId.utilMethodId(
209+
name = "getLambdaCapturedArgumentValues",
210+
returnType = objectArrayClassId,
211+
arguments = arrayOf(arrayTypeOf(capturedArgumentClassId))
212+
)
213+
214+
val getInstantiatedMethodTypeMethodId: MethodId
215+
get() = utilClassId.utilMethodId(
216+
name = "getInstantiatedMethodType",
217+
returnType = MethodType::class.id,
218+
arguments = arrayOf(Method::class.id, arrayTypeOf(classClassId))
219+
)
220+
221+
val getLambdaMethodMethodId: MethodId
222+
get() = utilClassId.utilMethodId(
223+
name = "getLambdaMethod",
224+
returnType = Method::class.id,
225+
arguments = arrayOf(classClassId, stringClassId)
226+
)
227+
228+
val getSingleAbstractMethodMethodId: MethodId
229+
get() = utilClassId.utilMethodId(
230+
name = "getSingleAbstractMethod",
231+
returnType = java.lang.reflect.Method::class.id,
232+
arguments = arrayOf(classClassId)
233+
)
234+
235+
val capturedArgumentClassId: BuiltinClassId
236+
get() = BuiltinClassId(
237+
name = "${utilClassId.name}\$CapturedArgument",
238+
canonicalName = "${utilClassId.name}.CapturedArgument",
239+
simpleName = "CapturedArgument"
240+
)
241+
242+
val capturedArgumentConstructorId: BuiltinConstructorId
243+
get() = builtinConstructorId(capturedArgumentClassId, classClassId, objectClassId)
164244
}
165245

166246
/**
@@ -195,74 +275,6 @@ internal val utUtilsClassId: ClassId
195275
isFinal = true
196276
)
197277

198-
internal val ClassId.buildStaticLambdaMethodId: MethodId
199-
get() = utilMethodId(
200-
name = "buildStaticLambda",
201-
returnType = objectClassId,
202-
arguments = arrayOf(
203-
classClassId,
204-
classClassId,
205-
stringClassId,
206-
arrayTypeOf(capturedArgumentClassId)
207-
)
208-
)
209-
210-
internal val ClassId.buildLambdaMethodId: MethodId
211-
get() = utilMethodId(
212-
name = "buildLambda",
213-
returnType = objectClassId,
214-
arguments = arrayOf(
215-
classClassId,
216-
classClassId,
217-
stringClassId,
218-
objectClassId,
219-
arrayTypeOf(capturedArgumentClassId)
220-
)
221-
)
222-
223-
internal val ClassId.getLookupInMethodId: MethodId
224-
get() = utilMethodId(
225-
name = "getLookupIn",
226-
returnType = MethodHandles.Lookup::class.id,
227-
arguments = arrayOf(classClassId)
228-
)
229-
230-
internal val ClassId.getLambdaCapturedArgumentTypesMethodId: MethodId
231-
get() = utilMethodId(
232-
name = "getLambdaCapturedArgumentTypes",
233-
returnType = arrayTypeOf(classClassId),
234-
arguments = arrayOf(arrayTypeOf(capturedArgumentClassId))
235-
)
236-
237-
internal val ClassId.getLambdaCapturedArgumentValuesMethodId: MethodId
238-
get() = utilMethodId(
239-
name = "getLambdaCapturedArgumentValues",
240-
returnType = objectArrayClassId,
241-
arguments = arrayOf(arrayTypeOf(capturedArgumentClassId))
242-
)
243-
244-
internal val ClassId.getInstantiatedMethodTypeMethodId: MethodId
245-
get() = utilMethodId(
246-
name = "getInstantiatedMethodType",
247-
returnType = MethodType::class.id,
248-
arguments = arrayOf(Method::class.id, arrayTypeOf(classClassId))
249-
)
250-
251-
internal val ClassId.getLambdaMethodMethodId: MethodId
252-
get() = utilMethodId(
253-
name = "getLambdaMethod",
254-
returnType = Method::class.id,
255-
arguments = arrayOf(classClassId, stringClassId)
256-
)
257-
258-
259-
internal val ClassId.getSingleAbstractMethodMethodId: MethodId
260-
get() = utilMethodId(
261-
name = "getSingleAbstractMethod",
262-
returnType = java.lang.reflect.Method::class.id,
263-
arguments = arrayOf(classClassId)
264-
)
265-
266278
/**
267279
* [MethodId] for [AutoCloseable.close].
268280
*/
@@ -298,17 +310,3 @@ internal val ClassId.closeMethodIdOrNull: MethodId?
298310
this is BuiltinClassId -> null
299311
else -> (jClass as? AutoCloseable)?.let { closeMethodId }
300312
}
301-
302-
// TODO: when util class branch is merged into main,
303-
// we should move this method into UtilMethodProvider,
304-
// so that class CapturedArgument is declared either in util class or in test class,
305-
// but for now it can only be declared in the test class
306-
val ClassId.capturedArgumentClassId: BuiltinClassId
307-
get() = BuiltinClassId(
308-
name = "${this.name}\$CapturedArgument",
309-
canonicalName = "${this.name}.CapturedArgument",
310-
simpleName = "CapturedArgument"
311-
)
312-
313-
val ClassId.capturedArgumentConstructorId: BuiltinConstructorId
314-
get() = builtinConstructorId(capturedArgumentClassId, classClassId, objectClassId)

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.utbot.framework.codegen.model.constructor.tree
22

3-
import org.utbot.framework.codegen.model.constructor.builtin.capturedArgumentConstructorId
43
import org.utbot.framework.codegen.model.constructor.builtin.forName
54
import org.utbot.framework.codegen.model.constructor.builtin.setArrayElement
65
import org.utbot.framework.codegen.model.constructor.context.CgContext
@@ -50,14 +49,12 @@ import org.utbot.framework.plugin.api.UtPrimitiveModel
5049
import org.utbot.framework.plugin.api.UtReferenceModel
5150
import org.utbot.framework.plugin.api.UtVoidModel
5251
import org.utbot.framework.plugin.api.util.defaultValueModel
53-
import org.utbot.framework.plugin.api.util.executableId
5452
import org.utbot.framework.plugin.api.util.jField
5553
import org.utbot.framework.plugin.api.util.findFieldByIdOrNull
5654
import org.utbot.framework.plugin.api.util.id
5755
import org.utbot.framework.plugin.api.util.intClassId
5856
import org.utbot.framework.plugin.api.util.isArray
5957
import org.utbot.framework.plugin.api.util.isPrimitiveWrapperOrString
60-
import org.utbot.framework.plugin.api.util.jClass
6158
import org.utbot.framework.plugin.api.util.stringClassId
6259
import org.utbot.framework.plugin.api.util.wrapperByPrimitive
6360
import java.lang.reflect.Field
@@ -133,7 +130,7 @@ internal class CgVariableConstructor(val context: CgContext) :
133130
}
134131

135132
private fun constructStaticLambda(model: UtLambdaModel, capturedValues: List<UtModel>): CgMethodCall {
136-
val capturedArguments = capturedValues.map { outerMostTestClass.capturedArgumentConstructorId(getClassOf(it.classId), getOrCreateVariable(it)) }
133+
val capturedArguments = capturedValues.map { utilMethodProvider.capturedArgumentConstructorId(getClassOf(it.classId), getOrCreateVariable(it)) }
137134
return testClassThisInstance[buildStaticLambda](
138135
getClassOf(model.samType),
139136
getClassOf(model.declaringClass),
@@ -147,7 +144,7 @@ internal class CgVariableConstructor(val context: CgContext) :
147144
val capturedThisInstance = getOrCreateVariable(capturedValues.first())
148145
val capturedArguments = capturedValues
149146
.subList(1, capturedValues.size)
150-
.map { outerMostTestClass.capturedArgumentConstructorId(getClassOf(it.classId), getOrCreateVariable(it)) }
147+
.map { utilMethodProvider.capturedArgumentConstructorId(getClassOf(it.classId), getOrCreateVariable(it)) }
151148
return testClassThisInstance[buildLambda](
152149
getClassOf(model.samType),
153150
getClassOf(model.declaringClass),

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.utbot.framework.codegen.model.constructor.tree.TestsGenerationReport
88
import org.utbot.framework.codegen.model.util.CgExceptionHandler
99
import org.utbot.framework.codegen.model.visitor.CgRendererContext
1010
import org.utbot.framework.codegen.model.visitor.CgVisitor
11+
import org.utbot.framework.codegen.model.visitor.auxiliaryClassTextById
1112
import org.utbot.framework.codegen.model.visitor.utilMethodTextById
1213
import org.utbot.framework.plugin.api.BuiltinClassId
1314
import org.utbot.framework.plugin.api.ClassId
@@ -244,9 +245,22 @@ data class CgExecutableUnderTestCluster(
244245
override val content: List<CgRegion<CgMethod>>
245246
) : CgRegion<CgRegion<CgMethod>>()
246247

247-
sealed class CgUtilEntity : CgElement
248+
sealed class CgUtilEntity : CgElement {
249+
internal abstract fun getText(rendererContext: CgRendererContext): String
250+
}
248251

249-
data class CgAuxiliaryClass(val id: ClassId) : CgUtilEntity()
252+
data class CgAuxiliaryClass(val id: ClassId) : CgUtilEntity() {
253+
override fun getText(rendererContext: CgRendererContext): String {
254+
// we should not throw an exception on failure here,
255+
// because this function is used during rendering and
256+
// exceptions can crash rendering, so we use an empty string if the text is not found
257+
return with(rendererContext) {
258+
rendererContext.utilMethodProvider
259+
.auxiliaryClassTextById(id, codegenLanguage)
260+
.getOrDefault("")
261+
}
262+
}
263+
}
250264

251265
/**
252266
* This class does not inherit from [CgMethod], because it only needs an [id],
@@ -257,7 +271,7 @@ data class CgAuxiliaryClass(val id: ClassId) : CgUtilEntity()
257271
* @property id identifier of the util method.
258272
*/
259273
data class CgUtilMethod(val id: MethodId) : CgUtilEntity() {
260-
internal fun getText(rendererContext: CgRendererContext): String {
274+
override fun getText(rendererContext: CgRendererContext): String {
261275
// we should not throw an exception on failure here,
262276
// because this function is used during rendering and
263277
// exceptions can crash rendering, so we use an empty string if the text is not found

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,8 @@ internal abstract class CgAbstractRenderer(
229229
}
230230

231231
override fun visit(element: CgAuxiliaryClass) {
232-
context.generatedClass
233-
.auxiliaryClassById(element.id, context)
234-
.split("\n")
232+
val auxiliaryClassText = element.getText(context)
233+
auxiliaryClassText.split("\n")
235234
.forEach { line -> println(line) }
236235
}
237236

0 commit comments

Comments
 (0)