开发者

AfterAdvice for all constructors of classes statically implementing an interface

I want to do some authorization-checks for domain objects. This includes checks if someone is allowed to instantiate an object (depending of it's type - this check is done externally so no need to solve th开发者_运维百科is).

All our domain objects implement one specific interface (directly or indirectly)

What I need is an advice which runs after the instantiation of a domain object and is able to use the created instance (needed for the determination of the permission). (Additionally the advice may not execute when the constructor is called out of hibernate)

I want to implement this using AspectJ (which is working yet for methods) ideally using only static analysis as there are no a runtime dependent changes

Now I am trying to create an @AfterReturning adivce which intercepts constructor calls. But I do not get the pointcut working.

What I tried:

@Pointcut("within(a.b.c.DomainObject+) && execution(*.new(..))")

@Pointcut("execution(a.b.c.DomainObject+.new(..))")

But both does not work.

Does anyone know how I can achieve this?

Regards Michael


Here are some examples that helped me figure out how to do something similar. http://www.eclipse.org/aspectj/sample-code.html

Also, here is something that I have in my project--for testing purposes--to add a listener after an object is created:

pointcut init(JComponent j):
    this(j) &&
    initialization(*.new());

after(JComponent j) returning: init(j) {
    if(j instanceof JButton && !(j instanceof AccessibleButton))
        System.out.println(j.toString() + thisJoinPointStaticPart.getSourceLocation());
    j.addFocusListener(new VisualFocusListener());
}

EDIT:

The above will execute every time a constructor is called, whether it is the one called or called by this() or super()--probably not what you want. I have just figured out another way to get at the object returned from the constructor.

after() returning(JComponent j): call(*.new(..)) {
    //do something with j
}

That reads in English almost like it does in code: "after returning a JComponent from a constructor, do something". I have *.new(..) because I don't care which constructor is called or what parameters it takes as long as it gives me a JComponent. You can further limit that by saying Foo.new(..) or Foo.new(int).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜