Skip to content

ovechkin-dm/macro-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stopwatch macro

Intercept methods in AOP-like fashion with macro annotations

Usage example

package demo

import impl.{DelegateProducer, Delegated}
import impl.DelegateProducer.MethodInfo

object Main extends App {

  @Delegated
  trait SimpleTrait extends scala.AnyRef {
    def test(i: Int): Int
  }

  val ev = implicitly[DelegateProducer[SimpleTrait]]

  val simple = new SimpleTrait {
    def test(i: Int): Int = i
  }

  val stopWatch: (MethodInfo, () => Any) => Any = { (info, func) =>
    val start = System.currentTimeMillis()
    val result = func()
    val end = System.currentTimeMillis()
    val time = end - start
    println(s"Executed method ${info.methodName}. Args: ${info.args} Took $time ms.")
    result
  }

  val del = ev.delegate(simple)(stopWatch)
  println(del.test(10))


}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages