If you encapsulated all your logging in the aspect (and so didn't need to
access it from outside the aspect), you could use a pertypewithin aspect
which creates an aspect instance per woven class.

Any instance based state in your aspect is then effectively per-type.
See
http://stackoverflow.com/questions/12126436/aspectj-creating-a-global-logger-field-using-an-inter-type-declaration


public aspect LoggingAspect    pertypewithin(org.foo..*)              //
per-type association
{    Logger logger;

    after() : staticinitialization(*) {    // run 1x after class-loading
        logger = Logger.getLogger(
            getWithinTypeName()            // type associated with aspect
instance
        );
    }

    pointcut logged() :                    // what to log, e.g. public
methods
        execution(public * *(..));         // (pointcut could also be
abstract
                                           // and refined in sub-aspects)

    before() : logged() {
        logger.log(...);                   // logging action
    }
}

>Is it really necessary if AspectJ used for compile time weaving and not
used any of the classes or methods within the advices, to have the
aspectj-library within your program?

I answered this in stackoverflow the other day. Yes, it is necessary. The
small runtime not only contains stuff to support thisJoinPoint/etc but also
internally used classes for handling pointcuts like cflow and exceptions
like 'NoSuchAspectException' that may occur. It would be possibly to
generate all this stuff during a build, leaving you no dependencies on an
aspectj runtime but not enough people have requested it to justify the
effort.

cheers,

Andy


On 12 February 2013 08:02, Kempff, Malte <malte.kem...@de.equens.com> wrote:

>  So I cannot use any static fields of the classes, unless they are
> officially in common like using ITD, is that right?
>
> ITD would be theoretically fine, but  I already use the static logger
> field so I need probably care about the name, right?
>
> Well just as question. Is it really necessary if AspectJ used for compile
> time weaving and not used any of the classes or methods within the advices,
> to have the aspectj-library within your program?
>
>
>
>
>
>
>
>
>
> *Von:* aspectj-users-boun...@eclipse.org [mailto:
> aspectj-users-boun...@eclipse.org] *Im Auftrag von *Alexander Kriegisch
> *Gesendet:* Dienstag, 12. Februar 2013 16:49
>
> *An:* aspectj-users@eclipse.org
> *Betreff:* Re: [aspectj-users] using static fields in an advice
>
>
>
> As Matthew said, you could use reflection, but the factory method
> Logger.getLogger is probably faster and will return the very same Logger
> instance as the one stored in your static field.
>
>
> As for the AspectJ runtime, you need it at runtime, thus the name.
>
>
> Alexander Kriegisch
>
>
>
>  all I would like to know if it is possible to user a calls field (static
> field in an advice and how does it look like.
>
> Could be that I did not understand something. But I thought when using
> AspectJ to compile time the enriched program could be run without
> aspectj-library under certain circumstances.
>
> But this is just another addon-question. Using a static field in an advice
> could be worthy in also not logging-circumstances, couldn’t it?
>
> ------------------------------------------------------------------------------------------------
> Disclaimer: The contents of this electronic mail message are only binding
> upon Equens or its affiliates, if the contents of the message are
> accompanied by a lawfully recognised type of signature. The contents of
> this electronic mail message are privileged and confidential and are
> intended only for use by the addressee. If you have received this
> electronic mail message by error, please notify the sender and delete the
> message without taking notices of its content, reproducing it and using it
> in any way.
> ------------------------------------------------------------------------------------------------
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@eclipse.org
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
_______________________________________________
aspectj-users mailing list
aspectj-users@eclipse.org
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Reply via email to