简体   繁体   中英

Does aspectj have access to implicit parameters on a method in Scala?

I'm using aspectJ to do some user authentication. It would be really nice to have the userId as an implicit parameter to a method. But I don't know if an aspectJ joinPoint can see an implicit parameter.

Anyone else try this?

Given a service

class User(val name: String) {
  override def toString = s"$name"
}

class Service {
  def run()(implicit user: User) {
    println(user.name)
  }

the implicit parameter just turns up as a "normal" argument in the class file (output from javap ):

public class app.scala.Service extends java.lang.Object{
    public void run(app.scala.User);
    public app.scala.Service();
}

so using a "normal" AspectJ pointcut like:

before(Service s, User user) : 
    call(void Service.run(User)) && target(s) && args(user) {

    System.out.println("Hello from AspectJ (before service run)");
    System.out.println("  "  + user);
}

works (applied to the .class files generated by scalac ).

From aspectj's point of view, an implicit parameter is nothing special. The Scala compiler merges arguments from multiple argument lists into a single one, and this is stored in the .class file.

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