Skip to content

Commit ad9d047

Browse files
authored
Add Handler and Collector (#301)
1 parent ea4c69a commit ad9d047

File tree

5 files changed

+626
-8
lines changed

5 files changed

+626
-8
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2022 UnitTestBot contributors (utbot.org)
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress("PropertyName")
18+
19+
package org.jacodb.ets.utils
20+
21+
import org.jacodb.ets.base.EtsEntity
22+
import org.jacodb.ets.base.EtsStmt
23+
24+
class EntityCollector<R : Any, C : MutableCollection<R>>(
25+
val result: C,
26+
val block: (EtsEntity) -> R?,
27+
) : AbstractHandler() {
28+
override fun handle(value: EtsEntity) {
29+
val item = block(value)
30+
if (item != null) {
31+
result += item
32+
}
33+
}
34+
35+
override fun handle(stmt: EtsStmt) {
36+
// Do nothing.
37+
}
38+
}
39+
40+
fun <R : Any, C : MutableCollection<R>> EtsEntity.collectEntitiesTo(
41+
destination: C,
42+
block: (EtsEntity) -> R?,
43+
): C {
44+
accept(EntityCollector(destination, block))
45+
return destination
46+
}
47+
48+
fun <R : Any, C : MutableCollection<R>> EtsStmt.collectEntitiesTo(
49+
destination: C,
50+
block: (EtsEntity) -> R?,
51+
): C {
52+
accept(EntityCollector(destination, block))
53+
return destination
54+
}

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetUses.kt renamed to jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,31 @@
1616

1717
package org.jacodb.ets.utils
1818

19+
import org.jacodb.ets.base.EtsAssignStmt
1920
import org.jacodb.ets.base.EtsEntity
21+
import org.jacodb.ets.base.EtsLocal
2022
import org.jacodb.ets.base.EtsStmt
23+
import org.jacodb.ets.model.EtsMethod
2124

22-
fun EtsStmt.getUses(): Sequence<EtsEntity> =
23-
getOperands().flatMap { sequenceOf(it) + it.getUses() }
25+
fun EtsMethod.getDeclaredLocals(): Set<EtsLocal> =
26+
cfg.stmts.mapNotNullTo(mutableSetOf()) {
27+
if (it is EtsAssignStmt && it.lhv is EtsLocal) {
28+
it.lhv
29+
} else {
30+
null
31+
}
32+
}
2433

25-
fun EtsEntity.getUses(): Sequence<EtsEntity> =
26-
getOperands().flatMap { sequenceOf(it) + it.getUses() }
34+
fun EtsMethod.getLocals(): Set<EtsLocal> {
35+
val result = mutableSetOf<EtsLocal>()
36+
cfg.stmts.forEach { it.collectEntitiesTo(result) { it as? EtsLocal } }
37+
return result
38+
}
39+
40+
fun EtsStmt.getLocals(): Set<EtsLocal> {
41+
return collectEntitiesTo(mutableSetOf()) { it as? EtsLocal }
42+
}
43+
44+
fun EtsEntity.getLocals(): Set<EtsLocal> {
45+
return collectEntitiesTo(mutableSetOf()) { it as? EtsLocal }
46+
}

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetValues.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import org.jacodb.ets.base.EtsEntity
2020
import org.jacodb.ets.base.EtsStmt
2121
import org.jacodb.ets.base.EtsValue
2222

23-
fun EtsStmt.getValues(): Sequence<EtsValue> =
24-
getOperands().filterIsInstance<EtsValue>()
23+
fun EtsStmt.getValues(): Set<EtsValue> {
24+
return collectEntitiesTo(mutableSetOf()) { it as? EtsValue }
25+
}
2526

26-
fun EtsEntity.getValues(): Sequence<EtsValue> =
27-
getOperands().filterIsInstance<EtsValue>()
27+
fun EtsEntity.getValues(): Set<EtsValue> {
28+
return collectEntitiesTo(mutableSetOf()) { it as? EtsValue }
29+
}

0 commit comments

Comments
 (0)