Skip to content

Commit df651d2

Browse files
committed
wip
1 parent 5eac028 commit df651d2

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/assemble/AssembleModelGenerator.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ class AssembleModelGenerator(private val basePackageName: String) {
463463

464464
return allModificatorsOfClass
465465
.mapNotNull { (fieldId, possibleModificators) ->
466-
chooseModificator(fieldId, possibleModificators)?.let { fieldId to it }
466+
chooseModificator(classId, fieldId, possibleModificators)?.let { fieldId to it }
467467
}
468468
.toMap()
469469
}
@@ -474,12 +474,13 @@ class AssembleModelGenerator(private val basePackageName: String) {
474474
* Note: direct accessor is more preferred than setter.
475475
*/
476476
private fun chooseModificator(
477+
classId: ClassId,
477478
fieldId: FieldId,
478479
settersAndDirectAccessors: Set<StatementId>,
479480
): StatementId? {
480481
val directAccessors = settersAndDirectAccessors
481482
.filterIsInstance<DirectFieldAccessId>()
482-
.filter {it.fieldId.isAccessibleFrom(basePackageName) }
483+
.filter {it.fieldId.isAccessibleFrom(basePackageName, classId) }
483484

484485
if (directAccessors.any()) {
485486
return directAccessors.singleOrNull()

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/access/CgCallableAccessManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
325325

326326
private fun FieldId.accessSuitability(accessor: CgExpression?): FieldAccessorSuitability {
327327
// Check field accessibility.
328-
if (!canBeReadFrom(context)) {
328+
if (accessor != null && !canBeReadFrom(context, accessor.type)) {
329329
return ReflectionOnly
330330
}
331331

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/access/CgFieldStateManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ internal class CgFieldStateManagerImpl(val context: CgContext)
180180
for ((index, fieldPathElement) in path.withIndex()) {
181181
when (fieldPathElement) {
182182
is FieldAccess -> {
183-
if (!fieldPathElement.field.canBeReadFrom(context)) {
183+
if (!fieldPathElement.field.canBeReadFrom(context, curType)) {
184184
lastAccessibleIndex = index - 1
185185
break
186186
}
@@ -246,7 +246,7 @@ internal class CgFieldStateManagerImpl(val context: CgContext)
246246

247247
private fun variableForStaticFieldState(owner: ClassId, fieldPath: FieldPath, customName: String?): CgVariable {
248248
val firstField = (fieldPath.elements.first() as FieldAccess).field
249-
val firstAccessor = if (owner.isAccessibleFrom(testClassPackageName) && firstField.canBeReadFrom(context)) {
249+
val firstAccessor = if (owner.isAccessibleFrom(testClassPackageName) && firstField.canBeReadFrom(context, owner)) {
250250
owner[firstField]
251251
} else {
252252
// TODO: there is a function getClassOf() for these purposes, but it is not accessible from here for now

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getN
141141
import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getStatementConstructorBy
142142
import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getTestFrameworkManagerBy
143143
import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getVariableConstructorBy
144+
import org.utbot.framework.codegen.util.isAccessibleFrom
144145
import org.utbot.framework.plugin.api.UtExecutionResult
145146
import org.utbot.framework.plugin.api.UtOverflowFailure
146147
import org.utbot.framework.plugin.api.UtStreamConsumingFailure
@@ -236,7 +237,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
236237
val accessibleStaticFields = statics.accessibleFields()
237238
for ((field, _) in accessibleStaticFields) {
238239
val declaringClass = field.declaringClass
239-
val fieldAccessible = field.canBeReadFrom(context)
240+
val fieldAccessible = field.canBeReadFrom(context, field.declaringClass)
240241

241242
// prevValue is nullable if not accessible because of getStaticFieldValue(..) : Any?
242243
val prevValue = newVar(
@@ -261,7 +262,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
261262
val accessibleStaticFields = statics.accessibleFields()
262263
for ((field, model) in accessibleStaticFields) {
263264
val declaringClass = field.declaringClass
264-
val fieldAccessible = field.canBeSetFrom(context)
265+
val fieldAccessible = field.canBeSetFrom(context, field.declaringClass)
265266

266267
val fieldValue = if (isParametrized) {
267268
currentMethodParameters[CgParameterKind.Statics(model)]
@@ -282,7 +283,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
282283

283284
protected fun recoverStaticFields() {
284285
for ((field, prevValue) in prevStaticFieldValues.accessibleFields()) {
285-
if (field.canBeSetFrom(context)) {
286+
if (field.canBeSetFrom(context, field.declaringClass)) {
286287
field.declaringClass[field] `=` prevValue
287288
} else {
288289
val declaringClass = getClassOf(field.declaringClass)
@@ -1125,10 +1126,11 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
11251126
private fun FieldId.getAccessExpression(variable: CgVariable): CgExpression =
11261127
// Can directly access field only if it is declared in variable class (or in its ancestors)
11271128
// and is accessible from current package
1128-
if (variable.type.hasField(this)
1129+
if (variable.type.hasField(this) && canBeReadFrom(context, variable.type)
11291130
//TODO: think about moving variable type checks into [isAccessibleFrom] after contest
1130-
&& (!isPackagePrivate || variable.type.packageName == context.testClassPackageName)
1131-
&& canBeReadFrom(context)
1131+
/* && (!isPackagePrivate || variable.type.packageName == context.testClassPackageName)*/
1132+
1133+
/* && ((field.isPublic || field.isPrivate || field.isProtected) || variable.type.packageName == context.testClassPackageName)*/
11321134
) {
11331135
if (jField.isStatic) CgStaticFieldAccess(this) else CgFieldAccess(variable, this)
11341136
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ open class CgVariableConstructor(val context: CgContext) :
192192
// byteBuffer is field of type ByteBuffer and upper line is incorrect
193193
val canFieldBeDirectlySetByVariableAndFieldTypeRestrictions =
194194
fieldFromVariableSpecifiedType != null && fieldFromVariableSpecifiedType.type.id == variableForField.type
195-
if (canFieldBeDirectlySetByVariableAndFieldTypeRestrictions && fieldId.canBeSetFrom(context)) {
195+
if (canFieldBeDirectlySetByVariableAndFieldTypeRestrictions && fieldId.canBeSetFrom(context, obj.type)) {
196196
// TODO: check if it is correct to use declaringClass of a field here
197197
val fieldAccess = if (field.isStatic) CgStaticFieldAccess(fieldId) else CgFieldAccess(obj, fieldId)
198198
fieldAccess `=` variableForField

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/util/FieldIdUtil.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.framework.codegen.util
22

33
import org.utbot.framework.codegen.domain.context.CgContext
4+
import org.utbot.framework.plugin.api.ClassId
45
import org.utbot.framework.plugin.api.CodegenLanguage
56
import org.utbot.framework.plugin.api.FieldId
67
import org.utbot.framework.plugin.api.MethodId
@@ -18,9 +19,10 @@ import org.utbot.framework.plugin.api.util.voidClassId
1819
*
1920
* @param context context in which code is generated (it is needed because the method needs to know package and language)
2021
*/
21-
fun FieldId.isAccessibleFrom(packageName: String): Boolean {
22+
fun FieldId.isAccessibleFrom(packageName: String, classId: ClassId): Boolean {
2223
val isClassAccessible = declaringClass.isAccessibleFrom(packageName)
23-
val isAccessibleByVisibility = isPublic || (declaringClass.packageName == packageName && (isPackagePrivate || isProtected))
24+
val isAccessibleByVisibility =
25+
isPublic || (classId.packageName == packageName && declaringClass.packageName == packageName && (isPackagePrivate || isProtected))
2426
val isAccessibleFromPackageByModifiers = isAccessibleByVisibility && !isSynthetic
2527

2628
return isClassAccessible && isAccessibleFromPackageByModifiers
@@ -32,14 +34,14 @@ private fun FieldId.canBeReadViaGetterFrom(context: CgContext): Boolean =
3234
/**
3335
* Returns whether you can read field's value without reflection
3436
*/
35-
internal infix fun FieldId.canBeReadFrom(context: CgContext): Boolean {
37+
internal fun FieldId.canBeReadFrom(context: CgContext, classId: ClassId): Boolean {
3638
if (context.codegenLanguage == CodegenLanguage.KOTLIN) {
3739
// Kotlin will allow direct field access for non-static fields with accessible getter
3840
if (!isStatic && canBeReadViaGetterFrom(context))
3941
return true
4042
}
4143

42-
return isAccessibleFrom(context.testClassPackageName)
44+
return isAccessibleFrom(context.testClassPackageName, classId)
4345
}
4446

4547
private fun FieldId.canBeSetViaSetterFrom(context: CgContext): Boolean =
@@ -48,16 +50,16 @@ private fun FieldId.canBeSetViaSetterFrom(context: CgContext): Boolean =
4850
/**
4951
* Whether or not a field can be set without reflection
5052
*/
51-
internal fun FieldId.canBeSetFrom(context: CgContext): Boolean {
53+
internal fun FieldId.canBeSetFrom(context: CgContext, cls: ClassId): Boolean {
5254
if (context.codegenLanguage == CodegenLanguage.KOTLIN) {
5355
// Kotlin will allow direct write access if both getter and setter is defined
5456
// !isAccessibleFrom(context) is important here because above rule applies to final fields only if they are not accessible in Java terms
55-
if (!isAccessibleFrom(context.testClassPackageName) && !isStatic && canBeReadViaGetterFrom(context) && canBeSetViaSetterFrom(context)) {
57+
if (!isAccessibleFrom(context.testClassPackageName, cls) && !isStatic && canBeReadViaGetterFrom(context) && canBeSetViaSetterFrom(context)) {
5658
return true
5759
}
5860
}
5961

60-
return isAccessibleFrom(context.testClassPackageName) && !isFinal
62+
return isAccessibleFrom(context.testClassPackageName, cls) && !isFinal
6163
}
6264

6365
/**

0 commit comments

Comments
 (0)