Skip to content

Commit

Permalink
don't swallow exceptions when instanciating Scalafix
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Apr 17, 2024
1 parent 6d8e50f commit fd28b64
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/main/scala/scalafix/internal/sbt/BlockingCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,29 @@ class BlockingCache[K, V] {

private val underlying = new ju.concurrent.ConcurrentHashMap[K, V]

private object SkipUpdate extends Exception

// Evaluations of `transform` are guaranteed to be sequential for the same key
def compute(key: K, transform: Option[V] => Option[V]): V = {
// ju.Map.compute does not return the value if the BiFunction does not update it,
// so we store it in a var to return it
var unchangedValue: V = null.asInstanceOf[V]
var originalValue: V = null.asInstanceOf[V]
Try(
underlying.compute(
key,
new ju.function.BiFunction[K, V, V] {
override def apply(key: K, prev: V): V = {
unchangedValue = prev
transform(Option(prev))
// throw an exception if None is returned to skip reassignement
.get
originalValue = prev
transform(Option(prev)).getOrElse(throw SkipUpdate)
}
}
)
).getOrElse(unchangedValue)
).fold(
{
case SkipUpdate => originalValue
case ex => throw ex
},
identity
)
}
}

0 comments on commit fd28b64

Please sign in to comment.