Skip to content

Commit 6721173

Browse files
authored
fix: improve workaround for Scala3 bug 18248 (#1349)
Towards #325
1 parent 92b38e7 commit 6721173

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

actor-typed/src/main/scala/org/apache/pekko/actor/typed/internal/ExtensionsImpl.scala

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,44 +38,43 @@ private[pekko] trait ExtensionsImpl extends Extensions { self: ActorSystem[_] wi
3838
* Hook for ActorSystem to load extensions on startup
3939
*/
4040
def loadExtensions(): Unit = {
41+
loadExtensionsFor("pekko.actor.typed.library-extensions", throwOnLoadFail = true)
42+
loadExtensionsFor("pekko.actor.typed.extensions", throwOnLoadFail = false)
43+
}
4144

42-
/*
43-
* @param throwOnLoadFail Throw exception when an extension fails to load (needed for backwards compatibility)
44-
*/
45-
def loadExtensionsFor(key: String, throwOnLoadFail: Boolean): Unit = {
46-
47-
settings.config.getStringList(key).asScala.foreach { extensionIdFQCN =>
48-
// it is either a Scala object or it is a Java class with a static singleton accessor
49-
val idTry = dynamicAccess.getObjectFor[AnyRef](extensionIdFQCN).recoverWith {
50-
case _ => idFromJavaSingletonAccessor(extensionIdFQCN)
51-
}
45+
/*
46+
* @param throwOnLoadFail Throw exception when an extension fails to load (needed for backwards compatibility)
47+
*/
48+
private def loadExtensionsFor(key: String, throwOnLoadFail: Boolean): Unit = {
5249

53-
idTry match {
54-
case Success(id: ExtensionId[_]) => registerExtension(id)
55-
case Success(_) =>
56-
if (!throwOnLoadFail) log.error("[{}] is not an 'ExtensionId', skipping...", extensionIdFQCN)
57-
else throw new RuntimeException(s"[$extensionIdFQCN] is not an 'ExtensionId'")
58-
case Failure(problem) =>
59-
if (!throwOnLoadFail)
60-
log.error(s"While trying to load extension $extensionIdFQCN, skipping...", problem)
61-
else throw new RuntimeException(s"While trying to load extension [$extensionIdFQCN]", problem)
62-
}
50+
settings.config.getStringList(key).asScala.foreach { extensionIdFQCN =>
51+
// it is either a Scala object or it is a Java class with a static singleton accessor
52+
val idTry = dynamicAccess.getObjectFor[AnyRef](extensionIdFQCN).recoverWith {
53+
case _ => idFromJavaSingletonAccessor(extensionIdFQCN)
6354
}
64-
}
6555

66-
def idFromJavaSingletonAccessor(extensionIdFQCN: String): Try[ExtensionId[Extension]] =
67-
dynamicAccess.getClassFor[ExtensionId[Extension]](extensionIdFQCN).flatMap[ExtensionId[Extension]] {
68-
(clazz: Class[_]) =>
69-
Try {
70-
val singletonAccessor = clazz.getDeclaredMethod("getInstance")
71-
singletonAccessor.invoke(null).asInstanceOf[ExtensionId[Extension]]
72-
}
56+
idTry match {
57+
case Success(id: ExtensionId[_]) => registerExtension(id)
58+
case Success(_) =>
59+
if (!throwOnLoadFail) log.error("[{}] is not an 'ExtensionId', skipping...", extensionIdFQCN)
60+
else throw new RuntimeException(s"[$extensionIdFQCN] is not an 'ExtensionId'")
61+
case Failure(problem) =>
62+
if (!throwOnLoadFail)
63+
log.error(s"While trying to load extension $extensionIdFQCN, skipping...", problem)
64+
else throw new RuntimeException(s"While trying to load extension [$extensionIdFQCN]", problem)
7365
}
74-
75-
loadExtensionsFor("pekko.actor.typed.library-extensions", throwOnLoadFail = true)
76-
loadExtensionsFor("pekko.actor.typed.extensions", throwOnLoadFail = false)
66+
}
7767
}
7868

69+
private def idFromJavaSingletonAccessor(extensionIdFQCN: String): Try[ExtensionId[Extension]] =
70+
dynamicAccess.getClassFor[ExtensionId[Extension]](extensionIdFQCN).flatMap[ExtensionId[Extension]] {
71+
(clazz: Class[_]) =>
72+
Try {
73+
val singletonAccessor = clazz.getDeclaredMethod("getInstance")
74+
singletonAccessor.invoke(null).asInstanceOf[ExtensionId[Extension]]
75+
}
76+
}
77+
7978
final override def hasExtension(ext: ExtensionId[_ <: Extension]): Boolean = findExtension(ext) != null
8079

8180
final override def extension[T <: Extension](ext: ExtensionId[T]): T = findExtension(ext) match {

0 commit comments

Comments
 (0)