@@ -13,11 +13,8 @@ package org.jetbrains.kotlinx.lincheck.strategy.managed
13
13
import kotlinx.atomicfu.AtomicArray
14
14
import kotlinx.atomicfu.AtomicBooleanArray
15
15
import kotlinx.atomicfu.AtomicIntArray
16
- import org.jetbrains.kotlinx.lincheck.allDeclaredFieldWithSuperclasses
17
16
import org.jetbrains.kotlinx.lincheck.strategy.managed.AtomicReferenceMethodType.*
18
- import org.jetbrains.kotlinx.lincheck.strategy.managed.AtomicReferenceNames.AtomicReferenceOwnerWithName.*
19
- import org.jetbrains.kotlinx.lincheck.strategy.managed.AtomicReferenceNames.TraverseResult.*
20
- import java.lang.reflect.Modifier
17
+ import org.jetbrains.kotlinx.lincheck.strategy.managed.OwnerWithName.*
21
18
import java.util.*
22
19
import java.util.concurrent.atomic.AtomicIntegerArray
23
20
import java.util.concurrent.atomic.AtomicLongArray
@@ -37,16 +34,16 @@ internal object AtomicReferenceNames {
37
34
atomicReference : Any ,
38
35
parameters : Array <Any ?>
39
36
): AtomicReferenceMethodType {
40
- val receiverAndName = getAtomicReferenceReceiverAndName (testObject, atomicReference)
37
+ val receiverAndName = FieldSearchHelper .findFinalFieldWithOwner (testObject, atomicReference)
41
38
return if (receiverAndName != null ) {
42
39
if (isAtomicArrayIndexMethodCall(atomicReference, parameters)) {
43
40
when (receiverAndName) {
44
- is InstanceOwnerWithName -> InstanceFieldAtomicArrayMethod (receiverAndName.receiver , receiverAndName.fieldName, parameters[0 ] as Int )
41
+ is InstanceOwnerWithName -> InstanceFieldAtomicArrayMethod (receiverAndName.owner , receiverAndName.fieldName, parameters[0 ] as Int )
45
42
is StaticOwnerWithName -> StaticFieldAtomicArrayMethod (receiverAndName.clazz, receiverAndName.fieldName, parameters[0 ] as Int )
46
43
}
47
44
} else {
48
45
when (receiverAndName) {
49
- is InstanceOwnerWithName -> AtomicReferenceInstanceMethod (receiverAndName.receiver , receiverAndName.fieldName)
46
+ is InstanceOwnerWithName -> AtomicReferenceInstanceMethod (receiverAndName.owner , receiverAndName.fieldName)
50
47
is StaticOwnerWithName -> AtomicReferenceStaticMethod (receiverAndName.clazz, receiverAndName.fieldName)
51
48
}
52
49
}
@@ -69,70 +66,7 @@ internal object AtomicReferenceNames {
69
66
atomicReference is AtomicBooleanArray
70
67
}
71
68
72
- private fun getAtomicReferenceReceiverAndName (testObject : Any , reference : Any ): AtomicReferenceOwnerWithName ? =
73
- runCatching {
74
- val visitedObjects: MutableSet <Any > = Collections .newSetFromMap(IdentityHashMap ())
75
- return when (val result = findObjectField(testObject, reference, visitedObjects)) {
76
- is FieldName -> result.fieldName
77
- MultipleFieldsMatching , NotFound -> null
78
- }
79
- }.getOrElse { exception ->
80
- exception.printStackTrace()
81
- null
82
- }
83
-
84
- private sealed interface TraverseResult {
85
- data object NotFound : TraverseResult
86
- data class FieldName (val fieldName : AtomicReferenceOwnerWithName ) : TraverseResult
87
- data object MultipleFieldsMatching : TraverseResult
88
- }
89
-
90
- private fun findObjectField (testObject : Any? , value : Any , visitedObjects : MutableSet <Any >): TraverseResult {
91
- if (testObject == null ) return NotFound
92
- var fieldName: AtomicReferenceOwnerWithName ? = null
93
- // We take all the fields from the hierarchy.
94
- // If two or more fields match (===) the AtomicReference object, we fall back to the default behavior,
95
- // so there is no problem that we can receive some fields of the same name and the same type.
96
- for (field in testObject::class .java.allDeclaredFieldWithSuperclasses) {
97
- if (field.type.isPrimitive || ! field.trySetAccessible()) continue
98
- val fieldValue = field.get(testObject)
99
69
100
- if (fieldValue in visitedObjects) continue
101
- visitedObjects + = testObject
102
-
103
- if (fieldValue == = value) {
104
- if (fieldName != null ) return MultipleFieldsMatching
105
-
106
- fieldName = if (Modifier .isStatic(field.modifiers)) {
107
- StaticOwnerWithName (field.name, testObject::class .java)
108
- } else {
109
- InstanceOwnerWithName (field.name, testObject)
110
- }
111
- continue
112
- }
113
- when (val result = findObjectField(fieldValue, value, visitedObjects)) {
114
- is FieldName -> {
115
- if (fieldName != null ) {
116
- return MultipleFieldsMatching
117
- } else {
118
- fieldName = result.fieldName
119
- }
120
- }
121
-
122
- MultipleFieldsMatching -> return result
123
- NotFound -> {}
124
- }
125
- }
126
- return if (fieldName != null ) FieldName (fieldName) else NotFound
127
- }
128
-
129
- private sealed class AtomicReferenceOwnerWithName (val fieldName : String ) {
130
- class StaticOwnerWithName (fieldName : String , val clazz : Class <* >) :
131
- AtomicReferenceOwnerWithName (fieldName)
132
-
133
- class InstanceOwnerWithName (fieldName : String , val receiver : Any ) :
134
- AtomicReferenceOwnerWithName (fieldName)
135
- }
136
70
}
137
71
138
72
/* *
0 commit comments