I am kind of blocked with the following (macro annotation)
situation. Suppose I have an annotation called @factory
which
aims to generate an apply
method for the annotated trait in the corresponding companion object. For instance, given the trait A
:
@factory
trait A {
val a1: Int
}
the expected code to be generated is the following one:
object A extends Factory[A] {
def apply(_a1: Int) = new A {
val a1 = _a1
}
}
Now suppose we have a trait B
which inherits from A
:
@factory
trait B extends A {
val b1: String
}
which is supposed to generate:
object B extends Factory[B] {
def apply(_a1: Int, _b1: String) = new B {
val a1 = _a1
val b1 = _b1
}
}
In the latter case, I need to know which are the attributes existing at A
, but I don't know how to get any information about them. While dealing with macro annotations I have only access to the B
trait AST (as a ClassDef
). Although its template
contains references to the parents (as TypeTrees
), both fields tpe
and symbol
are empty.
It would be great for me to get access to the A
AST. However, I think that's not feasible. Therefore, any symbol or type (pointing either the parent or the current type) would be good enough.
If you want to see more implementation details, I have uploaded the project to https://github.com/jesuslopez-gonzalez/cool-factory. It can generate the apply
for the local values.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…