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

Scala example source code file (TemplateInfoTransformComponent.scala)

This example Scala source code file (TemplateInfoTransformComponent.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

compilationunit, list, plugincomponent, symbol, symbol, templatetransformer, templatetransformer, transformer, tree, tree, type, type, typemap, valdef

The Scala TemplateInfoTransformComponent.scala source code

package plugintemplate

import scala.tools.nsc._
import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.transform.InfoTransform
// import scala.tools.nsc.transform.TypingTransformers

/** This class implements a plugin component using tree transformers and
 *  InfoTransformer. An InfoTransformer will be automatically created
 *  and registered in <code>SymbolTable.infoTransformers. If
 *  a <code>Typer is needed during transformation, the component
 *  should mix in <code>TypingTransformers. This provides a local
 *  variable <code>localTyper: Typer that is always updated to
 *  the current context.
 *
 *  @todo Adapt the name of this class to the plugin, and implement it.
 */
class TemplateInfoTransformComponent(val global: Global) extends PluginComponent
                                                         // with TypingTransformers
                                                         with InfoTransform {

  import global._
  import global.definitions._

  val runsAfter = List[String]("refchecks")
  /** The phase name of the compiler plugin
   *  @todo Adapt to specific plugin.
   */
  val phaseName = "plugintemplateinfotransform"

  def transformInfo(sym: Symbol, tp: Type): Type = infoTransformer.mapOver(tp)

  def newTransformer(unit: CompilationUnit) = new TemplateTransformer

  /** The type transformation applied by this component. The trait InfoTransform
   *  will create an instance of InfoTransformer applying this TypeMap. The type
   *  map will be applied when computing a symbol's type in all phases
   *  <em>after "plugintemplateinfotransform".
   *
   *  @todo Implement.
   */
  private val infoTransformer = new TypeMap {
    def apply(tp: Type): Type = tp match {
      case MethodType(pts, rt) =>
          println("methodType (_, _, ..) => "+ rt)
          tp
      case _ => mapOver(tp)
    }
  }

  /** The tree transformer that implements the behavior of this
   *  component. Change the superclass to <code>TypingTransformer
   *  to make a local typechecker <code>localTyper available.
   *
   *  @todo Implement.
   */
  class TemplateTransformer extends /*Typing*/ Transformer {
    /** When using <code>preTransform, each node is
     *  visited before its children.
     */
    def preTransform(tree: Tree): Tree = tree match {
      case ValDef(_, name, _, _) =>
        println("pre-info-transforming valdef "+ name)
        tree
      case _ => tree
    }

    /** When using <code>postTransform, each node is
     *  visited after its children.
     */
    def postTransform(tree: Tree): Tree = tree match {
      case _ => tree
    }

    override def transform(tree: Tree): Tree = {
      postTransform(super.transform(preTransform(tree)))
    }
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala TemplateInfoTransformComponent.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.