如何以编程方式生成扩展注释特征的抽象 Scala class?
How to programmatically generate an abstract Scala class extending an annotated trait?
给定一个带注释的特征,我应该如何生成实现该特征的抽象 class?
因此,鉴于以下用户特征...
@Neuron
trait SomeTrait {
// ...
}
...在我的图书馆中,我想在其旁边插入如下内容:
abstract class SomeTraitImpl extends SomeTrait
请注意,除了用 @Neuron
注释外,我对给定的特征一无所知。
我已经尝试使用 ASM 来实现这一点,实现问题 Using Scala traits with implemented methods in Java 的答案中解释的概念,但这个概念只触及了 Scala 编译器作为字节代码发出的内容的表面。即使我成功地掌握了 var、val、lazy val、abstract override 等所有可能的组合,它也很有可能会在 Scala 编译器的下一个版本中崩溃。
所以看起来我应该改为编写一个编译时宏。但是,我正在为 Scala 宏的文档抓耳挠腮,所以我想知道是否有人可以起草一些让我入门的东西?请提供任何提示!
正确的做法是把@Neuron
做成macro annotation (using the Macro Paradise compiler plugin) and implement the macro to do the code transformation. The resulting code is now part of Neuron DI,一个我写的依赖注入框架。
给定一个带注释的特征,我应该如何生成实现该特征的抽象 class?
因此,鉴于以下用户特征...
@Neuron
trait SomeTrait {
// ...
}
...在我的图书馆中,我想在其旁边插入如下内容:
abstract class SomeTraitImpl extends SomeTrait
请注意,除了用 @Neuron
注释外,我对给定的特征一无所知。
我已经尝试使用 ASM 来实现这一点,实现问题 Using Scala traits with implemented methods in Java 的答案中解释的概念,但这个概念只触及了 Scala 编译器作为字节代码发出的内容的表面。即使我成功地掌握了 var、val、lazy val、abstract override 等所有可能的组合,它也很有可能会在 Scala 编译器的下一个版本中崩溃。
所以看起来我应该改为编写一个编译时宏。但是,我正在为 Scala 宏的文档抓耳挠腮,所以我想知道是否有人可以起草一些让我入门的东西?请提供任何提示!
正确的做法是把@Neuron
做成macro annotation (using the Macro Paradise compiler plugin) and implement the macro to do the code transformation. The resulting code is now part of Neuron DI,一个我写的依赖注入框架。