score:1
let's define test compiler plugin.
core/src/main/scala/main.scala
abstract class genericcontroller[t] {
def getpath():string
def save(entity:t):string = "parent"
}
class statuscontroller extends genericcontroller[string] {
override def getpath():string = "/statuses"
override def save(entity:string):string = "child"
}
plugin/src/main/resources/scalac-plugin.xml
<plugin>
<name>mycompilerplugin</name>
<classname>compilerplugin.mycompilerplugin</classname>
</plugin>
plugin/src/main/scala/compilerplugin/mycompilerplugin.scala
package compilerplugin
import scala.tools.nsc.{global, phase}
import scala.tools.nsc.plugins.{plugin, plugincomponent}
class mycompilerplugin(val global: global) extends plugin {
import global._
val name = "mycompilerplugin"
val description = "my compiler plugin"
val components: list[plugincomponent] = list[plugincomponent](mycomponent)
private object mycomponent extends plugincomponent {
val global: mycompilerplugin.this.global.type = mycompilerplugin.this.global
val runsafter: list[string] = list[string](/*"typer"*/"jvm")
val phasename: string = mycompilerplugin.this.name
def newphase(_prev: phase) = new myphase(_prev)
class myphase(prev: phase) extends stdphase(prev) {
override def name: string = mycompilerplugin.this.name
def apply(unit: compilationunit): unit = {
for (tree@q"$mods def $tname[..$tparams](...$paramss): $tpt = $expr" <- unit.body) {
println(s"tree=$tree, symbol=${tree.symbol.fullname}, symbol.overrides=${tree.symbol.overrides.map(_.fullname)}")
}
}
}
}
}
build.sbt
thisbuild / name := "compiler-plugin-demo"
lazy val commonsettings = seq(
scalaversion := "2.13.3",
version := "1.0",
)
lazy val plugin = project
.settings(
commonsettings,
librarydependencies ++= seq(
scalaorganization.value % "scala-reflect" % scalaversion.value,
scalaorganization.value % "scala-compiler" % scalaversion.value,
)
)
lazy val core = project
.settings(
commonsettings,
scalacoptions ++= seq(
"-xplugin:plugin/target/scala-2.13/plugin_2.13-1.0.jar",
"-xplugin-require:mycompilerplugin"
)
)
if you run (sbt reload; clean; plugin/package; core/compile
) this compiler plugin after typer
phase it prints
tree=def <init>(): genericcontroller[t] = {
genericcontroller.super.<init>();
()
}, symbol=genericcontroller.<init>, symbol.overrides=list()
tree=def getpath(): string, symbol=genericcontroller.getpath, symbol.overrides=list()
tree=def save(entity: t): string = "parent", symbol=genericcontroller.save, symbol.overrides=list()
tree=def <init>(): statuscontroller = {
statuscontroller.super.<init>();
()
}, symbol=statuscontroller.<init>, symbol.overrides=list()
tree=override def getpath(): string = "/statuses", symbol=statuscontroller.getpath, symbol.overrides=list(genericcontroller.getpath)
tree=override def save(entity: string): string = "child", symbol=statuscontroller.save, symbol.overrides=list(genericcontroller.save)
but if you run it after jvm
phase it prints
tree=def getpath(): string, symbol=genericcontroller.getpath, symbol.overrides=list()
tree=def save(entity: object): string = "parent", symbol=genericcontroller.save, symbol.overrides=list()
tree=def <init>(): genericcontroller = {
genericcontroller.super.<init>();
()
}, symbol=genericcontroller.<init>, symbol.overrides=list()
tree=override def getpath(): string = "/statuses", symbol=statuscontroller.getpath, symbol.overrides=list(genericcontroller.getpath)
tree=override def save(entity: string): string = "child", symbol=statuscontroller.save, symbol.overrides=list()
tree=override <bridge> <artifact> def save(entity: object): string = statuscontroller.this.save(entity.$asinstanceof[string]()), symbol=statuscontroller.save, symbol.overrides=list(genericcontroller.save)
tree=def <init>(): statuscontroller = {
statuscontroller.super.<init>();
()
}, symbol=statuscontroller.<init>, symbol.overrides=list()
so as you can see, after typer
phase, method symbol statuscontroller.getpath
overrides method symbol genericcontroller.getpath
and statuscontroller.save
overrides genericcontroller.save
. and after jvm
phase statuscontroller.getpath
again overrides genericcontroller.getpath
but it's bridge statuscontroller.save
instead of ordinary statuscontroller.save
that overrides genericcontroller.save
. so i guess you confused symbol of bridge method with symbol of ordinary method.
Source: stackoverflow.com
Related Query
- Scala Compiler Plugin - Determine whether method is overridden
- Scala compiler plugin to rewrite method calls
- Blacklist some method calls with Scala Compiler plugin
- Scala compiler cannot determine which overloaded method to invoke
- Why won't the Scala compiler apply tail call optimization unless a method is final?
- What factors could determine whether Clojure, Scala or Haskell will gain traction?
- Any info out there on migrating a Scala 2.9 compiler plugin to 2.10?
- Enabling the macro-paradise Scala compiler plugin in Maven projects
- scala - trying to print overridden toString method
- How to disable the method return type hint in IntellijIdea scala plugin
- Create new *package* in a Scala Compiler Plugin
- Generating Scala code trees from a Scala compiler plugin
- Pass closure to Scala compiler plugin
- Scala compiler error due to constructor parameter (property) having same name in both base and derived class and used in derived method
- Scala invariant generic type parameter ignored by method parameter type depending whether argument is literal expression versus variable
- Using IntelliJ, how can i determine whether particular function stems from Java or Scala
- Scala compiler says "No TypeTag available for T" in method using generics
- How to get Scala Compiler Plugin to work in Scala IDE
- unapply method of a case class is not used by the Scala compiler to do pattern matching, why is that?
- Scala Compiler Plugin Rewrite Function Definition As A Tuple: error: not found: value scala.Tuple2
- Scala compiler says my method is recursive in case when implicits and anonymous class is used
- Best way to write a generic method that checks whether a number is 0 in scala
- Is there a way in a Scala after() method to know whether the test failed?
- How scala compiler invoking abstract method from super class?
- Scala - bypass overridden toString method
- sbt: can i put the source of scala compiler plugin into a project that needs to be compiled using that plugin?
- Dynamic loading of a Scala compiler plugin
- Loading Native library in Scala Compiler Plugin
- Gradle + Scala Plugin + Zinc 1.3.5 leaks Protobuf 3.7 to the compiler classpath
- How to add Scala compiler plugin only for test sources
More Query from same tag
- Why is different a variable or a copy of the variable?
- How to @Autowire list of beans by Trait?
- NoWarn Is Not A Valid Choice for -Wunused Compiler Error
- Run scalaz tasks on custom pool (ExecutorService)
- playframework scala code efficiency and future problems
- inferred type arguments [Any,Int] do not conform to method's type parameter bounds [T <: Comparable[T],V <: T]
- Custom TypeConverters using spark cassandra connector
- XGBoost4J - Scala dataframe to sparse dmatrix
- Com.google.inject.CreationException: Unable to create injector
- Scala Style: pattern matching with all branches being wildcards with pattern guard
- Long running transactions in Slick
- Different return types for function Java
- Spark SQL 2.0: NullPointerException with a valid PostgreSQL query
- How to transform a string column of a dataframe into a column of Array[String] with Apache Spark and Scala
- How to define a function that takes a function literal (with an implicit parameter) as an argument?
- Operate on neighbor elements in RDD in Spark
- Error when trying to mock a DataFrameReader object when using ScalaMock
- Scala: Alternative for deprecated set difference
- Saving double dataype in spark dataframe
- Dynamically add elements to fs2.Stream
- Deciding between use of foldleft() or Seq.newBuilder in Scala
- SBT unmanaged jars
- Drop duplicate attribute in json parsing in spark scala
- "error: missing parameter type" during macro splicing
- Composing two ActionBuilders in Play
- Streams in Scala
- SBT increase compilation memory
- Convert Java List to Scala Seq
- Infinite loop scala code
- Spark SQL count() returns wrong number