我需要使用AspectJ为每个初始化对象注入几个方法.
我想用这个:
pointcut vistaInjection(Object o)
: initialization(java.lang.Object.new() )
&& target(o)
&& !within(objectAspect);
before(Object o): methodInjection(o){System.err.println("INIT");}
Run Code Online (Sandbox Code Playgroud)
切入点对象的初始化,所以我可以将这些方法直接注入到作为每个其他对象一部分的对象中.
但是,它不起作用.你知道为什么吗?或者可能是另一种方式如何100%确定每个初始化对象都是切入点?*.new不适用于字符串,列表和其他内容.
谢谢!
我知道我可以从中获取方法和类名,StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();但这不是我想要的.我想要类对象,所以我可以访问他的界面,注释等...
有可能的?
Class<?> classObject = getCallerClass();
我看到了这个问题,但这只是针对类名.
编辑:现在我以这种方式传递课程:
someService.dummyMethod(foo1, foo2, new Object(){}.getClass());
someService(String foo1, int foo2, Class<?> c) {
// stuff here to get the methodname,
// the interface of the class and an annotation from the interface.
}
Run Code Online (Sandbox Code Playgroud)
我从很多不同的类中调用someService,如果不可能,我将继续这种方式,但是如果有一种方法可以在运行时获取调用者类,我更喜欢这种方式.