Skip to main content

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

Hi,

Currently you can't make those static declarations on interfaces, but
it has been requested a few times and I would like to get to it soon.
In the meantime you can simulate it using a pertypewithin
instantiation model.  This thread discusses it:

http://aspectj.2085585.n4.nabble.com/Introduce-static-members-into-several-classes-at-once-td2080794.html

hope that helps,

cheers,
Andy

On 7 December 2011 01:44, mark Kharitonov <mark.kharitonov@xxxxxxxxx> wrote:
> 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.
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>


Back to the top