Skip to content

Support hidden fields in code generation #763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ val floatWrapperClassId = java.lang.Float::class.id
val doubleWrapperClassId = java.lang.Double::class.id

val classClassId = java.lang.Class::class.id
val fieldClassId = java.lang.reflect.Field::class.id
val methodClassId = java.lang.reflect.Method::class.id
val constructorClassId = java.lang.reflect.Constructor::class.id

// We consider void wrapper as primitive wrapper
// because voidClassId is considered primitive here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import org.utbot.testcheckers.eq
internal class HiddenFieldAccessModifiersTest : UtValueTestCaseChecker(testClass = HiddenFieldAccessModifiersExample::class) {
@Test
fun testCheckSuperFieldEqualsOne() {
// TODO: currently, codegen can't handle tests with field hiding (see #646)
withEnabledTestingCodeGeneration(testCodeGeneration = false) {
withEnabledTestingCodeGeneration(testCodeGeneration = true) {
check(
HiddenFieldAccessModifiersExample::checkSuperFieldEqualsOne,
eq(3),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ internal class HiddenFieldExampleTest : UtValueTestCaseChecker(testClass = Hidde

@Test
fun testCheckSuccField() {
// TODO: currently, codegen can't handle tests with field hiding (see #646)
withEnabledTestingCodeGeneration(testCodeGeneration = false) {
withEnabledTestingCodeGeneration(testCodeGeneration = true) {
check(
HiddenFieldExample::checkSuccField,
eq(5),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import java.lang.reflect.InvocationTargetException

internal val reflectionBuiltins: Set<MethodId>
get() = setOf(
setAccessible, invoke, newInstance, get, forName,
setAccessible, invoke, newInstance, getMethodId, forName,
getDeclaredMethod, getDeclaredConstructor, allocateInstance,
getClass, getDeclaredField, isEnumConstant, getFieldName,
equals, getSuperclass, set, newArrayInstance,
equals, getSuperclass, setMethodId, newArrayInstance,
setArrayElement, getArrayElement, getTargetException,
)

Expand All @@ -49,7 +49,7 @@ internal val newInstance = methodId(
arguments = arrayOf(Array<Any>::class.id)
)

internal val get = methodId(
internal val getMethodId = methodId(
classId = Field::class.id,
name = "get",
returnType = objectClassId,
Expand Down Expand Up @@ -132,7 +132,7 @@ internal val getSuperclass = methodId(
returnType = Class::class.id
)

internal val set = methodId(
internal val setMethodId = methodId(
classId = Field::class.id,
name = "set",
returnType = voidClassId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal abstract class UtilMethodProvider(val utilClassId: ClassId) {
get() = utilClassId.utilMethodId(
name = "setField",
returnType = voidClassId,
arguments = arrayOf(objectClassId, stringClassId, objectClassId)
arguments = arrayOf(objectClassId, stringClassId, stringClassId, objectClassId)
)

val setStaticFieldMethodId: MethodId
Expand All @@ -83,7 +83,7 @@ internal abstract class UtilMethodProvider(val utilClassId: ClassId) {
get() = utilClassId.utilMethodId(
name = "getFieldValue",
returnType = objectClassId,
arguments = arrayOf(objectClassId, stringClassId)
arguments = arrayOf(objectClassId, stringClassId, stringClassId)
)

val getStaticFieldValueMethodId: MethodId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ internal interface CgContextOwner {
// java.lang.reflect.Executable is a superclass of both of these types.
var declaredExecutableRefs: PersistentMap<ExecutableId, CgVariable>

// Variables of java.lang.reflect.Field type declared in the current name scope
var declaredFieldRefs: PersistentMap<FieldId, CgVariable>

// generated this instance for method under test
var thisInstance: CgValue?

Expand Down Expand Up @@ -308,6 +311,7 @@ internal interface CgContextOwner {
val prevVariableNames = existingVariableNames
val prevDeclaredClassRefs = declaredClassRefs
val prevDeclaredExecutableRefs = declaredExecutableRefs
val prevDeclaredFieldRefs = declaredFieldRefs
val prevValueByModel = IdentityHashMap(valueByModel)
val prevValueByModelId = valueByModelId.toMutableMap()
return try {
Expand All @@ -316,6 +320,7 @@ internal interface CgContextOwner {
existingVariableNames = prevVariableNames
declaredClassRefs = prevDeclaredClassRefs
declaredExecutableRefs = prevDeclaredExecutableRefs
declaredFieldRefs = prevDeclaredFieldRefs
valueByModel = prevValueByModel
valueByModelId = prevValueByModelId
}
Expand Down Expand Up @@ -416,6 +421,7 @@ internal data class CgContext(
override var existingVariableNames: PersistentSet<String> = persistentSetOf(),
override var declaredClassRefs: PersistentMap<ClassId, CgVariable> = persistentMapOf(),
override var declaredExecutableRefs: PersistentMap<ExecutableId, CgVariable> = persistentMapOf(),
override var declaredFieldRefs: PersistentMap<FieldId, CgVariable> = persistentMapOf(),
override var thisInstance: CgValue? = null,
override val methodArguments: MutableList<CgValue> = mutableListOf(),
override val codeGenerationErrors: MutableMap<CgMethodTestSet, MutableMap<String, Int>> = mutableMapOf(),
Expand Down
Loading