Skip to content

Commit bd965a9

Browse files
committed
refactor: simplify and make errors more explicit for Utils#frame method
1 parent e7c1223 commit bd965a9

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ See also the [CHANGELOG](https://github.com/cucumber/cucumber-jvm/blob/master/CH
1313

1414
### Changed
1515

16+
- [Internal] Fix `getLocation` wherever it was wrongly defined. ([#404](https://github.com/cucumber/cucumber-jvm-scala/issues/404))
17+
1618
### Deprecated
1719

1820
### Removed
@@ -24,6 +26,7 @@ See also the [CHANGELOG](https://github.com/cucumber/cucumber-jvm/blob/master/CH
2426
### Changed
2527

2628
- [Internal] Implement `HookDefinition.getHookType` and unimplement `Location.getLocation`. No impact expected for users.
29+
([#401](https://github.com/cucumber/cucumber-jvm-scala/issues/401))
2730

2831
## [8.29.0] (2025-07-15)
2932

cucumber-scala/src/main/scala/io/cucumber/scala/Utils.scala

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,25 @@ private[scala] object Utils {
99
val frames = Thread.currentThread().getStackTrace
1010
val currentClass = self.getClass.getName
1111
// Note: the -1 check is here for Scala < 2.13 and objects
12-
frames.reverse
13-
.find(f => f.getClassName == currentClass && f.getLineNumber != -1)
14-
.get
12+
findLast(frames)(f =>
13+
f.getClassName == currentClass && f.getLineNumber != -1
14+
) match {
15+
case Some(stackFrame) => stackFrame
16+
case None =>
17+
throw new IllegalStateException(
18+
s"Not able to find stack frame for $currentClass"
19+
)
20+
}
21+
}
22+
23+
// Copied from Scala 2.13 library, not available in 2.12 nor in scala-collections-compat
24+
private def findLast[A](seq: Array[A])(p: A => Boolean): Option[A] = {
25+
val it = seq.reverseIterator
26+
while (it.hasNext) {
27+
val elem = it.next()
28+
if (p(elem)) return Some(elem)
29+
}
30+
None
1531
}
1632

1733
}

0 commit comments

Comments
 (0)