Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] How to inject a static field into a multitude of types with AspectJ?

I would like to add a log4j.Logger private static field into a multitude of types. For instance, into all the types annotated with the @Path annotation.

This my current aspect code:

    public aspect LoggingAspect {
      public interface HttpHandlerType {}
      declare parents: (@Path *) implements HttpHandlerType;
     
      public Logger HttpHandlerType.Log = Logger.getLogger(getClass());
     
      pointcut httpHandlerMethods(HttpHandlerType o) : within(HttpHandlerType+) &&
        execution(@(GET || PUT || POST || DELETE) public * *.*(..)) && this(o);
     
      before(HttpHandlerType o): httpHandlerMethods(o) {
        if (o.Log.isInfoEnabled()) {
          o.Log.info(logMethod(thisJoinPoint));
        }
      }
     
      after(HttpHandlerType o) returning (Object result): httpHandlerMethods(o) {
        if (o.Log.isDebugEnabled()) {
          o.Log.debug(logMethod(thisJoinPoint, result));
        }
      }
     
      after(HttpHandlerType o) throwing (Exception e): httpHandlerMethods(o) {
        if (o.Log.isEnabledFor(Level.ERROR)) {
          o.Log.error(logMethod(thisJoinPoint), e);
        } 
      }
   
      private static String logMethod(JoinPoint jp) {
        ...
      }
   
      private static String logMethod(JoinPoint jp, Object result) {
        ...
      }
    }

The problem is that the Log field is an instance field, while it should be a static one. But one cannot specify a static field inside an interface.

So my question is how to change the aspect implementation to make the Log a static field?

Thanks.

--
Be well and prosper.
==============================
"There are two kinds of people.Those whose guns are loaded and those who dig."
   ("The good, the bad and the ugly")
So let us drink for our guns always be loaded.


Back to the top