Skip to content

Commit 3b5cac0

Browse files
committed
refactor: Simplify reflection in ComposeTestNodeProvider
- Remove robust and last-resort reflection paths for finding `ComposeUiTest`. - The code now only attempts to find the `ComposeUiTest` instance in a field named "composeTest". - Delete unused private helper functions: `allFields` and `allNoArgMethods`.
1 parent 6d0cb70 commit 3b5cac0

File tree

1 file changed

+0
-33
lines changed

1 file changed

+0
-33
lines changed

ui/test-jvm/src/main/kotlin/com/softartdev/notedelight/ComposeTestNodeProvider.kt

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,13 @@ import androidx.compose.ui.test.ComposeUiTest
66
import androidx.compose.ui.test.ExperimentalTestApi
77
import androidx.compose.ui.test.junit4.ComposeContentTestRule
88
import java.lang.reflect.Field
9-
import java.lang.reflect.Method
109

1110
fun reflect(composeTestRule: ComposeContentTestRule): ComposeUiTest {
12-
// Just in case some wrapper directly implements ComposeUiTest.
13-
(composeTestRule as? ComposeUiTest)?.let { return it }
14-
15-
// 1) Fast path: both AndroidComposeTestRule and DesktopComposeTestRule store it in "composeTest".
1611
findField(composeTestRule, "composeTest")?.let { field ->
1712
field.getOrNull(composeTestRule)?.let { value ->
1813
if (value is ComposeUiTest) return value
1914
}
2015
}
21-
22-
// 2) Robust path: scan all fields in the hierarchy and return the first ComposeUiTest found.
23-
for (field in allFields(composeTestRule.javaClass)) {
24-
field.getOrNull(composeTestRule)?.let { value ->
25-
if (value is ComposeUiTest) return value
26-
}
27-
}
28-
29-
// 3) Last resort: scan no-arg methods returning ComposeUiTest.
30-
for (method in allNoArgMethods(composeTestRule.javaClass)) {
31-
if (ComposeUiTest::class.java.isAssignableFrom(method.returnType)) {
32-
method.isAccessible = true
33-
val value = runCatching { method.invoke(composeTestRule) }.getOrNull()
34-
if (value is ComposeUiTest) return value
35-
}
36-
}
37-
3816
throw IllegalStateException(
3917
"Failed to reflect ComposeUiTest from ${composeTestRule.javaClass.name}. " +
4018
"Expected AndroidComposeTestRule/DesktopComposeTestRule internals to contain it."
@@ -52,17 +30,6 @@ private fun findField(target: Any, name: String): Field? {
5230
return null
5331
}
5432

55-
private fun allFields(clazz: Class<*>): Sequence<Field> =
56-
generateSequence(clazz) { it.superclass }
57-
.takeWhile { it != Any::class.java }
58-
.flatMap { it.declaredFields.asSequence() }
59-
60-
private fun allNoArgMethods(clazz: Class<*>): Sequence<Method> =
61-
generateSequence(clazz) { it.superclass }
62-
.takeWhile { it != Any::class.java }
63-
.flatMap { it.declaredMethods.asSequence() }
64-
.filter { it.parameterTypes.isEmpty() }
65-
6633
private fun Field.getOrNull(instance: Any): Any? =
6734
runCatching {
6835
isAccessible = true

0 commit comments

Comments
 (0)