Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Hi All ! Got a couple of questions regarding AspectJ

Hi -

> However got few
> queries..Hope that you can help me in this regard..

All these questions are answered in the development environment guide 
or the programming guide.  It's best to read the documentation before
posting to the list, not only to avoid burdening others, but also since 
the documentation has been polished/corrected by use for some time,
and you'll be very likely to find not only the correct answer, but
answers to questions you didn't know you had.

That said, in answer to your questions, assuming

  java-src/...
  aspectj-src/...

and that

  {ajc} means ajc -classpath {..}aspectjrt.jar

then ...  

> 1.  I have compiled my application using javac. I develop few aspects
> for this application. I only want to compile the aspects nothing else.
> Is it possible?

  {ajc} -sourceroots aspectj-src -outjar aspects.jar 

> 2.  If the answer to the above question is YES, can i use the new byte
> code weaver with this aspect binaries and normal .class files [javac
> output]??

(btw, the bytecode weaver at this point is almost two years old)

  javac -d classes {files in java-src} 
  jar cfM classes.jar -C classes .
  {ajc} -aspectpath aspects.jar -inpath classes.jar -outjar wovenClasses.jar

then to run

  java -classpath "wovenClasses.jar;aspectjrt.jar" {main-class-name}

> 4. I have compiled sources and aspects with ajc compiler, and if want 
> to
> deliver to the customer, without the aspects that were introduced with
> the compilation..how can I remove those?? Is selective removal of
> aspects possible?? If so could you please let me know the option to be
> used.

You don't remove them.  Just use the unwoven classes.jar.

> 3.  If i want to instrument only few methods, which i dont know at
> compile time of the aspect, what should i do? i.e is it possible to
> provide pointcuts and advices for an aspect at run time??
> 	i.e. I want only few methods of a particular class to be
> instrumented, and i dont know these methods at compile time. I want the
> user to select the methods and packages/classes at runtime via UI.
> 	so that I can have a general aspect, which need not be
> recompiled.

Every weaver now, "dynamic" or otherwise, has to prepare the code for advice.
It's best if you give the weaver some hints as to what code you might be 
interested in advising.  In AspectJ you *can* advise everything and turn on
the advice at runtime.  More likely, you want to say something like,
"For any public method throwing a BusinessException, create advice which
when enabled will throw a BusinessException, in order to test exception
handling."  e.g.,

    // (warning - syntax not verified)
    aspect InjectBusinessExceptions {
       static boolean enabled;
       before() throws BusinessException : 
           if(enabled) && execution(public * *(..) throws BusinessException)
                 && within(com.apps..*) {
           throw new BusinessException("injected");
       }
    }

Then, e.g., you can write an MBean or a GUI to toggle the enabled flag.

If you truly want pluggable advice, you can write around advice in an aspect
that accepts a "handler" for the join point.  Then instead of toggling the
flag, you just register the closure/handler with the aspect, and it will
be used the next time the advice runs.  How the handler is written depends
on what you want to support in your advice.  If you *really* want to do
that, I have some sample code from the AOSD 2004 Good AOP tutorial that
does this.  Also, you might prefer to wait for AspectJ 5's support for
specifying pointcuts for an concrete aspect using configuration.  That 
wouldn't change what the aspect did, but you could decide at load-time
when advice should run.

Wes



Back to the top