[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Re: Get field read

I have managed to do this with the @Before advice and some reflection.
This should have some performance impact but with the  @AfterReturning  advice
the field is already set  can not be modified.


On 8/13/07, Bengali Bengali <bengalister@xxxxxxxxx> wrote:

i am an aspectj beginner and i have difficulties to write a pointcut and actually
i am wondering if what i try to achieve is possible with aspectj.

I would like to get every field within every one of mypackages which are marked with an annotation
at read time and if null set it a value.


package com.mycompany.beans
class MyBean  {
  @Logger mylogger;

   public void test() {
      mylogger.info("hello world");


If mylogger is null i would like to instantiate it.

I tried with the following pointcut:

    @Before("get(KeyLogger com.mycompany.beans..*.*) && @annotation(kLogger)")
    public void setLoggerIfNull(Logger kLogger,JoinPoint thisJoinPoint) {

The problem is that the joint point target's is an instance of MyBean (owner of mylogger attribute).
How could i access the field directly without knowing its name (it can be any class and i don't know
how the field will be named)?

Thanks in advance,