Skip to content

Removed enum constants from meaningful static fields #1561

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 1 commit into from
Dec 21, 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 @@ -405,6 +405,9 @@ val ClassId.isEnum: Boolean
val ClassId.isData: Boolean
get() = kClass.isData

val ClassId.enumConstants: List<Enum<*>>?
get() = jClass.enumConstants?.filterIsInstance<Enum<*>>()

fun ClassId.findFieldByIdOrNull(fieldId: FieldId): Field? {
if (isNotSubtypeOf(fieldId.declaringClass)) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.utbot.testcheckers.withPushingStateFromPathSelectorForConcrete
import org.utbot.testcheckers.withoutConcrete
import org.utbot.testing.DoNotCalculate
import org.utbot.testing.UtValueTestCaseChecker
import org.utbot.testing.ignoreExecutionsNumber
import org.utbot.testing.isException

class ClassWithEnumTest : UtValueTestCaseChecker(testClass = ClassWithEnum::class) {
Expand All @@ -36,7 +37,7 @@ class ClassWithEnumTest : UtValueTestCaseChecker(testClass = ClassWithEnum::clas
fun testDifficultIfBranch() {
check(
ClassWithEnum::useEnumInDifficultIf,
eq(2),
ignoreExecutionsNumber,
{ s, r -> s.equals("TRYIF", ignoreCase = true) && r == 1 },
{ s, r -> !s.equals("TRYIF", ignoreCase = true) && r == 2 },
)
Expand Down Expand Up @@ -168,7 +169,7 @@ class ClassWithEnumTest : UtValueTestCaseChecker(testClass = ClassWithEnum::clas
withPushingStateFromPathSelectorForConcrete {
check(
ClassWithEnum::implementingInterfaceEnumInDifficultBranch,
eq(2),
ignoreExecutionsNumber,
{ s, r -> s.equals("SUCCESS", ignoreCase = true) && r == 0 },
{ s, r -> !s.equals("SUCCESS", ignoreCase = true) && r == 2 },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import java.util.concurrent.ConcurrentHashMap
import kotlinx.collections.immutable.PersistentMap
import kotlinx.collections.immutable.persistentHashMapOf
import org.utbot.engine.types.OBJECT_TYPE
import org.utbot.framework.plugin.api.util.enumConstants

val JIdentityStmt.lines: String
get() = tags.joinToString { "$it" }
Expand Down Expand Up @@ -287,6 +288,9 @@ val Type.defaultSymValue: UtExpression
val SootField.fieldId: FieldId
get() = FieldId(declaringClass.id, name)

val SootField.isEnumConstant: Boolean
get() = name in declaringClass.id.enumConstants.orEmpty().map { enum -> enum.name }

val UtSort.defaultValue: UtExpression
get() = when (this) {
UtByteSort -> mkByte(0)
Expand Down
2 changes: 2 additions & 0 deletions utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,8 @@ class Traverser(
!Modifier.isSynthetic(field.modifiers) &&
// we don't want to set fields that cannot be set via reflection anyway
!field.fieldId.isInaccessibleViaReflection &&
// we should not manually set enum constants
!(field.declaringClass.isEnum && field.isEnumConstant) &&
// we don't want to set fields from library classes
!workaround(IGNORE_STATICS_FROM_TRUSTED_LIBRARIES) {
ignoreStaticsFromTrustedLibraries && field.declaringClass.isFromTrustedLibrary()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ boolean affectSystemStaticAndInitEnumFromItAndGetItFromEnumFun() {
enum OuterStaticUsageEnum {
A;

int y;
final int y;

OuterStaticUsageEnum() {
y = staticInt;
Expand All @@ -237,5 +237,11 @@ enum OuterStaticUsageEnum {
int getOuterStatic() {
return staticInt;
}


@Override
public String toString() {
return String.format("%s(y = %d)", name(), y);
}
}
}