简体   繁体   中英

AspectJ can't work on Scala function literal?

I have the following scala class and annotated aspectj class:

package playasjectj

import org.aspectj.lang.annotation.Pointcut
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before

class Entity {
    def foo(p:String):String ={
      return p
    }
    def bar(handler:(String,String,Long)=>String):Unit={
        handler("first", "second", 100L)
    }
}

object Entity {
  def main(args:Array[String]){
     val inst = new Entity
     inst.foo("we are the champion")
     val handler = (first:String, second:String, value:Long) => {
          first + second + ":" + value
     }
     inst.bar(handler)
  }
}

@Aspect
class EntityAspect{

    @Pointcut("execution(* foo(String) ) && target(playasjectj.Entity) && args(p)")
    def pointcut_foo(p:String):Unit={}

    @Pointcut("execution(* bar(scala.Function3<String,String,Long,String>)) && target(playasjectj.Entity) && args(handler)")
    def pointcut_bar(handler: (String,String,Long)=>String):Unit={}

    @Before("pointcut_foo(p)")
    def beforeAdvice_foo(p:String):Unit={
        println("before advice foo: "  + p)
    }

    @Before("pointcut_bar(handler)")
    def beforeAdvice_bar(handler:(String,String,Long)=>String):Unit={
        println("before advice bar:")
    }

}

function bar works well, but function foo doesn't. There is no any errors, seems the execution of function "foo" is not caught.

[AppClassLoader@14dad5dc] info AspectJ Weaver Version 1.8.5 built on Thursday Jan 29, 2015 at 01:03:58 GMT

[AppClassLoader@14dad5dc] info register classloader sun.misc.Launcher$AppClassLoader@14dad5dc

[AppClassLoader@14dad5dc] info using configuration /Users/grant/programming/java/workspace/playasjectj/bin/META-INF/aop.xml

[AppClassLoader@14dad5dc] info register aspect playasjectj.EntityAspect before advice foo: we are the champion

Anyone knows how to solve the problem? I guess it is related to how scala transform the tuple into java class

solved by myself. the problem is not function literal, but type "Long". If I use "java.lang.Long" in that situation, that works. for generic type, AspectJ expects "Type",not primitive. in Scala, numeric type values like Int, Long, even Boolean are equivalent to java primitive type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM