macros - Scala annotations are not found -


I have a case class with an annotated field, such as:

  the case class fu (   

I have a macro that processes the declaration of this class:

  val (className, access, field, chairs, body) = classical Match {Case Q} Case Class $ n $ m (.. $ ps) Expands. $ BS {.. $ ss} "=> (N, M, PS, BS, SS) Case _ = & After that, I search for the aligned field like:  
  val aliases = Fields.asInstanceOf [List [ValDef]]. FlatMap {Field => Field.symbol.annotations.collect {// Excluded Version: // Case Annotation if annotation.tpe & lt ;: & lt; Cv.weakTypeOf [Alias ] = & Gt; if annotation.TTPA & lt ;: & lt; C.weakTypeOf [nickname] = & gt; // dislike version: // annotation. ScalaArgs.head match {annotation.tree.children.tail.head match {The matter is verbatim (constant (absolute: string)) => (Param, field.name)}}}   

However, the list of nicknames is exhausted I have decided That field. Symbol.annotations.size, in fact, 0, sit clearly on the field Despite annotations.

What's wrong?

Edit

Answering the first two comments:

(1) I tried modifications, but he Did not work. He actually gives the list [annotation] instead of the list [tree], back by symbol. Perhaps I did not modify the code correctly, but the instantaneous effect during macro expansion was an exception, I want to play some more with it.

(2) A class declaration is caught when slapping an annotation macro on the category of the case.

The complete code is as follows. It is further illustrated in the test code.

  package com.xxx.util.macros import scala.collection.immutable.hashMap import scala.language.experimental.macros import scala.annotation. Static annotation import scale.flatt.macro.witbox attribute map {def $ (key: string) = _vals.get (key) protected def + = (key: string, value: any) = _vals + = ((key, value ) Private Var_vals = New Hashmap [String, Any]} Extends the Square Alias ​​(Val Key: String) Extended Static Annotation Class aliased StaticAnnotation {defMacrotronforms (Annotates: Any *): Any = Macro-aliated Macro.impl } Object aliasedMacro {def inf (c): Whitebox.Cont Ext) (Annotates: CXP (APA) *): CXP (API) = {import c. Universe _ Wal (classdecal, compedecle) = annotates.map (_ tree) match {case (clause: classadaf) :: neel => (Claz, None) Case (Claude: ClassDaf) :: (Compu .: Module Def) :: Zero = & gt; (Clause, some comp) case _ = & gt; Cancel (C, "A class should notify")} Val (ClassName, Access, Field, Base, Body) = Class Match Match {Case Q "Case Class $ N $ M ($. $ SS) Expanded .. $ BS {.. $ ss} "= & gt; (N, M, PS, BS, SS) Case _ = & gt; Abortion (c, "@leases only supported on case class")} Val mapping = field Instanceoff [List [Waldiff]]. FlatMap {field = & gt; Field.symbol.annotations.collect {Annotation of the case if annotation.tree.tpe & lt ;: & lt; C.weakTypeOf [nickname] = & gt; Annotation.tree.children.tail.head match {matter verbally (constant (absolute: string)) => Q "" "This $ = ($ param, $ {field.name})" ""}}} val class code = q "" "case class $ squarename $ access ($. $ Field) expands .. $ Bases {. $ Body; .. $ mappings} "c.exp (compDeccl match {case some (compCode) => q" "" $ compCode; $ classCode "" case none => q "" "$ ClassCode" ""}}} protected def lock (c: whitebox.Context, message: string) = c.abort (c.enclosingPosition, message)}   

test code: < / P>

  package test.xxx.util.macros import org.scalatest.FunSuite import org.scalatest.junit.JUnitRunner import org.junit.runner.RunWith import com.xxx.util.macros._ @ Aliased case class fu (@ alias ("foo") fu: int, d ("Foo", "Extra") Examination ("By its own name") ("bar") bar: string, falcon: string) @runwith (classfo [juneintiner]) Class Mappest Fans FanSite {val foo = 13 val bar Field "is aliased") {Asterart Result (some (FU)) (OJJ $ "FU"}} "Another string" with LID "field" ({some (bar)) (obj $ " Bar ") emphasize (none) (obj $" bar ")" test "(" unleaded field ") {assertResult (none) (obj $" baz ")}}    

Thanks for the tips! Finally, help using field.mods.notations is how it works:

  Val mappings = fields. Instanceoff [List [Waldiff]]. FlatMap {field = & gt; Field = (names (name (name (name ("name") (name (pronoun ("nickname"), name-name constructor), list (literal (constant (absolute: string))) => Q "" " This + = ($ param, $ {field.name}) "" "}}    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -