gpt4 book ai didi

java - AspectJ(注释)编译时与 Ant 和 NetBeans 编织

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:23:00 25 4
gpt4 key购买 nike

我想在 NetBeans 中使用编译时 AspectJ 和 Ant。我想在 Google App Engine 上运行它,但目前这不是必需的。 AspectJ 是基于注解的。
我更喜欢编译时编织(类的修改、检测?)。我不想使用自定义类加载器。如何实现?

我已经拥有的:

我试过了 AspectJ Annotation Tutorial与 NetBeans。我修改了 build.xml 以处理 aspectj(使用 iajc Ant task ),如 here 所述.问题是它需要添加 -javaagent:lib/aspectjweaver.jar(在 GAE 上不可能)。
运行我的构建生成此输出:

info compiling C:\NetBeansProjects\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java
weaveinfo Join point 'method-call(void java.io.PrintStream.println(java.lang.String))' in Type 'net.andrewewhite.examples.HelloWorld' (HelloWorld.java:9) advised by before advice from 'net.andrewewhite.aspects.BasicAspect' (BasicAspect.class:17(from BasicAspect.java))
weaveinfo Join point 'method-call(void java.io.PrintStream.println(java.lang.String))' in Type 'net.andrewewhite.examples.HelloWorld' (HelloWorld.java:9) advised by after advice from 'net.andrewewhite.aspects.BasicAspect' (BasicAspect.class:23(from BasicAspect.java))
info woven class net.andrewewhite.examples.HelloWorld (from C:\NetBeansProjects\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java)
info Compiler took 2547ms

当我使用 -javaagent 参数运行我的项目时,它工作正常。 (在 NetBeans 中:单击项目>属性>运行>VM 选项:-javaagent:./dist/lib/aspectjweaver.jar)。来自 tutorial 的代码输出:

run:
About to make call to print Hello World
Hello World!
Just made call to print Hello World
BUILD SUCCESSFUL (total time: 0 seconds)

没有代理(清除 VM 选项)代码就像没有 AspectJ 一样运行:

run:
Hello World!
BUILD SUCCESSFUL (total time: 0 seconds)

来源:

\TryAspectJ\src\META-INF\aop.xml

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<aspect name="net.andrewewhite.aspects.BasicAspect" />
</aspects>
</aspectj>

\TryAspectJ\src\net\andrewewhite\aspects\BasicAspect.java

package net.andrewewhite.aspects;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class BasicAspect {

@Before(" call(void java.io.PrintStream.println(java.lang.String)) " +
"&& !within(net.andrewewhite.aspects..*)")
public void beforePrintlnCall() {
System.out.println("About to make call to print Hello World");
}

@After(" call(void java.io.PrintStream.println(java.lang.String)) " +
"&& !within(net.andrewewhite.aspects..*)")
public void afterPrintlnCall() {
System.out.println("Just made call to print Hello World");
}
}

\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java

package net.andrewewhite.examples;

public class HelloWorld {
public static void main(String[] argv) {
System.out.println("Hello World!");
}
}

\TryAspectJ\build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="TryAspectJ" default="default" basedir=".">
<description>Builds, tests, and runs the project TryAspectJ.</description>
<import file="nbproject/build-impl.xml"/>
<taskdef classpath="lib/aspectj/aspectjtools.jar" resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"/>

<target name="aspectj">
<echo level="info">--- aspectj (start) ---</echo>
<iajc destDir="${build.classes.dir}" source="1.6" target="1.6" showweaveinfo="true" verbose="true" >
<inpath>
<pathelement location="lib/aspectj/aspectjrt.jar"/>
<pathelement location="${build.classes.dir}" />
</inpath>
<sourceroots>
<pathelement location="${src.dir}"/>
</sourceroots>
<classpath>
<pathelement location="${javac.classpath}"/>
<pathelement location="${j2ee.platform.classpath}"/>
</classpath>
</iajc>
 <echo level="info">--- aspectj finished ---</echo>
</target>

<target name="-post-compile" depends="aspectj"></target>
</project>

我必须添加或更改什么?

最佳答案

找到解决方案。这是正确的 build.xml 片段:

<taskdef classpath="lib/aspectjtools.jar" resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"/>
<target name="aspectj">
<echo level="info">--- aspectj (start) ---</echo>
<!-- begin fix classpath for this bug https://issues.apache.org/bugzilla/show_bug.cgi?id=40291 -->
<condition property="targetos" value="windows" else="unix">
<os family="windows"/>
</condition>
<!-- converting classpath -->
<pathconvert targetos="${targetos}" property="javac.convertedClasspath" >
<path path="${javac.classpath}" />
</pathconvert>
<!-- end fix classpath -->
<iajc source="1.6" target="1.6" showweaveinfo="true" verbose="true" destdir="${build.classes.dir}" >
<inpath>
<pathelement location="${build.classes.dir}"/>
</inpath>
<classpath>
<pathelement location="${javac.convertedClasspath}" />
</classpath>
</iajc>
<echo level="info">--- aspectj (finished) ---</echo>
</target>
<target name="-post-compile" depends="aspectj"></target>

调用 java -classpath ./lib/aspectjrt.jar -jar TryAspectJ.jar 工作正常。奇怪的是,来自 NetBeans(在项目上右键单击 -> 运行)的结果就像没有 AspectJ 一样。我认为 NetBeans 从 build/classes 目录而不是 jar 运行项目。但这不是问题。

关于java - AspectJ(注释)编译时与 Ant 和 NetBeans 编织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6678400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com