开发者

AspectJ - Compile-time vs load-time weaving

开发者 https://www.devze.com 2023-02-22 00:15 出处:网络
I am having trouble understanding aspectJ\'s compile-time and load-time weaving and figuring out what to use(and how to use ajc) to compile and build my project.

I am having trouble understanding aspectJ's compile-time and load-time weaving and figuring out what to use(and how to use ajc) to compile and build my project.

Here's my project structure:-

  • TestProject : a java service library. This is being used by a few other projects.开发者_运维问答 This project do not contain any aspects.

  • TestProject-Aspects : Contains just

    aspects which advice a few classes in TestProject. I am not using the

    AspectJ5 annotation style and all my joinpoints are just at the method

    execution currently.

My questions:

  • ajc vs iajc and how are they different?
  • Is there any need for weaving?

  • Will something like this work ?

Compile TestProject-Aspects

<iajc>
    sourceroots=${sources.dir}
    destdir=${classes.dir}
    classpath=${standard.compile.classpath}
</iajc>

Compile TestProject

<iajc>
    sourceroots=${sources.dir}
    destdir=${classes.dir}
    classpath=${standard.compile.classpath}
    inpath=${[TestProject-Aspects]pkg.classpath}
</iajc>
  • Don't I have to use javac at all ? which I was initially using to compile TestProject?


ajc and iajc are extensions of the JDT compiler that comes with Eclipse. So, ajc and iajc will produce exactly the same byte code for pure Java as Eclipse would (which contains some minor differences to Oracle's javac).

ajc and iajc are basically the same except that iajc is incremental (that's the i in iajc). This means that the compiler checks time stamps and does a smarter incremental build if possible and avoids full builds (just like when using AJDT inside of eclipse). Other than this functionality, they are essentially the same. See here for more information:

http://www.eclipse.org/aspectj/doc/released/devguide/antTasks-iajc.html

If a project contains no aspects, using the ajc compiler is optional. These projects can be on the inpath of a project that contains aspects. To compile that contain code-style aspects, then you need to use ajc.

Annotation style aspects are a little different. If you are using annotation style for LTW only, then you can use javac to compile them as long as the correct aop.xml is created weaver is available at runtime.

However, annotation style with CTW weaving does require ajc.

In your particular case above, you can compile TestProject using javac as long as it is on the inpath of your aspect project. This would mean that the class files of your TestProject would be re-written and combined with the class files from your aspect project.

Or, if you are using LTW, then you don't need to add your TestProject to any inpath and you can use javac. But, you must set up your application for LTW at runtime.


EDIT

To answer your comment below:

Yes. You can compile your aspects project first using ajc or the iajc task. Then, you can compile your second, pure java project also by using the iajc task and additionally by putting the results of your first project on the aspect path. You cannot use javac for this at all.

The ant build.xml snippet will look something like this:

<project name="simple-example" default="compile" >
  <taskdef 
      resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
    <classpath>
      <pathelement location="${home.dir}/tools/aspectj/lib/aspectjtools.jar"/>
    </classpath>
  </taskdef>

  <target name="compile" >
    <iajc sourceroots="${home.dir}/TestProject-Aspects/src" 
        classpath="${home.dir}/tools/aspectj/lib/aspectjrt.jar"
        destDir="${home.dir}/TestProject-Aspects/bin"/> 
    <iajc sourceroots="${home.dir}/TestProject/src" 
        classpath="${home.dir}/tools/aspectj/lib/aspectjrt.jar"
        destDir="${home.dir}/TestProject/bin"
        aspectPath="${home.dir}/TestProject-Aspects/bin"/> 
  </target>
</project>

See here for more details on iajc:

http://www.eclipse.org/aspectj/doc/released/devguide/antTasks-iajc.html

0

精彩评论

暂无评论...
验证码 换一张
取 消