alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (GenAnnotationInfos.scala)

This example Scala source code file (GenAnnotationInfos.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

annotationinfo, apply, genannotationinfos, reflection, reifier, reify, tree

The GenAnnotationInfos.scala Scala example source code

package scala.reflect.reify
package codegen

trait GenAnnotationInfos {
  self: Reifier =>

  import global._

  // usually annotations are reified as their originals from Modifiers
  // however, when reifying free and tough types, we're forced to reify annotation infos as is
  // why is that bad? take a look inside
  def reifyAnnotationInfo(ann: AnnotationInfo): Tree = {
    val reifiedArgs = ann.args map { arg =>
      val saved1 = reifyTreeSymbols
      val saved2 = reifyTreeTypes

      try {
        // one more quirk of reifying annotations
        //
        // when reifying AnnotatedTypes we need to reify all the types and symbols of inner ASTs
        // that's because a lot of logic expects post-typer trees to have non-null tpes
        //
        // Q: reified trees are pre-typer, so there's shouldn't be a problem.
        //    reflective typechecker will fill in missing symbols and types, right?
        // A: actually, no. annotation ASTs live inside AnnotatedTypes,
        //    and insides of the types is the place where typechecker doesn't look.
        state.reifyTreeSymbols = true
        state.reifyTreeTypes = true

        // todo. every AnnotationInfo is an island, entire of itself
        // no regular Traverser or Transformer can reach it
        // hence we need to run its contents through the entire reification pipeline
        // e.g. to apply reshaping or to check metalevels
        reify(arg)
      } finally {
        state.reifyTreeSymbols = saved1
        state.reifyTreeTypes = saved2
      }
    }

    // if you reify originals of anns, you get SO when trying to reify AnnotatedTypes, so screw it - after all, it's not that important
    val Apply(Select(New(tpt), name), args) = annotationToTree(ann)
    val reifiedAtp = mirrorCall(nme.Select, mirrorCall(nme.New, mirrorCall(nme.TypeTree, reifyType(tpt.tpe))), reify(name))
    val reifiedAnnRepr = mirrorCall(nme.Apply, reifiedAtp, reifyList(args))
    mirrorFactoryCall(nme.Annotation, reifiedAnnRepr)
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala GenAnnotationInfos.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.