Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AspectJ加载时编织-命令行项目_Java_Command Line_Jar_Aspectj_Load Time Weaving - Fatal编程技术网

Java AspectJ加载时编织-命令行项目

Java AspectJ加载时编织-命令行项目,java,command-line,jar,aspectj,load-time-weaving,Java,Command Line,Jar,Aspectj,Load Time Weaving,几天以来,我一直在为一个简单的项目苦苦挣扎,而这个项目我就是无法运行 我的目标是扩展.jar文件的功能,以便获得有关该jar内部工作的更多信息。jar不是开源的。 我使用了很多不同的概念将自己的代码“注入”到jvm中,比如“ASM”-库、BECL、Javassist,最后是AspectJ AspectJ引起了我的注意,因为它似乎更易于使用,而且级别更高。为了在将AspectJ与封闭源代码.jar一起使用之前尝试它的工作方式,我设置了测试项目: 项目1: 为了测试它,我想创建一个简单的项目,其中包

几天以来,我一直在为一个简单的项目苦苦挣扎,而这个项目我就是无法运行

我的目标是扩展.jar文件的功能,以便获得有关该jar内部工作的更多信息。jar不是开源的。 我使用了很多不同的概念将自己的代码“注入”到jvm中,比如“ASM”-库、BECL、Javassist,最后是AspectJ

AspectJ引起了我的注意,因为它似乎更易于使用,而且级别更高。为了在将AspectJ与封闭源代码.jar一起使用之前尝试它的工作方式,我设置了测试项目:

项目1:

为了测试它,我想创建一个简单的项目,其中包含一个应该编译并打包到.jar文件中的类。以后应该使用方面来监视此类。我从互联网上的一个示例中获取了这个类的源代码: 类的文件内容如下所示: YourClass.java:

package com.test.java.instrumentation;

public class YourClass {

    public static void main(String[] args) {
        YourClass yourClass = new YourClass();
        yourClass.yourMethodBefore();
        yourClass.yourMethodAfter();
        yourClass.yourMethodAround(1);
        yourClass.yourMethodAround(1,"Test");
    }

    public void yourMethodBefore() {
        System.out.println("Executing TestTarget.yourMethodBefore()");
    }

    public void yourMethodAfter(){
        System.out.println("Executing TestTarget.yourMethodAfter()");
    }

    public void yourMethodAround(Integer i){
        System.out.println("Executing TestTarget.yourMethodAround()");
        System.out.println("i : "+i);
    }

    public void yourMethodAround(Integer i,String x){
        System.out.println("Executing TestTarget.yourMethodAround()");
        System.out.println("i : "+i);
        System.out.println("x : "+x);
    }
}
我使用以下命令编译代码:

javac -d bin -cp ./src/ ./src/com/test/java/instrumentation/YourClass.java
javac -cp ./lib/aspectjrt.jar;.lib/aspejctjweaver.jar;./src;. -d ./bin/ ./src/com/test/java/instrumentation/YourAspect.java
java -Daj.weaving.verbose=true -Dorg.aspectj.weaver.showWeaveInfo=true -javaagent:./Aspect/lib/aspectjweaver.jar -cp ./Aspect/lib/aspectjrt.jar;./Aspect/lib/aspejctjtools.jar;. -jar ./JavaProgram/bin/program.jar
命令运行时没有错误。 在下一步中,我创建了一个清单文件,其中包含在.jar文件中运行该类所需的信息: MANIFEST.MF:

Manifest-Version: 1.0
Main-Class: com.test.java.instrumentation.YourClass
Built-By: Me
Build-Jdk: 1.8.0_131
在下一步中,我将创建一个.jar:

cd bin\
jar cvfm program.jar ..\meta-inf\manifest.mf .
.jar可以通过以下方式成功执行:

java -jar program.jar
输出如下所示:

Executing TestTarget.yourMethodBefore()
Executing TestTarget.yourMethodAfter()
Executing TestTarget.yourMethodAround()
i : 1
Executing TestTarget.yourMethodAround()
i : 1
x : Test
到目前为止还不错。步骤1已完成。这是一个示例应用程序,稍后应使用AspectJ的加载时编织扩展它的附加功能

项目2: 在下一步中,我想创建一个AspectJ.jar文件,其中包含扩展Project1功能的代码: Priject2(YourAspect.java)的内容如下:

package com.test.java.instrumentation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.JoinPoint;

@Aspect
public class YourAspect {

    //Patterns
    //blank = modifier (public/private/protected or default(blank) should be looked for
    //* = return type to look for. Void/Object/Primitive type
    //com.jayway.YourClass.yourMethodBefore(..) = PackageName . ClassName . methodName (parameters)
    @Before("execution (* com.test.java.instrumentation.YourClass.yourMethodBefore(..))")
    //JointPoint = the reference of the call to the method
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("YourAspect's BeforeAdvice's body is now executed Before yourMethodBefore is called.");
    }
    //Patterns
    //public = look for the specific modifier named public
    //!Object = Basically we are looking for void or primitives. But if we specified Object we could get a good pattern
    //com.jayway.YourClass.yourMethodBefore(..) = PackageName . ClassName . methodName (parameters)
    @After("execution (public !Object com.test.java.instrumentation.YourClass.yourMethodAfter(..))")
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("YourAspect's afterAdvice's body is now executed After yourMethodAfter is called.");
    }
    //Patterns
    //!private = look for any modifier that's not private
    //void = looking for method with void
    //com.jayway.YourClass.yourMethodBefore(..) = PackageName . ClassName . methodName (parameters)
    @Around("execution (!private void com.test.java.instrumentation.YourClass.yourMethodAround(Integer,..))")
    //ProceedingJointPoint = the reference of the call to the method.
    //The difference between ProceedingJointPoint and JointPoint is that a JointPoint can't be continued (proceeded)
    //A ProceedingJointPoint can be continued (proceeded) and is needed for an Around advice
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        //Default Object that we can use to return to the consumer
        Object returnObject = null;
        try {
            System.out.println("YourAspect's aroundAdvice's body is now executed Before yourMethodAround is called.");
            //We choose to continue the call to the method in question
            returnObject = joinPoint.proceed();
            //If no exception is thrown we should land here and we can modify the returnObject, if we want to.
        } catch (Throwable throwable) {
            //Here we can catch and modify any exceptions that are called
            //We could potentially not throw the exception to the caller and instead return "null" or a default object.
            throw throwable;
        }
        finally {
            //If we want to be sure that some of our code is executed even if we get an exception
            System.out.println("YourAspect's aroundAdvice's body is now executed After yourMethodAround is called.");
        }

        return returnObject;
    }
    //Patterns
    //blank = modifier (public/private/protected or default(blank) should be looked for
    //* = return type to look for. Void/Object/Primitive type
    //com.jayway.YourClass.yourMethod*(..) = PackageName . ClassName . * (parameters)
    //Where the "*" will catch any method name
    @After("execution ( * com.test.java.instrumentation.YourClass.*(..))")
    //JointPoint = the reference of the call to the method
    public void printNewLine(JoinPoint pointcut){
        //Just prints new lines after each method that's executed in
        System.out.print("\n\r");
    }
}
[AppClassLoader@18b4aac2] info AspectJ Weaver Version 1.8.10 built on Monday Dec 12, 2016 at 19:07:48 GMT
[AppClassLoader@18b4aac2] info register classloader sun.misc.Launcher$AppClassLoader@18b4aac2
[AppClassLoader@18b4aac2] info no configuration found. Disabling weaver for class loader sun.misc.Launcher$AppClassLoader@18b4aac2
Executing TestTarget.yourMethodBefore()
Executing TestTarget.yourMethodAfter()
Executing TestTarget.yourMethodAround()
i : 1
Executing TestTarget.yourMethodAround()
i : 1
x : Test
我还创建了MANIFEST.FM:

Manifest-Version: 1.0
Main-Class: com.test.java.instrumentation.YourAspect
Built-By: Me
Build-Jdk: 1.8.0_131
和一个aop.xml:

<aspectj>

    <aspects>
        <aspect name="com.test.java.instrumentation.YourAspect"/>
   </aspects>

   <weaver options="-XlazyTjp">
       <include within="com.test.java.instrumentation..*"/>
   </weaver>

</aspectj>
生成的类文件与清单和aop.xml一起打包到.jar中:

cd bin\
jar cvfm aspect.jar ..\meta-inf\manifest.mf ..\meta-inf\aop.xml .
当我尝试运行示例程序(Project1)并尝试编织方面(Project2)时,我使用以下命令:

javac -d bin -cp ./src/ ./src/com/test/java/instrumentation/YourClass.java
javac -cp ./lib/aspectjrt.jar;.lib/aspejctjweaver.jar;./src;. -d ./bin/ ./src/com/test/java/instrumentation/YourAspect.java
java -Daj.weaving.verbose=true -Dorg.aspectj.weaver.showWeaveInfo=true -javaagent:./Aspect/lib/aspectjweaver.jar -cp ./Aspect/lib/aspectjrt.jar;./Aspect/lib/aspejctjtools.jar;. -jar ./JavaProgram/bin/program.jar
加载失败,导致的错误如下:

package com.test.java.instrumentation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.JoinPoint;

@Aspect
public class YourAspect {

    //Patterns
    //blank = modifier (public/private/protected or default(blank) should be looked for
    //* = return type to look for. Void/Object/Primitive type
    //com.jayway.YourClass.yourMethodBefore(..) = PackageName . ClassName . methodName (parameters)
    @Before("execution (* com.test.java.instrumentation.YourClass.yourMethodBefore(..))")
    //JointPoint = the reference of the call to the method
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("YourAspect's BeforeAdvice's body is now executed Before yourMethodBefore is called.");
    }
    //Patterns
    //public = look for the specific modifier named public
    //!Object = Basically we are looking for void or primitives. But if we specified Object we could get a good pattern
    //com.jayway.YourClass.yourMethodBefore(..) = PackageName . ClassName . methodName (parameters)
    @After("execution (public !Object com.test.java.instrumentation.YourClass.yourMethodAfter(..))")
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("YourAspect's afterAdvice's body is now executed After yourMethodAfter is called.");
    }
    //Patterns
    //!private = look for any modifier that's not private
    //void = looking for method with void
    //com.jayway.YourClass.yourMethodBefore(..) = PackageName . ClassName . methodName (parameters)
    @Around("execution (!private void com.test.java.instrumentation.YourClass.yourMethodAround(Integer,..))")
    //ProceedingJointPoint = the reference of the call to the method.
    //The difference between ProceedingJointPoint and JointPoint is that a JointPoint can't be continued (proceeded)
    //A ProceedingJointPoint can be continued (proceeded) and is needed for an Around advice
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        //Default Object that we can use to return to the consumer
        Object returnObject = null;
        try {
            System.out.println("YourAspect's aroundAdvice's body is now executed Before yourMethodAround is called.");
            //We choose to continue the call to the method in question
            returnObject = joinPoint.proceed();
            //If no exception is thrown we should land here and we can modify the returnObject, if we want to.
        } catch (Throwable throwable) {
            //Here we can catch and modify any exceptions that are called
            //We could potentially not throw the exception to the caller and instead return "null" or a default object.
            throw throwable;
        }
        finally {
            //If we want to be sure that some of our code is executed even if we get an exception
            System.out.println("YourAspect's aroundAdvice's body is now executed After yourMethodAround is called.");
        }

        return returnObject;
    }
    //Patterns
    //blank = modifier (public/private/protected or default(blank) should be looked for
    //* = return type to look for. Void/Object/Primitive type
    //com.jayway.YourClass.yourMethod*(..) = PackageName . ClassName . * (parameters)
    //Where the "*" will catch any method name
    @After("execution ( * com.test.java.instrumentation.YourClass.*(..))")
    //JointPoint = the reference of the call to the method
    public void printNewLine(JoinPoint pointcut){
        //Just prints new lines after each method that's executed in
        System.out.print("\n\r");
    }
}
[AppClassLoader@18b4aac2] info AspectJ Weaver Version 1.8.10 built on Monday Dec 12, 2016 at 19:07:48 GMT
[AppClassLoader@18b4aac2] info register classloader sun.misc.Launcher$AppClassLoader@18b4aac2
[AppClassLoader@18b4aac2] info no configuration found. Disabling weaver for class loader sun.misc.Launcher$AppClassLoader@18b4aac2
Executing TestTarget.yourMethodBefore()
Executing TestTarget.yourMethodAfter()
Executing TestTarget.yourMethodAround()
i : 1
Executing TestTarget.yourMethodAround()
i : 1
x : Test
如何将我的方面(Project2)注入到正在运行的program.jar(Project1)中? 你有什么想法吗

我在下面的链接中附加了所有文件,包括我创建的源代码和清单(两个项目)作为.zip文件,因此您也可以尝试我迄今为止所做的工作。这可能会帮助您更容易发现我的错误。该项目设计为在windows命令行上运行

链接:


非常感谢你的想法

首先,上传压缩的可执行文件很简单。下次托管源。无论如何,您的编译命令看起来有点不对劲-难道.jar不应该遵循-cp选项吗?看起来这使得编译无法如期完成。您能提供您正在尝试使用的.jar的名称吗?还是源头?查看生成的.jar内容-它只包含示例类.zip文件包含源代码、编译命令和目录结构(包括.jar文件)。如果你不信任他们,就不要管理这些罐子。两种编译都工作正常,不会抛出错误。正如我所说,源代码包含在.zip文件中。有人找到了解决方案吗?我也被困在同一个地方problem@HurrDurr你找到解决办法了吗?