伙计们,我需要切入点以按特定类过滤被调用函数。
public aspect intLogin {
private capture c = new capture();
pointcut login() : execution(public * login(..))
before ():login(){
c.print();
}
}
这是我的方面,我想知道哪个类调用登录功能。你能帮我吗?
答案 0 :(得分:1)
所有其他人调用的帮助程序类:
package de.scrum_master.app;
public class Other {
public void doOther() {}
}
具有各种内部类的驱动程序应用程序:
这里有
package de.scrum_master.app;
public class Application {
public String foo(int number) {
new Other().doOther();
return "foo";
}
public class MyInner {
public void doSomething() {
new Other().doOther();
}
}
public static class MyStaticInner {
public void doSomethingElse() {
new Other().doOther();
}
}
public static void main(String[] args) {
new Application().foo(11);
new Application().new MyInner().doSomething();
new Application.MyStaticInner().doSomethingElse();
class LocalInner {
public void doWhatever() {
new Other().doOther();
}
}
new LocalInner().doWhatever();
new Runnable() {
@Override public void run() {
new Other().doOther();
}
}.run();
}
}
请注意记录呼叫者的类名:
package de.scrum_master.aspect;
public aspect CallingClassLogger {
before(Object caller) : !within(CallingClassLogger) && call(* *(..)) && this(caller) {
System.out.println(caller.getClass().getName());
}
}
控制台日志:
de.scrum_master.app.Application
de.scrum_master.app.Application$MyInner
de.scrum_master.app.Application$MyStaticInner
de.scrum_master.app.Application$1LocalInner
de.scrum_master.app.Application$1
您的方面将打印类似
Application.java:5
Application.java:11
Application.java:17
Application.java:28
Application.java:35
如果您对类名感兴趣的话,这对IMO不太有用。
答案 1 :(得分:-1)
我使用thisJoinPoint.getSourceLocation()解决了这个问题。 代码是:
public aspect intLogin {
private capture c = new capture();
pointcut login(Object a) : call(public * login(..)) && (target(a)) && this(capture);
before (Object x):login( spring.aop.so_52992365.intLogin) {
String xString = x.toString();
System.out.println("The class that is calling the function is:" + thisJoinPoint.getSourceLocation());
c.print();
}
}