简体   繁体   English

Aspectj是否可以访问Scala中方法的隐式参数?

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

I'm using aspectJ to do some user authentication. 我正在使用AspectJ进行一些用户身份验证。 It would be really nice to have the userId as an implicit parameter to a method. 将userId作为方法的隐式参数将是非常好的。 But I don't know if an aspectJ joinPoint can see an implicit parameter. 但是我不知道AspectJ joinPoint是否可以看到隐式参数。

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 ): 隐式参数只是作为类文件中的“正常”参数出现(从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: 因此使用“正常” AspectJ切入点,例如:

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 ). 有效(适用于scalac生成的.class文件)。

From aspectj's point of view, an implicit parameter is nothing special. 从Aspectj的角度来看,隐式参数没什么特别的。 The Scala compiler merges arguments from multiple argument lists into a single one, and this is stored in the .class file. Scala编译器将多个参数列表中的参数合并为一个,并将其存储在.class文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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