[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?
|
- From: Andy Clement <andrew.clement@xxxxxxxxx>
- Date: Wed, 7 Dec 2011 08:17:26 -0800
- Delivered-to: aspectj-users@eclipse.org
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=T1ZXjE1gtE9TiimMuMdBlr1pAIBcFmOQ5XactlMHeyQ=; b=xmloxwZBnXHud2Fhj16/O2V4IlceAQnF+I1fZch2CgihwvPv9tN0luC+a5ZKfQea9V bEopUXUFO10I7XaXjhHv8rxpoE+hlvegT8Lt+gXEKCJo2ySOD+e1JPckPb2/IAkDI8kK 1PffOFBQOiqUwWYwC5b1Dlp97TSfIlUtimB6M=
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
>